Ani14 commited on
Commit
f138bf1
Β·
verified Β·
1 Parent(s): 1b45e14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -32
app.py CHANGED
@@ -1,36 +1,74 @@
1
-
2
  import streamlit as st
3
- from gpt_researcher.agent import GPTResearcher
 
 
 
 
 
4
 
5
- st.set_page_config(page_title="GPT Researcher UI", layout="wide")
6
- st.title("πŸ€– GPT Researcher β€” Streamlit UI")
 
7
 
8
- # --- Sidebar inputs ---
9
  with st.sidebar:
10
- st.header("🧠 Research Configuration")
11
- topic = st.text_input("πŸ’‘ Research Topic", "AI in climate change")
12
- report_type = st.selectbox("πŸ“„ Report Type", ["summary", "detailed", "academic"])
13
- report_format = st.selectbox("πŸ“œ Format", ["markdown", "text"])
14
- report_source = st.selectbox("🌐 Sources", ["web", "arxiv", "semantic-scholar", "hybrid"])
15
- tone = st.selectbox("🎯 Tone", ["objective", "persuasive", "informative"])
16
- start = st.button("πŸš€ Start Research")
17
-
18
- # --- Run GPTResearcher ---
19
- if start and topic:
20
- st.markdown("### ⏳ Running Autonomous Research Agent...")
21
- with st.spinner("Gathering knowledge, synthesizing insights..."):
22
- agent = GPTResearcher(
23
- query=topic,
24
- report_type=report_type,
25
- report_format=report_format,
26
- report_source=report_source,
27
- tone=tone
28
- )
29
- output = agent.run()
30
-
31
- st.success("βœ… Research Complete!")
32
-
33
- st.markdown("### πŸ“„ Final Report")
34
- st.markdown(output, unsafe_allow_html=True)
35
-
36
- st.download_button("πŸ’Ύ Download Markdown", output, file_name="report.md", mime="text/markdown")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import streamlit as st
3
+ import asyncio
4
+ import nest_asyncio
5
+ from gpt_researcher import GPTResearcher
6
+
7
+ # Allow async in Streamlit (important for notebooks + event loops)
8
+ nest_asyncio.apply()
9
 
10
+ # Streamlit page config
11
+ st.set_page_config(page_title="GPT Researcher Async UI", layout="wide")
12
+ st.title("πŸ“˜ GPT Researcher (Async Streamlit Interface)")
13
 
14
+ # Sidebar Inputs
15
  with st.sidebar:
16
+ st.header("🧠 Research Agent Setup")
17
+ query = st.text_input("πŸ” Research Topic", "Should I invest in Nvidia?")
18
+ report_type = st.selectbox("πŸ“„ Report Type", [
19
+ "research_report", "summary", "detailed_report"
20
+ ])
21
+ run_button = st.button("πŸš€ Run Research Agent")
22
+
23
+ # Async wrapper function
24
+ async def run_gpt_researcher(query: str, report_type: str):
25
+ researcher = GPTResearcher(query, report_type)
26
+
27
+ # Phase 1: Planning + Research
28
+ await researcher.conduct_research()
29
+
30
+ # Phase 2: Writing
31
+ report = await researcher.write_report()
32
+
33
+ # Additional context
34
+ context = researcher.get_research_context()
35
+ costs = researcher.get_costs()
36
+ images = researcher.get_research_images()
37
+ sources = researcher.get_research_sources()
38
+
39
+ return report, context, costs, images, sources
40
+
41
+ # Main Execution
42
+ if run_button and query:
43
+ st.info("πŸ€– Autonomous agent is researching... Please wait.")
44
+
45
+ # Run async function in Streamlit
46
+ report, context, costs, images, sources = asyncio.run(run_gpt_researcher(query, report_type))
47
+
48
+ st.success("βœ… Research completed!")
49
+
50
+ # Show final report
51
+ st.subheader("πŸ“„ Final Report")
52
+ st.markdown(report, unsafe_allow_html=True)
53
+
54
+ # Show sources
55
+ if sources:
56
+ st.subheader("πŸ“š Sources Used")
57
+ for src in sources:
58
+ title = src.get("title", "Untitled")
59
+ url = src.get("url", "#")
60
+ st.markdown(f"- [{title}]({url})")
61
+
62
+ # Show research cost
63
+ if costs:
64
+ st.subheader("πŸ’° Cost Breakdown")
65
+ st.json(costs)
66
+
67
+ # Show related images
68
+ if images:
69
+ st.subheader("πŸ–ΌοΈ Related Images")
70
+ for img in images:
71
+ st.image(img, use_column_width=True)
72
+
73
+ # Download button for the report
74
+ st.download_button("πŸ’Ύ Download Report", data=report, file_name="report.md", mime="text/markdown")