Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,6 +74,10 @@ def get_semantic_papers(query):
|
|
| 74 |
"url": p.get("url")
|
| 75 |
} for p in papers]
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def check_plagiarism(text, topic):
|
| 78 |
hits = []
|
| 79 |
for r in get_sources(topic):
|
|
@@ -120,36 +124,40 @@ def generate_latex(text):
|
|
| 120 |
def generate_download_button(file, label, mime_type):
|
| 121 |
b64 = base64.b64encode(file.read()).decode()
|
| 122 |
return f"""
|
| 123 |
-
<a href=\"data:{mime_type};base64,{b64}\" download=\"{label}\">
|
| 124 |
-
\ud83d\udcbe Download {label}
|
| 125 |
-
</a>
|
| 126 |
"""
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# --- Streamlit UI ---
|
| 129 |
st.set_page_config("Deep Research Bot", layout="wide")
|
| 130 |
|
| 131 |
with st.sidebar:
|
| 132 |
-
st.title("
|
| 133 |
-
topic = st.text_input("
|
| 134 |
-
report_type = st.selectbox("
|
| 135 |
"Summary - Short and fast (~2 min)",
|
| 136 |
"Detailed Report (~5 min)",
|
| 137 |
"Thorough Academic Research (~10 min)"
|
| 138 |
])
|
| 139 |
-
tone = st.selectbox("
|
| 140 |
"Objective - Impartial and unbiased presentation of facts and findings",
|
| 141 |
"Persuasive - Advocating a specific point of view",
|
| 142 |
"Narrative - Storytelling tone for layperson readers"
|
| 143 |
])
|
| 144 |
-
source_type = st.selectbox("
|
| 145 |
-
custom_domains = st.text_input("
|
| 146 |
research_button = st.button("Research")
|
| 147 |
|
| 148 |
-
st.title("
|
| 149 |
|
| 150 |
if research_button and topic:
|
| 151 |
try:
|
| 152 |
-
with st.status("
|
| 153 |
st.info("Fetching from sources...")
|
| 154 |
|
| 155 |
all_sources = []
|
|
@@ -174,7 +182,7 @@ if research_button and topic:
|
|
| 174 |
combined_text += f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}...\n\n"
|
| 175 |
citations.append(generate_apa_citation(m['title'], m['url'], m['source']))
|
| 176 |
|
| 177 |
-
with st.spinner("
|
| 178 |
if "Summary" in report_type:
|
| 179 |
prompt = f"""
|
| 180 |
# Topic Overview: {topic}
|
|
@@ -217,27 +225,45 @@ Also, suggest 1-2 relevant open-license images and include their links.
|
|
| 217 |
|
| 218 |
final_output = call_llm([{"role": "user", "content": prompt}])
|
| 219 |
|
| 220 |
-
st.markdown(f" {report_type}")
|
| 221 |
st.markdown(final_output, unsafe_allow_html=True)
|
| 222 |
|
| 223 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
for cite in citations:
|
| 225 |
st.markdown(f"- {cite}")
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
overlaps = check_plagiarism(final_output, topic)
|
| 235 |
if overlaps:
|
| 236 |
-
st.warning("
|
| 237 |
for hit in overlaps:
|
| 238 |
st.markdown(f"- [{hit['title']}]({hit['url']})")
|
| 239 |
else:
|
| 240 |
-
st.success("
|
| 241 |
|
| 242 |
except Exception as e:
|
| 243 |
st.error(f"Error: {e}")
|
|
|
|
| 74 |
"url": p.get("url")
|
| 75 |
} for p in papers]
|
| 76 |
|
| 77 |
+
def get_images(topic):
|
| 78 |
+
response = tavily.image_search(query=topic, max_results=5)
|
| 79 |
+
return response.get("images", [])
|
| 80 |
+
|
| 81 |
def check_plagiarism(text, topic):
|
| 82 |
hits = []
|
| 83 |
for r in get_sources(topic):
|
|
|
|
| 124 |
def generate_download_button(file, label, mime_type):
|
| 125 |
b64 = base64.b64encode(file.read()).decode()
|
| 126 |
return f"""
|
| 127 |
+
<a href=\"data:{mime_type};base64,{b64}\" download=\"{label}\">Download {label}</a>
|
|
|
|
|
|
|
| 128 |
"""
|
| 129 |
|
| 130 |
+
def download_image_as_bytes(url):
|
| 131 |
+
response = requests.get(url)
|
| 132 |
+
if response.status_code == 200:
|
| 133 |
+
return BytesIO(response.content)
|
| 134 |
+
return None
|
| 135 |
+
|
| 136 |
# --- Streamlit UI ---
|
| 137 |
st.set_page_config("Deep Research Bot", layout="wide")
|
| 138 |
|
| 139 |
with st.sidebar:
|
| 140 |
+
st.title("Deep Research Assistant")
|
| 141 |
+
topic = st.text_input("Topic to research")
|
| 142 |
+
report_type = st.selectbox("Type of report", [
|
| 143 |
"Summary - Short and fast (~2 min)",
|
| 144 |
"Detailed Report (~5 min)",
|
| 145 |
"Thorough Academic Research (~10 min)"
|
| 146 |
])
|
| 147 |
+
tone = st.selectbox("Tone of the report", [
|
| 148 |
"Objective - Impartial and unbiased presentation of facts and findings",
|
| 149 |
"Persuasive - Advocating a specific point of view",
|
| 150 |
"Narrative - Storytelling tone for layperson readers"
|
| 151 |
])
|
| 152 |
+
source_type = st.selectbox("Sources to include", ["Web Only", "Academic Only", "Hybrid"])
|
| 153 |
+
custom_domains = st.text_input("Query Domains (Optional)", placeholder="techcrunch.com, forbes.com")
|
| 154 |
research_button = st.button("Research")
|
| 155 |
|
| 156 |
+
st.title("Research Output")
|
| 157 |
|
| 158 |
if research_button and topic:
|
| 159 |
try:
|
| 160 |
+
with st.status("Gathering data..."):
|
| 161 |
st.info("Fetching from sources...")
|
| 162 |
|
| 163 |
all_sources = []
|
|
|
|
| 182 |
combined_text += f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}...\n\n"
|
| 183 |
citations.append(generate_apa_citation(m['title'], m['url'], m['source']))
|
| 184 |
|
| 185 |
+
with st.spinner("Synthesizing report..."):
|
| 186 |
if "Summary" in report_type:
|
| 187 |
prompt = f"""
|
| 188 |
# Topic Overview: {topic}
|
|
|
|
| 225 |
|
| 226 |
final_output = call_llm([{"role": "user", "content": prompt}])
|
| 227 |
|
| 228 |
+
st.markdown(f"### {report_type}")
|
| 229 |
st.markdown(final_output, unsafe_allow_html=True)
|
| 230 |
|
| 231 |
+
with st.spinner("Preparing PDF and LaTeX..."):
|
| 232 |
+
pdf_file = generate_pdf(final_output)
|
| 233 |
+
latex_file = generate_latex(final_output)
|
| 234 |
+
st.markdown(generate_download_button(pdf_file, "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
| 235 |
+
st.markdown(generate_download_button(latex_file, "Research_Report.tex", "application/x-latex"), unsafe_allow_html=True)
|
| 236 |
+
|
| 237 |
+
st.markdown("### Citations (APA Format)")
|
| 238 |
for cite in citations:
|
| 239 |
st.markdown(f"- {cite}")
|
| 240 |
|
| 241 |
+
st.markdown("### Topic-Related Images")
|
| 242 |
+
images = get_images(topic)
|
| 243 |
+
if images:
|
| 244 |
+
cols = st.columns(len(images))
|
| 245 |
+
for i, img in enumerate(images):
|
| 246 |
+
with cols[i]:
|
| 247 |
+
try:
|
| 248 |
+
image_bytes = requests.get(img["url"]).content
|
| 249 |
+
st.image(image_bytes, caption=img.get("title", "Related Image"), use_column_width=True)
|
| 250 |
+
image_data = download_image_as_bytes(img["url"])
|
| 251 |
+
if image_data:
|
| 252 |
+
b64_img = base64.b64encode(image_data.read()).decode()
|
| 253 |
+
href = f'<a href="data:image/jpeg;base64,{b64_img}" download="image_{i+1}.jpg">Download</a>'
|
| 254 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 255 |
+
except Exception as e:
|
| 256 |
+
st.warning(f"Could not load image: {e}")
|
| 257 |
+
else:
|
| 258 |
+
st.info("No related images found.")
|
| 259 |
|
| 260 |
overlaps = check_plagiarism(final_output, topic)
|
| 261 |
if overlaps:
|
| 262 |
+
st.warning("Potential overlaps detected:")
|
| 263 |
for hit in overlaps:
|
| 264 |
st.markdown(f"- [{hit['title']}]({hit['url']})")
|
| 265 |
else:
|
| 266 |
+
st.success("No major overlaps found.")
|
| 267 |
|
| 268 |
except Exception as e:
|
| 269 |
st.error(f"Error: {e}")
|