File size: 3,323 Bytes
736beb3
 
3bde37d
736beb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)