Spaces:
Sleeping
Sleeping
AnonymousSub
commited on
Commit
•
2b935c8
1
Parent(s):
0bd8ca5
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tenacity import retry, stop_after_attempt, wait_random_exponential
|
2 |
+
from tqdm import tqdm
|
3 |
+
import time
|
4 |
+
import sys
|
5 |
+
|
6 |
+
# import openai
|
7 |
+
import time
|
8 |
+
# import pandas as pd
|
9 |
+
import random
|
10 |
+
import csv
|
11 |
+
import os
|
12 |
+
import pickle
|
13 |
+
import json
|
14 |
+
import nltk
|
15 |
+
nltk.download('punkt')
|
16 |
+
nltk.download('stopwords')
|
17 |
+
from nltk.tokenize import sent_tokenize
|
18 |
+
from nltk.corpus import stopwords
|
19 |
+
import string
|
20 |
+
from typing import List
|
21 |
+
import difflib
|
22 |
+
|
23 |
+
|
24 |
+
# import tiktoken
|
25 |
+
|
26 |
+
import re
|
27 |
+
from nltk.tokenize import sent_tokenize
|
28 |
+
from collections import defaultdict
|
29 |
+
|
30 |
+
|
31 |
+
import nltk
|
32 |
+
from nltk.tokenize import sent_tokenize
|
33 |
+
from nltk.tokenize import word_tokenize
|
34 |
+
import numpy as np
|
35 |
+
from retrieve import get_retrieved_results, get_slide
|
36 |
+
|
37 |
+
# Ensure you have downloaded the 'punkt' tokenizer models
|
38 |
+
nltk.download('punkt')
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
import streamlit as st
|
45 |
+
|
46 |
+
# Get the parent directory
|
47 |
+
# parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
|
48 |
+
# Add the parent directory to the system path
|
49 |
+
# sys.path.append(parent_dir)
|
50 |
+
|
51 |
+
from utils import AzureModels, write_to_file, read_from_file
|
52 |
+
# from utils_open import OpenModels
|
53 |
+
|
54 |
+
# Function to calculate similarity
|
55 |
+
def calculate_similarity(sentence1: str, sentence2: str) -> float:
|
56 |
+
return difflib.SequenceMatcher(None, sentence1, sentence2).ratio()
|
57 |
+
|
58 |
+
# Function to highlight sentences based on similarity
|
59 |
+
def highlight_sentences(predicted: str, ground_truth: str) -> str:
|
60 |
+
ground_truth_sentences = nltk.sent_tokenize(ground_truth)
|
61 |
+
predicted_sentences = nltk.sent_tokenize(predicted)
|
62 |
+
|
63 |
+
highlighted_text = ""
|
64 |
+
|
65 |
+
for pred_sentence in predicted_sentences:
|
66 |
+
max_similarity = 0
|
67 |
+
for gt_sentence in ground_truth_sentences:
|
68 |
+
similarity = calculate_similarity(pred_sentence, gt_sentence)
|
69 |
+
if similarity > max_similarity:
|
70 |
+
max_similarity = similarity
|
71 |
+
# Determine shade of green
|
72 |
+
shade = max_similarity # No need to convert to int, max_similarity is already in [0, 1]
|
73 |
+
highlighted_text += f'<span style="background-color: rgba(0, 255, 0, {shade})">{pred_sentence}</span> '
|
74 |
+
|
75 |
+
return highlighted_text
|
76 |
+
|
77 |
+
st.title('Multi-Document Narrative Generation')
|
78 |
+
|
79 |
+
options = ["Select", "Adobe Firefly", "Adobe Acrobat"]
|
80 |
+
selection = st.selectbox('Select an example', options)
|
81 |
+
|
82 |
+
if selection=="Select":
|
83 |
+
pass
|
84 |
+
elif selection=="Adobe Firefly":
|
85 |
+
with open('wiki_1.json', 'r') as fr:
|
86 |
+
list_1 = json.load(fr)
|
87 |
+
|
88 |
+
with open('wiki_2.json', 'r') as fr:
|
89 |
+
list_2 = json.load(fr)
|
90 |
+
document_name = "Adobe Firefly"
|
91 |
+
section_names = ["Introduction"]*7+["History"]*2
|
92 |
+
ref_doc_indices = np.arange(1,8).tolist() + np.arange(1,3).tolist()
|
93 |
+
else:
|
94 |
+
with open('wiki_2.json', 'r') as fr:
|
95 |
+
list_1 = json.load(fr)
|
96 |
+
|
97 |
+
with open('wiki_1.json', 'r') as fr:
|
98 |
+
list_2 = json.load(fr)
|
99 |
+
document_name = "Adobe Acrobat"
|
100 |
+
section_names = ["Introduction"]*3+["History"]*3+["Document Cloud"]*2
|
101 |
+
ref_doc_indices = np.arange(1,4).tolist() + np.arange(1,4).tolist() + np.arange(1,3).tolist()
|
102 |
+
|
103 |
+
inp_doc_list = []
|
104 |
+
inp_keys_list = []
|
105 |
+
retrieved_doc_list = []
|
106 |
+
|
107 |
+
|
108 |
+
if selection!='Select':
|
109 |
+
# for item, ret_item in zip(list_1, retrieved_out):
|
110 |
+
for item in list_1:
|
111 |
+
for key in item['ref_abstract']:
|
112 |
+
inp_doc_list.append(item['ref_abstract'][key])
|
113 |
+
inp_keys_list.append(key)
|
114 |
+
# retrieved_doc_list.append(ret_item['ref_abstract'][key]['abstract'])
|
115 |
+
# Initialize session state
|
116 |
+
if 'retrieve_clicked' not in st.session_state:
|
117 |
+
st.session_state.retrieve_clicked = False
|
118 |
+
|
119 |
+
retrieve_prompt_template = "{} : Document {} for the '{}' Section of the Article titled '{}'"
|
120 |
+
|
121 |
+
ui_doc_list = []
|
122 |
+
ui_retrieved_doc_list = []
|
123 |
+
|
124 |
+
# 5 input text boxes for 5 input documents
|
125 |
+
st.header('Input Documents')
|
126 |
+
# doc1 = st.text_area('Document 1', value="1. What up bruh??")
|
127 |
+
for i in range(len(section_names)):
|
128 |
+
ui_doc_list.append(st.text_area(retrieve_prompt_template.format(inp_keys_list[i], ref_doc_indices[i], section_names[i], document_name), value=inp_doc_list[i]))
|
129 |
+
|
130 |
+
if st.button('Retrieve'):
|
131 |
+
if 'organize_clicked' not in st.session_state:
|
132 |
+
st.session_state.organize_clicked = False
|
133 |
+
retrieved_out = get_retrieved_results("gpt4o", 0, "fixed", list_2, list_1)
|
134 |
+
write_to_file("retrieved_docs.json", retrieved_out)
|
135 |
+
retrieved_out_train = get_retrieved_results("gpt4o", 0, "fixed", list_1, list_2)
|
136 |
+
write_to_file("retrieved_docs_train.json", retrieved_out_train)
|
137 |
+
|
138 |
+
for ret_item in retrieved_out:
|
139 |
+
for key in ret_item['ref_abstract']:
|
140 |
+
# inp_doc_list.append(item['ref_abstract'][key])
|
141 |
+
retrieved_doc_list.append(ret_item['ref_abstract'][key]['abstract'])
|
142 |
+
|
143 |
+
# Step 2: Lowercase the documents
|
144 |
+
st.session_state.retrieve_clicked = True
|
145 |
+
st.header('Retrieved Documents')
|
146 |
+
|
147 |
+
for i in range(len(section_names)):
|
148 |
+
ui_retrieved_doc_list.append(st.text_area(retrieve_prompt_template.format(inp_keys_list[i], ref_doc_indices[i], section_names[i], document_name), value=retrieved_doc_list[i]))
|
149 |
+
if st.session_state.retrieve_clicked:
|
150 |
+
if st.button('Organize'):
|
151 |
+
if 'summarize_clicked' not in st.session_state:
|
152 |
+
st.session_state.summarize_clicked = False
|
153 |
+
st.session_state.organize_clicked = True
|
154 |
+
st.header("Organization of the documents in the narrative")
|
155 |
+
topics_list = ["Introduction", "History", "Document Cloud"]
|
156 |
+
|
157 |
+
organize_list = []
|
158 |
+
ui_organize_list = []
|
159 |
+
|
160 |
+
test_list = read_from_file("retrieved_docs.json")
|
161 |
+
train_list = read_from_file("retrieved_docs_train.json")
|
162 |
+
organize_out = get_retrieved_results("gpt4o", 1, "fixed", train_list, test_list, True)
|
163 |
+
for i in range(len(organize_out)):
|
164 |
+
organize_list.append(organize_out[i])
|
165 |
+
ui_organize_list.append(st.text_area("Section: " + topics_list[i], value=organize_out[i]))
|
166 |
+
write_to_file("organized_docs.json", organize_out)
|
167 |
+
|
168 |
+
if st.session_state.organize_clicked:
|
169 |
+
if st.button("Summarize"):
|
170 |
+
# if 'narrative_clicked' not in st.session_state:
|
171 |
+
# st.session_state.narrative_clicked = False
|
172 |
+
st.session_state.summarize_clicked = True
|
173 |
+
st.header("Intent-based multi-document summary")
|
174 |
+
topics_list = ["Introduction", "History", "Document Cloud"]
|
175 |
+
generate_list = []
|
176 |
+
ui_generate_list = []
|
177 |
+
slides_list = []
|
178 |
+
test_list = read_from_file("retrieved_docs.json")
|
179 |
+
train_list = read_from_file("retrieved_docs_train.json")
|
180 |
+
organize_out = read_from_file("organized_docs.json")
|
181 |
+
gen_summary_dict = get_retrieved_results("gpt4o", 1, "fixed", train_list, test_list, False, organize_out)
|
182 |
+
for i in range(len(gen_summary_dict)):
|
183 |
+
highlighted_summary = highlight_sentences(gen_summary_dict[i], test_list[i]['abstract'])
|
184 |
+
slides_list.append(get_slide(topics_list[i], gen_summary_dict[i]))
|
185 |
+
# generate_list.append(.format(topics_list[i], gen_summary_dict[i]))
|
186 |
+
st.markdown(f"## {topics_list[i]}")
|
187 |
+
# st.markdown(f"*{gen_summary_dict[i]}*")
|
188 |
+
st.markdown(highlighted_summary, unsafe_allow_html=True)
|
189 |
+
st.header("Generated Narrative")
|
190 |
+
for i in range(len(slides_list)):
|
191 |
+
st.markdown("---")
|
192 |
+
st.markdown(slides_list[i])
|
193 |
+
st.markdown("---")
|
194 |
+
# if st.session_state.summarize_clicked:
|
195 |
+
# if st.button("Narrative"):
|
196 |
+
# st.session_state.narrative_clicked = True
|