File size: 3,511 Bytes
037933d
 
 
dd4a1b4
 
 
 
 
 
 
 
037933d
59b6314
 
 
037933d
0b9c746
31faf14
037933d
0b9c746
037933d
5cfd79f
0b9c746
e08dea8
0b9c746
cf6a0bc
 
 
 
 
59b6314
 
5cfd79f
0b9c746
59b6314
 
037933d
59b6314
e08dea8
c2ebec8
0b9c746
 
 
 
 
dd4a1b4
0b9c746
31faf14
 
 
 
87e4a3f
 
 
 
 
037933d
59b6314
dd4a1b4
037933d
59b6314
 
 
cf6a0bc
c2ebec8
59b6314
e08dea8
59b6314
 
 
c2ebec8
59b6314
 
 
0b9c746
87e4a3f
 
 
31faf14
59b6314
0b9c746
87e4a3f
 
31faf14
 
87e4a3f
 
 
31faf14
59b6314
0b9c746
87e4a3f
 
e08dea8
037933d
87e4a3f
037933d
0b9c746
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import gradio as gr
import os
import sys
import threading

print("--- Desk Agent Starting Up ---")
print("This application requires the following Hugging Face Space secrets to be set:")
print("- JULES_API_KEY")
print("- HF_TOKEN")
print("- BLABLADOR_API_KEY")
print("-----------------------------")

# Ensure the multi_agent_system is in the Python path
sys.path.append(os.getcwd())
from multi_agent_system.orchestrator.agent import main_orchestration_workflow

# --- Streaming UI Updates ---
def stream_orchestrator(msg, history, jules_key, hf_token, blablador_key):
    """
    A generator that handles streaming UI updates from the orchestrator workflow.
    """
    history.append([msg, ""])
    yield history

    # Set API keys from UI or environment variables
    api_keys = {
        "JULES_API_KEY": os.environ.get("JULES_API_KEY", jules_key),
        "HF_TOKEN": os.environ.get("HF_TOKEN", hf_token),
        "BLABLADOR_API_KEY": os.environ.get("BLABLADOR_API_KEY", blablador_key),
    }
    for key, value in api_keys.items():
        if not value:
            history[-1][1] = f"Error: {key} is not set. Please provide it as a Space secret or in the UI."
            yield history
            return
        os.environ[key] = value

    workflow_generator = main_orchestration_workflow()
    final_bot_message = ""

    # Stream updates from the workflow
    for update in workflow_generator:
        final_bot_message += update
        history[-1][1] = final_bot_message
        yield history

    # Clean up environment variables
    for key in api_keys:
        if key in os.environ:
            del os.environ[key]

def disable_ui():
    return gr.Textbox(interactive=False), gr.Button(interactive=False)

def enable_ui():
    return gr.Textbox(interactive=True, placeholder="Workflow complete. You can start a new one."), gr.Button(interactive=True)

# --- Build the Gradio UI ---
with gr.Blocks(title="Desk Agent") as demo:
    gr.Markdown("# Desk Agent Orchestrator")
    gr.Markdown("A conversational interface to a multi-agent workflow system. Press 'Start Workflow' to begin.")

    chatbot = gr.Chatbot(label="Agent Conversation", height=500)
    msg_input = gr.Textbox(label="Your Message", placeholder="Press 'Start Workflow' to begin...")

    with gr.Accordion("API Key Overrides (Optional)", open=False):
        gr.Markdown("API keys will be automatically loaded from Space secrets if available.")
        jules_key_input = gr.Textbox(label="Jules API Key", type="password")
        hf_token_input = gr.Textbox(label="Hugging Face Token", type="password")
        blablador_key_input = gr.Textbox(label="Blablador API Key", type="password")

    start_button = gr.Button("Start Workflow", variant="primary")
    clear_button = gr.ClearButton([msg_input, chatbot])

    # --- Event Handling ---
    start_button.click(
        fn=disable_ui, outputs=[msg_input, start_button]
    ).then(
        fn=stream_orchestrator,
        inputs=[msg_input, chatbot, jules_key_input, hf_token_input, blablador_key_input],
        outputs=[chatbot]
    ).then(
        fn=enable_ui, outputs=[msg_input, start_button]
    )

    msg_input.submit(
        fn=disable_ui, outputs=[msg_input, start_button]
    ).then(
        fn=stream_orchestrator,
        inputs=[msg_input, chatbot, jules_key_input, hf_token_input, blablador_key_input],
        outputs=[chatbot]
    ).then(
        fn=enable_ui, outputs=[msg_input, start_button]
    )


if __name__ == "__main__":
    demo.launch(share=True)