Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
# Import the new deep research logic
|
| 7 |
+
from research_agent import deep_research, ResearchReport
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
async def interact(user_message, history):
|
| 13 |
+
"""
|
| 14 |
+
Handle user interaction for Deep Research.
|
| 15 |
+
"""
|
| 16 |
+
if not user_message:
|
| 17 |
+
yield history, ""
|
| 18 |
+
return
|
| 19 |
+
|
| 20 |
+
# Append user message to Gradio history
|
| 21 |
+
history = history or []
|
| 22 |
+
history.append({"role": "user", "content": user_message})
|
| 23 |
+
|
| 24 |
+
# Yield initial state
|
| 25 |
+
yield history, ""
|
| 26 |
+
|
| 27 |
+
# Call the deep_research generator
|
| 28 |
+
# It yields status strings (progress updates) and finally a ResearchReport object
|
| 29 |
+
async for update in deep_research(user_message):
|
| 30 |
+
if isinstance(update, str):
|
| 31 |
+
# It's a status update
|
| 32 |
+
# We can show this as a temporary system message or just log it
|
| 33 |
+
# For a chat interface, we can append a system message that updates
|
| 34 |
+
if history[-1]["role"] != "assistant":
|
| 35 |
+
history.append({"role": "assistant", "content": update})
|
| 36 |
+
else:
|
| 37 |
+
history[-1]["content"] = update
|
| 38 |
+
yield history, ""
|
| 39 |
+
|
| 40 |
+
elif isinstance(update, ResearchReport):
|
| 41 |
+
# Final report
|
| 42 |
+
report_md = f"# Research Report: {user_message}\n\n"
|
| 43 |
+
report_md += f"## Executive Summary\n{update.executive_summary}\n\n"
|
| 44 |
+
|
| 45 |
+
for section in update.sections:
|
| 46 |
+
report_md += f"### {section.title}\n{section.content}\n\n"
|
| 47 |
+
if section.sources:
|
| 48 |
+
report_md += "**Sources:**\n" + "\n".join([f"- {s}" for s in section.sources]) + "\n\n"
|
| 49 |
+
|
| 50 |
+
report_md += f"## Risks & Uncertainties\n{update.risks_uncertainties}\n\n"
|
| 51 |
+
report_md += "## What to Watch Next\n" + "\n".join([f"- {item}" for item in update.what_to_watch_next])
|
| 52 |
+
|
| 53 |
+
# Replace the last status message with the final report
|
| 54 |
+
history[-1]["content"] = report_md
|
| 55 |
+
yield history, ""
|
| 56 |
+
|
| 57 |
+
# Create the Gradio Interface
|
| 58 |
+
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("# Pydantic AI Deep Research Agent")
|
| 60 |
+
gr.Markdown("Enter a stock ticker (e.g., NVDA) or a research topic to generate a detailed report.")
|
| 61 |
+
|
| 62 |
+
chatbot = gr.Chatbot(label="Agent", height=700)
|
| 63 |
+
msg = gr.Textbox(placeholder="Enter ticker or topic...", label="Research Query")
|
| 64 |
+
|
| 65 |
+
# Submit handler
|
| 66 |
+
msg.submit(
|
| 67 |
+
interact,
|
| 68 |
+
inputs=[msg, chatbot],
|
| 69 |
+
outputs=[chatbot, msg]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch()
|