Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from swarm import Swarm, Agent
|
| 3 |
+
|
| 4 |
+
# Initialize Swarm client
|
| 5 |
+
client = Swarm()
|
| 6 |
+
|
| 7 |
+
# Define the agent functions
|
| 8 |
+
def transfer_to_agent_b():
|
| 9 |
+
return agent_b
|
| 10 |
+
|
| 11 |
+
def transfer_to_agent_c():
|
| 12 |
+
return agent_c
|
| 13 |
+
|
| 14 |
+
def transfer_to_spanish_agent():
|
| 15 |
+
"""Transfer Spanish speaking users immediately."""
|
| 16 |
+
return spanish_agent
|
| 17 |
+
|
| 18 |
+
# Define the agents
|
| 19 |
+
agent_a = Agent(
|
| 20 |
+
name="Agent A",
|
| 21 |
+
instructions="You are a helpful agent.",
|
| 22 |
+
functions=[transfer_to_agent_b],
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
agent_b = Agent(
|
| 26 |
+
name="Agent B",
|
| 27 |
+
instructions="Only speak in Haikus.",
|
| 28 |
+
functions=[transfer_to_spanish_agent],
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
agent_c = Agent(
|
| 32 |
+
name="Agent C",
|
| 33 |
+
instructions="Welcome to the matrix.",
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
spanish_agent = Agent(
|
| 37 |
+
name="Spanish Agent",
|
| 38 |
+
instructions="You only speak Spanish.",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
english_agent = Agent(
|
| 42 |
+
name="English Agent",
|
| 43 |
+
instructions="You only speak English.",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Append function to transfer to Spanish agent for English agent
|
| 47 |
+
english_agent.functions.append(transfer_to_spanish_agent)
|
| 48 |
+
|
| 49 |
+
# Create the Gradio interface function
|
| 50 |
+
def chat_with_agent(user_input):
|
| 51 |
+
messages = [{"role": "user", "content": user_input}]
|
| 52 |
+
response = client.run(agent=english_agent, messages=messages)
|
| 53 |
+
return response.messages[-1]["content"]
|
| 54 |
+
|
| 55 |
+
# Create Gradio interface
|
| 56 |
+
iface = gr.Interface(fn=chat_with_agent, inputs="text", outputs="text", live=True)
|
| 57 |
+
|
| 58 |
+
# Launch the interface
|
| 59 |
+
iface.launch()
|