Ani14 commited on
Commit
18a5d61
Β·
verified Β·
1 Parent(s): b63f7cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  import streamlit as st
4
  import asyncio
@@ -6,18 +5,20 @@ import nest_asyncio
6
  from gpt_researcher import GPTResearcher
7
  from dotenv import load_dotenv
8
 
 
9
  nest_asyncio.apply()
10
  load_dotenv()
11
 
12
- # Inject Tavily API key
13
  os.environ["TAVILY_API_KEY"] = "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"
14
 
 
15
  st.set_page_config(page_title="🧠 Super Deep Research Agent", layout="wide")
16
  st.title("πŸ“š GPT-Powered Super Deep Research Assistant")
17
 
18
- # --- Sidebar UI ---
19
  with st.sidebar:
20
- st.header("πŸ” Setup Research Agent")
21
  query = st.text_input("πŸ“Œ Research Topic", "Is AI a threat to creative jobs?")
22
  report_type = st.selectbox("πŸ“„ Report Type", ["research_report", "summary", "detailed_report"])
23
  tone = st.selectbox("πŸ—£οΈ Tone", ["objective", "persuasive", "informative"])
@@ -25,7 +26,7 @@ with st.sidebar:
25
  output_format = st.selectbox("πŸ“ Output Format", ["markdown", "text"])
26
  start = st.button("πŸš€ Start Deep Research")
27
 
28
- # Async wrapper
29
  async def run_research_with_logs(query, report_type, source, tone, fmt, log_callback):
30
  agent = GPTResearcher(
31
  query=query,
@@ -42,34 +43,51 @@ async def run_research_with_logs(query, report_type, source, tone, fmt, log_call
42
  images = agent.get_research_images()
43
  return report, context, sources, images
44
 
45
- # --- Main Run ---
46
  if start and query:
47
- st.info("πŸ€– Running super deep research...")
48
-
49
  log_placeholder = st.empty()
50
- log_text = ""
51
 
52
- def stream_log(msg):
53
- nonlocal log_text
54
- log_text += f"🟒 {msg}\n"
55
- log_placeholder.code(log_text, language="text")
56
 
57
- report, context, sources, images = asyncio.run(
58
- run_research_with_logs(query, report_type, source_type, tone, output_format, log_callback=stream_log)
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- st.success("βœ… Report generation complete!")
62
 
 
63
  st.subheader("πŸ“„ Final Report")
64
  st.markdown(report, unsafe_allow_html=True)
65
 
66
- st.subheader("πŸ“š Sources")
67
- for s in sources:
68
- st.markdown(f"- [{s.get('title', 'Untitled')}]({s.get('url', '#')})")
 
 
69
 
 
70
  if images:
71
- st.subheader("πŸ–ΌοΈ Relevant Images")
72
  for img in images:
73
  st.image(img, use_column_width=True)
74
 
 
75
  st.download_button("πŸ’Ύ Download Report", report, file_name="deep_research.md", mime="text/markdown")
 
 
1
  import os
2
  import streamlit as st
3
  import asyncio
 
5
  from gpt_researcher import GPTResearcher
6
  from dotenv import load_dotenv
7
 
8
+ # Apply for async event loop (required for Streamlit + asyncio)
9
  nest_asyncio.apply()
10
  load_dotenv()
11
 
12
+ # Set the Tavily API key (used internally by gpt-researcher)
13
  os.environ["TAVILY_API_KEY"] = "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"
14
 
15
+ # Streamlit UI config
16
  st.set_page_config(page_title="🧠 Super Deep Research Agent", layout="wide")
17
  st.title("πŸ“š GPT-Powered Super Deep Research Assistant")
18
 
19
+ # Sidebar configuration
20
  with st.sidebar:
21
+ st.header("πŸ” Research Configuration")
22
  query = st.text_input("πŸ“Œ Research Topic", "Is AI a threat to creative jobs?")
23
  report_type = st.selectbox("πŸ“„ Report Type", ["research_report", "summary", "detailed_report"])
24
  tone = st.selectbox("πŸ—£οΈ Tone", ["objective", "persuasive", "informative"])
 
26
  output_format = st.selectbox("πŸ“ Output Format", ["markdown", "text"])
27
  start = st.button("πŸš€ Start Deep Research")
28
 
29
+ # Async logic
30
  async def run_research_with_logs(query, report_type, source, tone, fmt, log_callback):
31
  agent = GPTResearcher(
32
  query=query,
 
43
  images = agent.get_research_images()
44
  return report, context, sources, images
45
 
46
+ # Main research execution
47
  if start and query:
48
+ st.info("πŸ€– Running super deep research agent...")
 
49
  log_placeholder = st.empty()
 
50
 
51
+ # Enclosing function to use nonlocal log_text
52
+ def run_loggable_research():
53
+ log_text = ""
 
54
 
55
+ def stream_log(msg):
56
+ nonlocal log_text
57
+ log_text += f"🟒 {msg}\n"
58
+ log_placeholder.code(log_text, language="text")
59
+
60
+ return asyncio.run(
61
+ run_research_with_logs(
62
+ query,
63
+ report_type,
64
+ source_type,
65
+ tone,
66
+ output_format,
67
+ log_callback=stream_log
68
+ )
69
+ )
70
+
71
+ # Run it
72
+ report, context, sources, images = run_loggable_research()
73
 
74
+ st.success("βœ… Research Complete!")
75
 
76
+ # Display report
77
  st.subheader("πŸ“„ Final Report")
78
  st.markdown(report, unsafe_allow_html=True)
79
 
80
+ # Display sources
81
+ if sources:
82
+ st.subheader("πŸ“š Sources")
83
+ for s in sources:
84
+ st.markdown(f"- [{s.get('title', 'Untitled')}]({s.get('url', '#')})")
85
 
86
+ # Display images
87
  if images:
88
+ st.subheader("πŸ–ΌοΈ Related Images")
89
  for img in images:
90
  st.image(img, use_column_width=True)
91
 
92
+ # Download button
93
  st.download_button("πŸ’Ύ Download Report", report, file_name="deep_research.md", mime="text/markdown")