Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -188,25 +188,21 @@ def generate_download_button(file, label, mime_type):
|
|
| 188 |
"""
|
| 189 |
|
| 190 |
# --- Streamlit UI ---
|
| 191 |
-
st.set_page_config("Deep Research
|
| 192 |
|
| 193 |
-
#
|
| 194 |
st.markdown("""
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
""", unsafe_allow_html=True)
|
| 200 |
|
| 201 |
st.markdown("""
|
| 202 |
-
|
| 203 |
.stApp { background-color: #0f172a; color: white; }
|
| 204 |
h1, h2, h3 { color: #facc15; }
|
| 205 |
-
|
| 206 |
-
background-color: #1e293b; color: white;
|
| 207 |
-
border-radius: 10px; padding: 10px;
|
| 208 |
-
}
|
| 209 |
-
</style>
|
| 210 |
""", unsafe_allow_html=True)
|
| 211 |
|
| 212 |
with st.sidebar:
|
|
@@ -215,8 +211,8 @@ with st.sidebar:
|
|
| 215 |
report_type = st.selectbox("π Report Type", ["Summary", "Detailed Report", "Thorough Academic Research"])
|
| 216 |
tone = st.selectbox("π― Tone", ["Objective", "Persuasive", "Narrative"])
|
| 217 |
source_type = st.selectbox("π Sources", ["Web Only", "Academic Only", "Hybrid"])
|
| 218 |
-
custom_domains = st.text_input("π Optional Web Domains", placeholder="example.com,
|
| 219 |
-
research_button = st.button("
|
| 220 |
|
| 221 |
st.title("π Research Output")
|
| 222 |
|
|
@@ -228,9 +224,8 @@ if research_button and topic:
|
|
| 228 |
if source_type in ["Web Only", "Hybrid"]:
|
| 229 |
sources += get_sources(topic, custom_domains)
|
| 230 |
if source_type in ["Academic Only", "Hybrid"]:
|
| 231 |
-
|
| 232 |
-
sources +=
|
| 233 |
-
sources += get_semantic_papers(topic) or []
|
| 234 |
return sources
|
| 235 |
|
| 236 |
all_sources, retries = [], 0
|
|
@@ -239,16 +234,14 @@ if research_button and topic:
|
|
| 239 |
if all_sources:
|
| 240 |
break
|
| 241 |
retries += 1
|
| 242 |
-
status.update(label=f"π Retrying... ({retries}) Deeper research underway...")
|
| 243 |
time.sleep(2)
|
| 244 |
|
| 245 |
if not all_sources:
|
| 246 |
-
raise ValueError("
|
| 247 |
|
| 248 |
merged = merge_duplicates(all_sources)
|
| 249 |
|
| 250 |
-
# Image previews
|
| 251 |
-
st.markdown("---")
|
| 252 |
st.subheader("πΌ Source Previews")
|
| 253 |
image_shown = False
|
| 254 |
cols = st.columns(2)
|
|
@@ -258,29 +251,20 @@ if research_button and topic:
|
|
| 258 |
st.image(m["image_url"], caption=m["title"], use_container_width=True)
|
| 259 |
image_shown = True
|
| 260 |
if not image_shown:
|
| 261 |
-
st.info("βΉοΈ No image previews available
|
| 262 |
|
|
|
|
| 263 |
citations = [generate_apa_citation(m['title'], m['url'], m['source']) for m in merged]
|
| 264 |
-
combined_text = "\n\n".join(
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
st.status("π§ Synthesizing final report...")
|
| 269 |
|
| 270 |
prompt = f"""
|
| 271 |
You are an expert research assistant.
|
| 272 |
-
1. Analyze the following sources
|
| 273 |
-
2.
|
| 274 |
-
3. Write a
|
| 275 |
-
|
| 276 |
-
Report Format:
|
| 277 |
-
- Introduction
|
| 278 |
-
- Identified Research Gap
|
| 279 |
-
- Proposed Novel Topic
|
| 280 |
-
- Methodology
|
| 281 |
-
- Comparative Insight
|
| 282 |
-
- Future Work
|
| 283 |
-
- Citations (APA)
|
| 284 |
|
| 285 |
Sources:
|
| 286 |
{combined_text}
|
|
@@ -289,7 +273,6 @@ APA Citations:
|
|
| 289 |
{chr(10).join(citations)}
|
| 290 |
"""
|
| 291 |
|
| 292 |
-
# Generate report
|
| 293 |
st.subheader(f"π {report_type} on '{topic}'")
|
| 294 |
output_placeholder = st.empty()
|
| 295 |
full_output = ""
|
|
@@ -297,68 +280,57 @@ APA Citations:
|
|
| 297 |
full_output += chunk
|
| 298 |
output_placeholder.markdown(full_output, unsafe_allow_html=True)
|
| 299 |
|
| 300 |
-
#
|
| 301 |
st.session_state["last_report"] = full_output
|
| 302 |
|
| 303 |
-
# Downloads
|
| 304 |
-
st.markdown("---")
|
| 305 |
st.subheader("π Downloads")
|
| 306 |
st.markdown(generate_download_button(generate_pdf(full_output), "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
| 307 |
st.markdown(generate_download_button(generate_latex(full_output), "Research_Report.tex", "application/x-latex"), unsafe_allow_html=True)
|
| 308 |
|
| 309 |
-
# Plagiarism
|
| 310 |
-
st.markdown("---")
|
| 311 |
st.subheader("π Plagiarism Check")
|
| 312 |
overlaps = check_plagiarism(full_output, topic)
|
| 313 |
if overlaps:
|
| 314 |
-
st.warning("β οΈ
|
| 315 |
for hit in overlaps:
|
| 316 |
st.markdown(f"- [{hit['title']}]({hit['url']})")
|
| 317 |
else:
|
| 318 |
-
st.success("β
No
|
| 319 |
|
| 320 |
-
# Mind Map Generator
|
| 321 |
-
st.markdown("---")
|
| 322 |
st.subheader("π§ Visual Mind Map")
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
st.warning("β οΈ Please generate a report first.")
|
| 326 |
-
else:
|
| 327 |
-
if st.button("πΊ Generate Mind Map"):
|
| 328 |
mindmap_prompt = [
|
| 329 |
-
{"role": "system", "content": "
|
| 330 |
-
{"role": "user", "content":
|
| 331 |
]
|
| 332 |
mindmap_code = ""
|
| 333 |
for chunk in call_llm(mindmap_prompt):
|
| 334 |
mindmap_code += chunk
|
| 335 |
-
|
| 336 |
-
# Remove wrapping markdown if exists
|
| 337 |
mindmap_code = mindmap_code.replace("```mermaid", "").replace("```", "").strip()
|
| 338 |
-
|
| 339 |
-
# Render mermaid directly
|
| 340 |
st.markdown(f"<div class='mermaid'>{mindmap_code}</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
st.
|
| 345 |
-
st.subheader("π¬ Ask a Follow-Up Question")
|
| 346 |
-
follow_input = st.text_input("Ask a question related to the report above:")
|
| 347 |
if st.button("π Submit Follow-Up"):
|
| 348 |
if "last_report" in st.session_state:
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
follow_placeholder.markdown(follow_output, unsafe_allow_html=True)
|
| 360 |
else:
|
| 361 |
-
st.
|
| 362 |
|
| 363 |
except Exception as e:
|
| 364 |
-
st.error(f"β Error occurred: {e}")
|
|
|
|
| 188 |
"""
|
| 189 |
|
| 190 |
# --- Streamlit UI ---
|
| 191 |
+
st.set_page_config("Deep Research Assistant", layout="centered")
|
| 192 |
|
| 193 |
+
# Mermaid.js for mind map rendering
|
| 194 |
st.markdown("""
|
| 195 |
+
<script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
|
| 196 |
+
<script>
|
| 197 |
+
mermaid.initialize({ startOnLoad: true });
|
| 198 |
+
</script>
|
| 199 |
""", unsafe_allow_html=True)
|
| 200 |
|
| 201 |
st.markdown("""
|
| 202 |
+
<style>
|
| 203 |
.stApp { background-color: #0f172a; color: white; }
|
| 204 |
h1, h2, h3 { color: #facc15; }
|
| 205 |
+
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
""", unsafe_allow_html=True)
|
| 207 |
|
| 208 |
with st.sidebar:
|
|
|
|
| 211 |
report_type = st.selectbox("π Report Type", ["Summary", "Detailed Report", "Thorough Academic Research"])
|
| 212 |
tone = st.selectbox("π― Tone", ["Objective", "Persuasive", "Narrative"])
|
| 213 |
source_type = st.selectbox("π Sources", ["Web Only", "Academic Only", "Hybrid"])
|
| 214 |
+
custom_domains = st.text_input("π Optional Web Domains", placeholder="example.com, forbes.com")
|
| 215 |
+
research_button = st.button("π Run Deep Research")
|
| 216 |
|
| 217 |
st.title("π Research Output")
|
| 218 |
|
|
|
|
| 224 |
if source_type in ["Web Only", "Hybrid"]:
|
| 225 |
sources += get_sources(topic, custom_domains)
|
| 226 |
if source_type in ["Academic Only", "Hybrid"]:
|
| 227 |
+
sources += get_arxiv_papers(topic)
|
| 228 |
+
sources += get_semantic_papers(topic)
|
|
|
|
| 229 |
return sources
|
| 230 |
|
| 231 |
all_sources, retries = [], 0
|
|
|
|
| 234 |
if all_sources:
|
| 235 |
break
|
| 236 |
retries += 1
|
|
|
|
| 237 |
time.sleep(2)
|
| 238 |
|
| 239 |
if not all_sources:
|
| 240 |
+
raise ValueError("β No sources found.")
|
| 241 |
|
| 242 |
merged = merge_duplicates(all_sources)
|
| 243 |
|
| 244 |
+
# πΉ Image previews
|
|
|
|
| 245 |
st.subheader("πΌ Source Previews")
|
| 246 |
image_shown = False
|
| 247 |
cols = st.columns(2)
|
|
|
|
| 251 |
st.image(m["image_url"], caption=m["title"], use_container_width=True)
|
| 252 |
image_shown = True
|
| 253 |
if not image_shown:
|
| 254 |
+
st.info("βΉοΈ No image previews available.")
|
| 255 |
|
| 256 |
+
# πΉ Generate report
|
| 257 |
citations = [generate_apa_citation(m['title'], m['url'], m['source']) for m in merged]
|
| 258 |
+
combined_text = "\n\n".join([
|
| 259 |
+
f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}..."
|
| 260 |
+
for m in merged
|
| 261 |
+
])
|
|
|
|
| 262 |
|
| 263 |
prompt = f"""
|
| 264 |
You are an expert research assistant.
|
| 265 |
+
1. Analyze the following sources.
|
| 266 |
+
2. Identify research gaps and propose a novel topic.
|
| 267 |
+
3. Write a {report_type.lower()} in a {tone.lower()} tone.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
Sources:
|
| 270 |
{combined_text}
|
|
|
|
| 273 |
{chr(10).join(citations)}
|
| 274 |
"""
|
| 275 |
|
|
|
|
| 276 |
st.subheader(f"π {report_type} on '{topic}'")
|
| 277 |
output_placeholder = st.empty()
|
| 278 |
full_output = ""
|
|
|
|
| 280 |
full_output += chunk
|
| 281 |
output_placeholder.markdown(full_output, unsafe_allow_html=True)
|
| 282 |
|
| 283 |
+
# Save output for follow-up
|
| 284 |
st.session_state["last_report"] = full_output
|
| 285 |
|
| 286 |
+
# πΉ Downloads
|
|
|
|
| 287 |
st.subheader("π Downloads")
|
| 288 |
st.markdown(generate_download_button(generate_pdf(full_output), "Research_Report.pdf", "application/pdf"), unsafe_allow_html=True)
|
| 289 |
st.markdown(generate_download_button(generate_latex(full_output), "Research_Report.tex", "application/x-latex"), unsafe_allow_html=True)
|
| 290 |
|
| 291 |
+
# πΉ Plagiarism Check
|
|
|
|
| 292 |
st.subheader("π Plagiarism Check")
|
| 293 |
overlaps = check_plagiarism(full_output, topic)
|
| 294 |
if overlaps:
|
| 295 |
+
st.warning("β οΈ Overlap Detected:")
|
| 296 |
for hit in overlaps:
|
| 297 |
st.markdown(f"- [{hit['title']}]({hit['url']})")
|
| 298 |
else:
|
| 299 |
+
st.success("β
No major overlaps found.")
|
| 300 |
|
| 301 |
+
# πΉ Mind Map Generator
|
|
|
|
| 302 |
st.subheader("π§ Visual Mind Map")
|
| 303 |
+
if st.button("πΊ Generate Mind Map", key="mindmap_btn"):
|
| 304 |
+
if "last_report" in st.session_state:
|
|
|
|
|
|
|
|
|
|
| 305 |
mindmap_prompt = [
|
| 306 |
+
{"role": "system", "content": "Convert this research report into a mermaid.js mind map."},
|
| 307 |
+
{"role": "user", "content": st.session_state["last_report"]}
|
| 308 |
]
|
| 309 |
mindmap_code = ""
|
| 310 |
for chunk in call_llm(mindmap_prompt):
|
| 311 |
mindmap_code += chunk
|
|
|
|
|
|
|
| 312 |
mindmap_code = mindmap_code.replace("```mermaid", "").replace("```", "").strip()
|
|
|
|
|
|
|
| 313 |
st.markdown(f"<div class='mermaid'>{mindmap_code}</div>", unsafe_allow_html=True)
|
| 314 |
+
else:
|
| 315 |
+
st.error("β No report found. Please run research first.")
|
| 316 |
|
| 317 |
+
# πΉ Follow-up Interaction
|
| 318 |
+
st.subheader("π¬ Ask a Follow-Up")
|
| 319 |
+
follow_input = st.text_input("Ask a question about the report:")
|
|
|
|
|
|
|
| 320 |
if st.button("π Submit Follow-Up"):
|
| 321 |
if "last_report" in st.session_state:
|
| 322 |
+
follow_prompt = [
|
| 323 |
+
{"role": "system", "content": "You are a helpful academic assistant."},
|
| 324 |
+
{"role": "user", "content": st.session_state["last_report"]},
|
| 325 |
+
{"role": "user", "content": follow_input}
|
| 326 |
+
]
|
| 327 |
+
follow_output = ""
|
| 328 |
+
follow_box = st.empty()
|
| 329 |
+
for chunk in call_llm(follow_prompt):
|
| 330 |
+
follow_output += chunk
|
| 331 |
+
follow_box.markdown(follow_output, unsafe_allow_html=True)
|
|
|
|
| 332 |
else:
|
| 333 |
+
st.warning("β οΈ Please generate the report first.")
|
| 334 |
|
| 335 |
except Exception as e:
|
| 336 |
+
st.error(f"β Error occurred: {e}")
|