Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,38 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import asyncio
|
| 4 |
import nest_asyncio
|
| 5 |
-
import datetime
|
| 6 |
-
import tempfile
|
| 7 |
-
import base64
|
| 8 |
from gpt_researcher import GPTResearcher
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
|
|
|
| 11 |
nest_asyncio.apply()
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
os.environ["TAVILY_API_KEY"] = "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"
|
| 16 |
|
|
|
|
| 17 |
st.set_page_config(page_title="π§ Super Deep Research Agent", layout="wide")
|
| 18 |
st.title("π GPT-Powered Super Deep Research Assistant")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
with st.sidebar:
|
| 22 |
-
st.header("π
|
| 23 |
query = st.text_input("π Research Topic", "Is AI a threat to creative jobs?")
|
| 24 |
report_type = st.selectbox("π Report Type", ["research_report", "summary", "detailed_report"])
|
| 25 |
tone = st.selectbox("π£οΈ Tone", ["objective", "persuasive", "informative"])
|
| 26 |
-
source_type = st.selectbox("
|
| 27 |
output_format = st.selectbox("π Output Format", ["markdown", "text"])
|
| 28 |
-
|
| 29 |
-
start = st.button("π Start Deep Research")
|
| 30 |
|
| 31 |
-
# Async
|
| 32 |
-
async def
|
| 33 |
agent = GPTResearcher(
|
| 34 |
query=query,
|
| 35 |
report_type=report_type,
|
| 36 |
report_source=source,
|
| 37 |
report_format=fmt,
|
| 38 |
-
tone=tone
|
| 39 |
-
log_fn=log_callback
|
| 40 |
)
|
| 41 |
await agent.conduct_research()
|
| 42 |
report = await agent.write_report()
|
|
@@ -45,67 +42,37 @@ async def run_research_with_logs(query, report_type, source, tone, fmt, log_call
|
|
| 45 |
images = agent.get_research_images()
|
| 46 |
return report, context, sources, images
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
def export_file(content, export_format):
|
| 50 |
-
filename_base = f"deep_research_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
| 51 |
-
|
| 52 |
-
if export_format == "Markdown":
|
| 53 |
-
return content, f"{filename_base}.md", "text/markdown"
|
| 54 |
-
|
| 55 |
-
elif export_format == "LaTeX":
|
| 56 |
-
latex_content = f"\\documentclass{{article}}\n\\begin{{document}}\n{content}\n\\end{{document}}"
|
| 57 |
-
return latex_content, f"{filename_base}.tex", "application/x-tex"
|
| 58 |
-
|
| 59 |
-
elif export_format == "PDF":
|
| 60 |
-
try:
|
| 61 |
-
from fpdf import FPDF
|
| 62 |
-
except ImportError:
|
| 63 |
-
st.error("Please install `fpdf` to export PDF: `pip install fpdf`")
|
| 64 |
-
return None, None, None
|
| 65 |
-
|
| 66 |
-
pdf = FPDF()
|
| 67 |
-
pdf.add_page()
|
| 68 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
| 69 |
-
pdf.set_font("Arial", size=12)
|
| 70 |
-
for line in content.split('\n'):
|
| 71 |
-
pdf.multi_cell(0, 10, line)
|
| 72 |
-
temp_path = tempfile.mktemp(suffix=".pdf")
|
| 73 |
-
pdf.output(temp_path)
|
| 74 |
-
with open(temp_path, "rb") as f:
|
| 75 |
-
return f.read(), f"{filename_base}.pdf", "application/pdf"
|
| 76 |
-
|
| 77 |
-
return None, None, None
|
| 78 |
-
|
| 79 |
-
# --- Main Run ---
|
| 80 |
if start and query:
|
| 81 |
-
st.info("
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
report, context, sources, images = asyncio.run(
|
| 91 |
-
run_research_with_logs(query, report_type, source_type, tone, output_format, log_callback=stream_log)
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
st.success("β
|
| 95 |
|
|
|
|
| 96 |
st.subheader("π Final Report")
|
| 97 |
st.markdown(report, unsafe_allow_html=True)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.
|
|
|
|
|
|
|
| 102 |
|
|
|
|
| 103 |
if images:
|
| 104 |
st.subheader("πΌοΈ Relevant Images")
|
| 105 |
for img in images:
|
| 106 |
st.image(img, use_column_width=True)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
| 110 |
-
if file_data and filename:
|
| 111 |
-
st.download_button(f"πΎ Download as {export_format}", data=file_data, file_name=filename, mime=mime_type)
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import asyncio
|
| 4 |
import nest_asyncio
|
|
|
|
|
|
|
|
|
|
| 5 |
from gpt_researcher import GPTResearcher
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
+
# Enable async for Streamlit
|
| 9 |
nest_asyncio.apply()
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
+
# Set your Tavily API key
|
| 13 |
os.environ["TAVILY_API_KEY"] = "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"
|
| 14 |
|
| 15 |
+
# App UI setup
|
| 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 UI
|
| 20 |
with st.sidebar:
|
| 21 |
+
st.header("π Research Setup")
|
| 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"])
|
| 25 |
+
source_type = st.selectbox("π Source Scope", ["web", "arxiv", "semantic-scholar", "hybrid"])
|
| 26 |
output_format = st.selectbox("π Output Format", ["markdown", "text"])
|
| 27 |
+
start = st.button("π Start Research")
|
|
|
|
| 28 |
|
| 29 |
+
# Async agent runner
|
| 30 |
+
async def run_research(query, report_type, source, tone, fmt):
|
| 31 |
agent = GPTResearcher(
|
| 32 |
query=query,
|
| 33 |
report_type=report_type,
|
| 34 |
report_source=source,
|
| 35 |
report_format=fmt,
|
| 36 |
+
tone=tone
|
|
|
|
| 37 |
)
|
| 38 |
await agent.conduct_research()
|
| 39 |
report = await agent.write_report()
|
|
|
|
| 42 |
images = agent.get_research_images()
|
| 43 |
return report, context, sources, images
|
| 44 |
|
| 45 |
+
# Run on click
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if start and query:
|
| 47 |
+
st.info("β³ Running research agent...")
|
| 48 |
|
| 49 |
+
# Spinner with placeholder log
|
| 50 |
+
with st.spinner("Thinking..."):
|
| 51 |
+
# Optional: log collector using mutable container (if future logging is needed)
|
| 52 |
+
logs = []
|
| 53 |
|
| 54 |
+
# Run agent
|
| 55 |
+
report, context, sources, images = asyncio.run(
|
| 56 |
+
run_research(query, report_type, source_type, tone, output_format)
|
| 57 |
+
)
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
st.success("β
Research Completed!")
|
| 60 |
|
| 61 |
+
# Display report
|
| 62 |
st.subheader("π Final Report")
|
| 63 |
st.markdown(report, unsafe_allow_html=True)
|
| 64 |
|
| 65 |
+
# Display sources
|
| 66 |
+
if sources:
|
| 67 |
+
st.subheader("π Sources")
|
| 68 |
+
for s in sources:
|
| 69 |
+
st.markdown(f"- [{s.get('title', 'Untitled')}]({s.get('url', '#')})")
|
| 70 |
|
| 71 |
+
# Display images
|
| 72 |
if images:
|
| 73 |
st.subheader("πΌοΈ Relevant Images")
|
| 74 |
for img in images:
|
| 75 |
st.image(img, use_column_width=True)
|
| 76 |
|
| 77 |
+
# Download report
|
| 78 |
+
st.download_button("πΎ Download Markdown", report, file_name="deep_research.md", mime="text/markdown")
|
|
|
|
|
|