rahul7star commited on
Commit
fa7c33d
·
verified ·
1 Parent(s): 6e5f40c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -32
app.py CHANGED
@@ -1,59 +1,62 @@
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()
 
1
  import gradio as gr
2
  from swarm import Swarm, Agent
3
 
 
4
  client = Swarm()
5
 
6
+ # Agent functions for task delegation
 
 
 
 
 
 
7
  def transfer_to_spanish_agent():
8
+ """Transfer Spanish-speaking users to the Spanish agent."""
9
  return spanish_agent
10
 
11
+ def transfer_to_english_agent():
12
+ """Transfer English-speaking users to the English agent."""
13
+ return english_agent
 
 
 
14
 
15
+ def verify_order_task():
16
+ """Handle order verification task."""
17
+ return order_verifier_agent
 
 
18
 
19
+ # General agent that identifies the user's language
20
+ general_agent = Agent(
21
+ name="General Agent",
22
+ instructions="Assist the user based on language and delegate tasks if needed.",
23
+ functions=[transfer_to_spanish_agent, transfer_to_english_agent],
24
  )
25
 
26
+ # Spanish-specific agent
27
  spanish_agent = Agent(
28
  name="Spanish Agent",
29
+ instructions="You only speak Spanish. Help with customer support in Spanish.",
30
  )
31
 
32
+ # English-specific agent
33
  english_agent = Agent(
34
  name="English Agent",
35
+ instructions="You only speak English. Help with customer support in English.",
36
  )
37
 
38
+ # Task-specific agent for verifying orders
39
+ order_verifier_agent = Agent(
40
+ name="Order Verifier",
41
+ instructions="Verify customer orders and check their status.",
42
+ )
43
 
44
+ # Creating the Gradio interface function
45
+ def customer_support_demo(user_input):
46
  messages = [{"role": "user", "content": user_input}]
47
+
48
+ # Check for language and delegate to the appropriate agent
49
+ if "hola" in user_input.lower():
50
+ response = client.run(agent=spanish_agent, messages=messages)
51
+ elif "hello" in user_input.lower():
52
+ response = client.run(agent=english_agent, messages=messages)
53
+ else:
54
+ response = client.run(agent=general_agent, messages=messages)
55
+
56
  return response.messages[-1]["content"]
57
 
58
+ # Create the Gradio interface
59
+ iface = gr.Interface(fn=customer_support_demo, inputs="text", outputs="text", live=True)
60
 
61
  # Launch the interface
62
  iface.launch()