File size: 2,274 Bytes
9118743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be0dccf
9118743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
import gradio as gr
from dotenv import load_dotenv
from research_manager import ResearchManager
from email_agent import set_recipient_email, is_valid_email

load_dotenv(override=True)


async def run(query: str, email: str = ""):
    # Set recipient email if provided and valid
    if email and email.strip():
        if is_valid_email(email):
            set_recipient_email(email.strip())
        else:
            set_recipient_email(None)
    else:
        set_recipient_email(None)

    async for chunk in ResearchManager().run(query):
        yield chunk


def clear_fields():
    """Clear input fields only."""
    return "", ""


def check_if_fields_have_input(query: str, email: str):
    """Check if either field has input to enable/disable clear button."""
    has_input = bool(query.strip() or email.strip())
    return gr.update(interactive=has_input)


with gr.Blocks(theme=gr.themes.Glass(primary_hue="indigo")) as ui:
    gr.Markdown("# Deep Research")
    query_textbox = gr.Textbox(
        label="I am your research assistant. What topic would you like to research?",
        placeholder="e.g. 'Posibilily of extraterrestrial life'"
    )
    email_textbox = gr.Textbox(
        label="OPTIONAL: Enter your email if you want to get a copy of the report emailed to you.",
        placeholder="[email protected]"
    )

    with gr.Row():
        run_button = gr.Button("Run", variant="primary")
        clear_button = gr.Button("Clear", interactive=False)

    report = gr.Markdown(label="Report")

    # Run button and submit functionality
    run_button.click(fn=run, inputs=[query_textbox, email_textbox], outputs=report)
    query_textbox.submit(fn=run, inputs=[query_textbox, email_textbox], outputs=report)

    # Clear button functionality
    clear_button.click(
        fn=clear_fields,
        inputs=[],
        outputs=[query_textbox, email_textbox]
    )

    # Enable/disable clear button based on input
    query_textbox.change(
        fn=check_if_fields_have_input,
        inputs=[query_textbox, email_textbox],
        outputs=clear_button
    )
    email_textbox.change(
        fn=check_if_fields_have_input,
        inputs=[query_textbox, email_textbox],
        outputs=clear_button
    )

ui.launch(share=True, inbrowser=True)