Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from dotenv import load_dotenv | |
| from deep_research_agents.research_manager import ResearchManager | |
| import sendgrid | |
| from sendgrid.helpers.mail import Email, Mail, Content, To | |
| import os | |
| import ssl | |
| ssl._create_default_https_context = ssl._create_unverified_context | |
| load_dotenv(override=True) | |
| # Custom email sender to use the user's email | |
| async def send_email_to_user(subject: str, html_body: str, user_email: str): | |
| sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY')) | |
| from_email = Email("liorsolomon.solid@gmail.com") # put your verified sender here | |
| to_email = To(user_email) | |
| content = Content("text/html", html_body) | |
| mail = Mail(from_email, to_email, subject, content).get() | |
| response = sg.client.mail.send.post(request_body=mail) | |
| print("Email response", response.status_code) | |
| return {"status": "success"} | |
| # Patch the ResearchManager to allow dynamic recipient email | |
| def make_research_manager_with_email(user_email): | |
| class PatchedResearchManager(ResearchManager): | |
| async def send_email(self, report): | |
| subject = "Your Deep Research Report" | |
| html_body = report.markdown_report.replace('\n', '<br>') | |
| await send_email_to_user(subject, html_body, user_email) | |
| return report | |
| return PatchedResearchManager() | |
| async def run_research(query: str, user_email: str): | |
| manager = make_research_manager_with_email(user_email) | |
| yield "<b>π Research started! Please wait while we process your request...</b>" | |
| async for chunk in manager.run(query): | |
| # Show step-by-step progress to the user | |
| if chunk.startswith("View trace: "): | |
| yield f"<i>Trace available: <a href='{chunk[11:]}' target='_blank'>{chunk[11:]}</a></i>" | |
| elif chunk.lower().startswith("searches planned"): | |
| yield "<b>π Searches planned. Starting to search the web...</b>" | |
| elif chunk.lower().startswith("searches complete"): | |
| yield "<b>β Searches complete. Writing the report...</b>" | |
| elif chunk.lower().startswith("report written"): | |
| yield "<b>ποΈ Report written. Sending the report to your email...</b>" | |
| elif chunk.lower().startswith("email sent"): | |
| yield "<b>π§ Email sent! Research complete. See your inbox for the full report.</b>" | |
| elif chunk.strip().startswith('#'): | |
| yield chunk | |
| else: | |
| yield f"<i>{chunk}</i>" | |
| with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as ui: | |
| gr.Markdown("# Deep Research Chatbot\nEnter your email and research topic. You'll receive a detailed report by email!") | |
| with gr.Row(): | |
| email_box = gr.Textbox(label="Your Email", type="text", placeholder="your@email.com") | |
| query_box = gr.Textbox(label="Research Query", lines=3, placeholder="What would you like to research?") | |
| run_btn = gr.Button("Run Deep Research", variant="primary") | |
| report_md = gr.Markdown(label="Report Preview (sent to your email)") | |
| run_btn.click(run_research, inputs=[query_box, email_box], outputs=report_md) | |
| query_box.submit(run_research, inputs=[query_box, email_box], outputs=report_md) | |
| email_box.submit(run_research, inputs=[query_box, email_box], outputs=report_md) | |
| if __name__ == "__main__": | |
| ui.launch(inbrowser=True) | |