Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,61 @@
|
|
| 1 |
# app.py
|
| 2 |
import asyncio
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
from research_manager import ResearchManager
|
| 6 |
|
|
|
|
| 7 |
|
| 8 |
manager = ResearchManager()
|
| 9 |
|
| 10 |
|
| 11 |
-
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
updates = []
|
| 16 |
-
async for update in manager.run(topic):
|
| 17 |
-
updates.append(update)
|
| 18 |
-
yield "\n\n".join(updates)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def run(topic: str):
|
| 22 |
-
"""
|
| 23 |
-
Gradio does not support async generators directly,
|
| 24 |
-
so we bridge async → sync safely.
|
| 25 |
"""
|
| 26 |
loop = asyncio.new_event_loop()
|
| 27 |
asyncio.set_event_loop(loop)
|
| 28 |
|
| 29 |
-
async_gen = run_research(topic)
|
| 30 |
-
|
| 31 |
outputs = []
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
while True:
|
| 34 |
-
|
| 35 |
-
outputs.append(
|
| 36 |
except StopAsyncIteration:
|
| 37 |
pass
|
| 38 |
finally:
|
| 39 |
loop.close()
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
-
with gr.Blocks() as
|
| 45 |
-
gr.Markdown(
|
| 46 |
-
"""
|
| 47 |
-
# 🔍 Deep Research & Email Agent
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
- Performs parallel research
|
| 52 |
-
- Writes a long-form report
|
| 53 |
-
- Sends the report via email
|
| 54 |
-
|
| 55 |
-
⚠️ Email is sent to a fixed recipient configured in the backend.
|
| 56 |
-
"""
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
topic = gr.Textbox(
|
| 60 |
-
label="Research topic",
|
| 61 |
placeholder="e.g. Latest AI agent frameworks in 2025",
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
fn=run,
|
| 73 |
-
inputs=
|
| 74 |
-
outputs=
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
|
|
|
| 1 |
# app.py
|
| 2 |
import asyncio
|
| 3 |
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
from research_manager import ResearchManager
|
| 7 |
|
| 8 |
+
load_dotenv(override=True)
|
| 9 |
|
| 10 |
manager = ResearchManager()
|
| 11 |
|
| 12 |
|
| 13 |
+
def run(query: str):
|
| 14 |
"""
|
| 15 |
+
Hugging Face Spaces requires sync functions.
|
| 16 |
+
This safely consumes your async generator.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
loop = asyncio.new_event_loop()
|
| 19 |
asyncio.set_event_loop(loop)
|
| 20 |
|
|
|
|
|
|
|
| 21 |
outputs = []
|
| 22 |
+
async_gen = manager.run(query)
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
while True:
|
| 26 |
+
chunk = loop.run_until_complete(async_gen.__anext__())
|
| 27 |
+
outputs.append(chunk)
|
| 28 |
except StopAsyncIteration:
|
| 29 |
pass
|
| 30 |
finally:
|
| 31 |
loop.close()
|
| 32 |
|
| 33 |
+
# Return the final combined output
|
| 34 |
+
return "\n\n".join(outputs)
|
| 35 |
|
| 36 |
|
| 37 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as ui:
|
| 38 |
+
gr.Markdown("# 🔍 Deep Research Agent")
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
query_textbox = gr.Textbox(
|
| 41 |
+
label="What topic would you like to research?",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
placeholder="e.g. Latest AI agent frameworks in 2025",
|
| 43 |
)
|
| 44 |
|
| 45 |
+
run_button = gr.Button("Run", variant="primary")
|
| 46 |
|
| 47 |
+
report = gr.Markdown(label="Output")
|
| 48 |
+
|
| 49 |
+
run_button.click(
|
| 50 |
+
fn=run,
|
| 51 |
+
inputs=query_textbox,
|
| 52 |
+
outputs=report,
|
| 53 |
)
|
| 54 |
|
| 55 |
+
query_textbox.submit(
|
| 56 |
fn=run,
|
| 57 |
+
inputs=query_textbox,
|
| 58 |
+
outputs=report,
|
| 59 |
)
|
| 60 |
|
| 61 |
+
ui.launch()
|