MultiAgent / app.py
rahul7star's picture
Update app.py
d1d5a30 verified
import gradio as gr
from swarm import Swarm, Agent
client = Swarm()
# Agent functions for task delegation
def transfer_to_spanish_agent():
"""Transfer Spanish-speaking users to the Spanish agent."""
return spanish_agent
def transfer_to_english_agent():
"""Transfer English-speaking users to the English agent."""
return english_agent
def transfer_to_hindi_agent():
"""Transfer Hindi-speaking users to the Hindi agent."""
return hindi_agent
def transfer_to_filipino_agent():
"""Transfer Filipino-speaking users to the Filipino agent."""
return filipino_agent
def verify_order_task():
"""Handle order verification task."""
return order_verifier_agent
# General agent that identifies the user's language
general_agent = Agent(
name="General Agent",
instructions="Assist the user based on language and delegate tasks if needed.",
functions=[transfer_to_spanish_agent, transfer_to_english_agent, transfer_to_hindi_agent, transfer_to_filipino_agent],
)
# Spanish-specific agent
spanish_agent = Agent(
name="Spanish Agent",
instructions="You only speak Spanish. Help with customer support in Spanish.",
)
# English-specific agent
english_agent = Agent(
name="English Agent",
instructions="You only speak English. Help with customer support in English.",
)
# Hindi-specific agent
hindi_agent = Agent(
name="Hindi Agent",
instructions="You only speak Hindi. Help with customer support in Hindi.",
)
# Filipino-specific agent
filipino_agent = Agent(
name="Filipino Agent",
instructions="You only speak Filipino/Tagalog. Help with customer support in Filipino.",
)
# Task-specific agent for verifying orders
order_verifier_agent = Agent(
name="Order Verifier",
instructions="Verify customer orders and check their status.",
)
# Creating the Gradio interface function
def customer_support_demo(user_input):
messages = [{"role": "user", "content": user_input}]
# Check for language and delegate to the appropriate agent
if "hola" in user_input.lower():
response = client.run(agent=spanish_agent, messages=messages)
elif "hello" in user_input.lower():
response = client.run(agent=english_agent, messages=messages)
elif "नमस्ते" in user_input.lower():
response = client.run(agent=hindi_agent, messages=messages)
elif "kamusta" in user_input.lower() or "magandang araw" in user_input.lower():
response = client.run(agent=filipino_agent, messages=messages)
else:
response = client.run(agent=general_agent, messages=messages)
return response.messages[-1]["content"]
# Create the Gradio interface with a label and description
iface = gr.Interface(
fn=customer_support_demo,
inputs="text",
outputs="text",
live=True,
title="Global Customer Support System",
description="This is a multi-agent system designed to provide global customer support in multiple languages. It detects the language of your input and directs you to the appropriate agent. If needed, the agents can delegate tasks to specialized agents, such as order verification.",
)
# Launch the interface
iface.launch()