Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,74 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
st.
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
with st.sidebar:
|
| 10 |
-
st.header("π§ Research
|
| 11 |
-
|
| 12 |
-
report_type = st.selectbox("π Report Type", [
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|