Ani14 commited on
Commit
a9f6a2b
Β·
verified Β·
1 Parent(s): c12e13c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -78
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 Bot", layout="centered")
192
 
193
- # Inject 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
- .css-1d391kg, .css-1kyxreq, .css-q8sbsg {
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, techcrunch.com")
219
- research_button = st.button("πŸ”Ž Run Deep Research")
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
- status.update(label="πŸ“š Fetching academic sources...")
232
- sources += get_arxiv_papers(topic) or []
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("Unable to fetch any sources. Please try again later.")
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 from sources.")
262
 
 
263
  citations = [generate_apa_citation(m['title'], m['url'], m['source']) for m in merged]
264
- combined_text = "\n\n".join(
265
- [f"- [{m['title']}]({m['url']})\n> {m.get('snippet', m.get('summary', ''))[:300]}..." for m in merged]
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 for gaps or limitations in current research.
273
- 2. Propose a novel research topic that fills the most promising gap.
274
- 3. Write a full academic-style report on the newly proposed topic using a {tone.lower()} tone.
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
- # Store report for mind map and follow-up
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("⚠️ Overlaps found:")
315
  for hit in overlaps:
316
  st.markdown(f"- [{hit['title']}]({hit['url']})")
317
  else:
318
- st.success("βœ… No significant overlaps found.")
319
 
320
- # Mind Map Generator (Visual)
321
- st.markdown("---")
322
  st.subheader("🧭 Visual Mind Map")
323
-
324
- if "last_report" not in st.session_state or not st.session_state["last_report"].strip():
325
- st.warning("⚠️ Please generate a report first.")
326
- else:
327
- if st.button("πŸ—Ί Generate Mind Map"):
328
  mindmap_prompt = [
329
- {"role": "system", "content": "You are a researcher who converts reports into mermaid mind maps."},
330
- {"role": "user", "content": f"Create a mermaid.js mind map of the following:\n\n{st.session_state['last_report']}"}
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
- # Follow-up Q&A
344
- st.markdown("---")
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
- with st.spinner("Thinking..."):
350
- follow_prompt = [
351
- {"role": "system", "content": "You are a helpful research assistant."},
352
- {"role": "user", "content": f"Here is the report:\n{st.session_state['last_report']}"},
353
- {"role": "user", "content": follow_input}
354
- ]
355
- follow_output = ""
356
- follow_placeholder = st.empty()
357
- for chunk in call_llm(follow_prompt):
358
- follow_output += chunk
359
- follow_placeholder.markdown(follow_output, unsafe_allow_html=True)
360
  else:
361
- st.error("⚠️ Report not found. Please generate a report first.")
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}")