File size: 2,080 Bytes
854cd25
 
 
 
 
 
 
 
 
 
 
57058ca
854cd25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57058ca
 
 
 
 
 
 
854cd25
 
57058ca
 
 
 
854cd25
57058ca
854cd25
 
57058ca
 
854cd25
49d9ad8
 
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
import gradio as gr
from coordinator_agent import CoordinatorAgent
from dotenv import load_dotenv
from openai.types.responses import ResponseTextDeltaEvent

from agents import Runner

load_dotenv(override=True)

async def run(query: str):
    coordinator_agent = CoordinatorAgent().get_agent()
    result = Runner.run_streamed(coordinator_agent, input=query, max_turns=30)
    response = ""
    status = ""
    async for event in result.stream_events():
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent) and event.data.delta:
            response += event.data.delta
            yield response, status
        elif event.type == "run_item_stream_event":
            item = event.item
            item_type = getattr(item, "type", "")

            if item_type == "tool_call_item":
                tool_name = getattr(item.raw_item, 'name', '')
                log_msg = f"Tool call: {tool_name}\n"
                status += log_msg
                yield response, status

def clear_fields():
    return "", ""

def update_clear_button(query: str):
    return gr.update(interactive=bool(query.strip()))

with gr.Blocks(theme=gr.themes.Glass(primary_hue="fuchsia")) as ui:
    gr.Markdown("# Course Instructor Assistant")
    query_textbox = gr.Textbox(label="What topic would you like to create a course about?", placeholder="e.g. 'How to create your AI double'")
    with gr.Row():
        run_button = gr.Button("Run", variant="primary")
        clear_button = gr.Button("Clear", variant="secondary", interactive=False)
    logs = gr.Textbox(label="Logs", placeholder="Logs will appear here", lines=5)
    report = gr.Markdown(label="Report")

    run_button.click(fn=run, inputs=query_textbox, outputs=[report, logs])
    query_textbox.submit(fn=run, inputs=query_textbox, outputs=[report, logs])
    clear_button.click(fn=clear_fields, outputs=[query_textbox, logs])
    query_textbox.change(fn=update_clear_button, inputs=query_textbox, outputs=clear_button)

if __name__ == "__main__":
    ui.launch(share=True, inbrowser=True)