Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,20 +17,6 @@ os.environ.setdefault("LLM_PROVIDER", "openai")
|
|
| 17 |
os.environ.setdefault("EMBEDDING_PROVIDER", "openai")
|
| 18 |
os.environ.setdefault("EMBEDDING_MODEL", "text-embedding-3-small")
|
| 19 |
|
| 20 |
-
# ---------- tiny version printer (optional) ----------
|
| 21 |
-
def get_version(pkg, module=None):
|
| 22 |
-
try:
|
| 23 |
-
v = getattr(module, "__version__", None) if module else None
|
| 24 |
-
v = v or importlib.metadata.version(pkg)
|
| 25 |
-
print(f"{pkg} version: {v}")
|
| 26 |
-
except Exception:
|
| 27 |
-
pass
|
| 28 |
-
|
| 29 |
-
get_version("streamlit", st)
|
| 30 |
-
get_version("gpt_researcher")
|
| 31 |
-
get_version("nest_asyncio", nest_asyncio)
|
| 32 |
-
get_version("fpdf")
|
| 33 |
-
|
| 34 |
# ---------- streamlit base ----------
|
| 35 |
st.set_page_config(layout="wide")
|
| 36 |
nest_asyncio.apply()
|
|
@@ -46,7 +32,6 @@ class PDF(FPDF):
|
|
| 46 |
def header(self):
|
| 47 |
self.set_font("Arial", "B", 12)
|
| 48 |
self.cell(0, 10, "Research Report", 0, 1, "C")
|
| 49 |
-
|
| 50 |
def footer(self):
|
| 51 |
self.set_y(-15)
|
| 52 |
self.set_font("Arial", "I", 8)
|
|
@@ -59,11 +44,10 @@ def create_pdf_bytes(report_text: str) -> bytes:
|
|
| 59 |
pdf.set_font("Arial", size=12)
|
| 60 |
for line in report_text.split("\n"):
|
| 61 |
pdf.multi_cell(0, 10, line.encode("latin-1", "replace").decode("latin-1"))
|
| 62 |
-
# dest='S' returns str; encode for bytes
|
| 63 |
return pdf.output(dest="S").encode("latin-1")
|
| 64 |
|
| 65 |
-
# ---------- live research with streaming logs ----------
|
| 66 |
-
async def run_research_streaming(query: str, report_type: str, sources: list, report_source: str, doc_dir: str,
|
| 67 |
buf = io.StringIO()
|
| 68 |
|
| 69 |
# Build researcher
|
|
@@ -73,30 +57,21 @@ async def run_research_streaming(query: str, report_type: str, sources: list, re
|
|
| 73 |
else:
|
| 74 |
researcher = GPTResearcher(query=query, report_type=report_type, source_urls=sources)
|
| 75 |
|
| 76 |
-
#
|
|
|
|
|
|
|
| 77 |
with redirect_stdout(buf), redirect_stderr(buf):
|
| 78 |
task = asyncio.create_task(researcher.conduct_research())
|
| 79 |
|
| 80 |
while not task.done():
|
| 81 |
logs = buf.getvalue()
|
| 82 |
-
|
| 83 |
-
logs_box.text_area(
|
| 84 |
-
"Agent Logs (live)",
|
| 85 |
-
value=logs if logs else "Starting…",
|
| 86 |
-
height=240,
|
| 87 |
-
key="live_logs_box",
|
| 88 |
-
)
|
| 89 |
await asyncio.sleep(1)
|
| 90 |
|
| 91 |
-
# ensure
|
| 92 |
await task
|
| 93 |
final_logs = buf.getvalue()
|
| 94 |
-
|
| 95 |
-
"Agent Logs (live)",
|
| 96 |
-
value=final_logs,
|
| 97 |
-
height=240,
|
| 98 |
-
key="live_logs_box",
|
| 99 |
-
)
|
| 100 |
|
| 101 |
# Now write the report
|
| 102 |
report = await researcher.write_report()
|
|
@@ -106,16 +81,16 @@ async def run_research_streaming(query: str, report_type: str, sources: list, re
|
|
| 106 |
# ---------- UI ----------
|
| 107 |
st.title("GPT Researcher")
|
| 108 |
st.markdown("""
|
| 109 |
-
GPT Researcher is an autonomous agent designed for comprehensive online research tasks. It pulls information from the web or uploaded documents to create detailed, factual
|
| 110 |
""")
|
| 111 |
|
| 112 |
with st.expander("Why Use GPT Researcher?", expanded=False):
|
| 113 |
st.markdown("""
|
| 114 |
-
- **Objective
|
| 115 |
-
- **Time-Efficient**
|
| 116 |
-
- **Up-to-Date**
|
| 117 |
-
- **Comprehensive
|
| 118 |
-
- **Reduced Misinformation**
|
| 119 |
""")
|
| 120 |
|
| 121 |
st.markdown(
|
|
@@ -136,11 +111,11 @@ final_query = f"{user_query} Current Date is {datetime.now().strftime('%B %Y')}"
|
|
| 136 |
st.sidebar.title("Research Settings")
|
| 137 |
with st.sidebar.expander("How to Use", expanded=False):
|
| 138 |
st.markdown("""
|
| 139 |
-
1. **Select Research Type**
|
| 140 |
-
2. **Enter Research Query**.
|
| 141 |
-
3. **Choose Report Type**.
|
| 142 |
-
4. **
|
| 143 |
-
5. **Run Research** — watch live logs
|
| 144 |
""")
|
| 145 |
|
| 146 |
research_type = st.sidebar.selectbox("Select research type:", ["Web Research", "Document Research"])
|
|
@@ -163,11 +138,9 @@ else:
|
|
| 163 |
|
| 164 |
run_clicked = st.sidebar.button("Run Research")
|
| 165 |
|
| 166 |
-
#
|
| 167 |
st.markdown("### Agent Logs")
|
| 168 |
logs_placeholder = st.empty()
|
| 169 |
-
|
| 170 |
-
# output placeholders
|
| 171 |
report_placeholder = st.empty()
|
| 172 |
download_placeholder = st.empty()
|
| 173 |
|
|
@@ -184,11 +157,11 @@ if run_clicked:
|
|
| 184 |
final_query, report_type, sources, src, UPLOAD_DIR, logs_placeholder
|
| 185 |
)
|
| 186 |
)
|
| 187 |
-
#
|
| 188 |
st.session_state.report = report
|
| 189 |
st.session_state.logs = logs
|
| 190 |
|
| 191 |
-
#
|
| 192 |
if "report" in st.session_state:
|
| 193 |
report_placeholder.markdown("### Research Report")
|
| 194 |
report_placeholder.markdown(st.session_state.report)
|
|
@@ -200,17 +173,11 @@ if "report" in st.session_state:
|
|
| 200 |
data=pdf_bytes,
|
| 201 |
file_name=f"report_{timestamp}.pdf",
|
| 202 |
mime="application/pdf",
|
| 203 |
-
key="dl_pdf_btn",
|
| 204 |
)
|
| 205 |
|
| 206 |
-
#
|
| 207 |
if "logs" in st.session_state:
|
| 208 |
-
logs_placeholder.
|
| 209 |
-
"Agent Logs (live)",
|
| 210 |
-
value=st.session_state.logs,
|
| 211 |
-
height=240,
|
| 212 |
-
key="live_logs_box",
|
| 213 |
-
)
|
| 214 |
|
| 215 |
# Hide Streamlit chrome
|
| 216 |
st.markdown("""
|
|
|
|
| 17 |
os.environ.setdefault("EMBEDDING_PROVIDER", "openai")
|
| 18 |
os.environ.setdefault("EMBEDDING_MODEL", "text-embedding-3-small")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# ---------- streamlit base ----------
|
| 21 |
st.set_page_config(layout="wide")
|
| 22 |
nest_asyncio.apply()
|
|
|
|
| 32 |
def header(self):
|
| 33 |
self.set_font("Arial", "B", 12)
|
| 34 |
self.cell(0, 10, "Research Report", 0, 1, "C")
|
|
|
|
| 35 |
def footer(self):
|
| 36 |
self.set_y(-15)
|
| 37 |
self.set_font("Arial", "I", 8)
|
|
|
|
| 44 |
pdf.set_font("Arial", size=12)
|
| 45 |
for line in report_text.split("\n"):
|
| 46 |
pdf.multi_cell(0, 10, line.encode("latin-1", "replace").decode("latin-1"))
|
|
|
|
| 47 |
return pdf.output(dest="S").encode("latin-1")
|
| 48 |
|
| 49 |
+
# ---------- live research with streaming logs (single placeholder, no keys) ----------
|
| 50 |
+
async def run_research_streaming(query: str, report_type: str, sources: list, report_source: str, doc_dir: str, logs_placeholder):
|
| 51 |
buf = io.StringIO()
|
| 52 |
|
| 53 |
# Build researcher
|
|
|
|
| 57 |
else:
|
| 58 |
researcher = GPTResearcher(query=query, report_type=report_type, source_urls=sources)
|
| 59 |
|
| 60 |
+
# Create an initial visible block for logs (single widget, then we just overwrite it)
|
| 61 |
+
logs_placeholder.code("Starting…")
|
| 62 |
+
|
| 63 |
with redirect_stdout(buf), redirect_stderr(buf):
|
| 64 |
task = asyncio.create_task(researcher.conduct_research())
|
| 65 |
|
| 66 |
while not task.done():
|
| 67 |
logs = buf.getvalue()
|
| 68 |
+
logs_placeholder.code(logs if logs else "Starting…")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
await asyncio.sleep(1)
|
| 70 |
|
| 71 |
+
# ensure final prints are shown
|
| 72 |
await task
|
| 73 |
final_logs = buf.getvalue()
|
| 74 |
+
logs_placeholder.code(final_logs if final_logs else "Done.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Now write the report
|
| 77 |
report = await researcher.write_report()
|
|
|
|
| 81 |
# ---------- UI ----------
|
| 82 |
st.title("GPT Researcher")
|
| 83 |
st.markdown("""
|
| 84 |
+
GPT Researcher is an autonomous agent designed for comprehensive online research tasks. It pulls information from the web or uploaded documents to create detailed, factual research reports.
|
| 85 |
""")
|
| 86 |
|
| 87 |
with st.expander("Why Use GPT Researcher?", expanded=False):
|
| 88 |
st.markdown("""
|
| 89 |
+
- **Objective & Unbiased**
|
| 90 |
+
- **Time-Efficient**
|
| 91 |
+
- **Up-to-Date**
|
| 92 |
+
- **Comprehensive (2,000+ words)**
|
| 93 |
+
- **Reduced Misinformation**
|
| 94 |
""")
|
| 95 |
|
| 96 |
st.markdown(
|
|
|
|
| 111 |
st.sidebar.title("Research Settings")
|
| 112 |
with st.sidebar.expander("How to Use", expanded=False):
|
| 113 |
st.markdown("""
|
| 114 |
+
1. **Select Research Type** (Web/Document).
|
| 115 |
+
2. **Enter Research Query**.
|
| 116 |
+
3. **Choose Report Type**.
|
| 117 |
+
4. **Add URLs or Upload Files**.
|
| 118 |
+
5. **Run Research** — watch live logs, then download the PDF.
|
| 119 |
""")
|
| 120 |
|
| 121 |
research_type = st.sidebar.selectbox("Select research type:", ["Web Research", "Document Research"])
|
|
|
|
| 138 |
|
| 139 |
run_clicked = st.sidebar.button("Run Research")
|
| 140 |
|
| 141 |
+
# stable placeholders
|
| 142 |
st.markdown("### Agent Logs")
|
| 143 |
logs_placeholder = st.empty()
|
|
|
|
|
|
|
| 144 |
report_placeholder = st.empty()
|
| 145 |
download_placeholder = st.empty()
|
| 146 |
|
|
|
|
| 157 |
final_query, report_type, sources, src, UPLOAD_DIR, logs_placeholder
|
| 158 |
)
|
| 159 |
)
|
| 160 |
+
# persist
|
| 161 |
st.session_state.report = report
|
| 162 |
st.session_state.logs = logs
|
| 163 |
|
| 164 |
+
# Render results if available (e.g., after rerun)
|
| 165 |
if "report" in st.session_state:
|
| 166 |
report_placeholder.markdown("### Research Report")
|
| 167 |
report_placeholder.markdown(st.session_state.report)
|
|
|
|
| 173 |
data=pdf_bytes,
|
| 174 |
file_name=f"report_{timestamp}.pdf",
|
| 175 |
mime="application/pdf",
|
|
|
|
| 176 |
)
|
| 177 |
|
| 178 |
+
# Keep last logs visible after run / rerun
|
| 179 |
if "logs" in st.session_state:
|
| 180 |
+
logs_placeholder.code(st.session_state.logs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
# Hide Streamlit chrome
|
| 183 |
st.markdown("""
|