Spaces:
Sleeping
Sleeping
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 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], | |
) | |
# 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.", | |
) | |
# 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) | |
else: | |
response = client.run(agent=general_agent, messages=messages) | |
return response.messages[-1]["content"] | |
# Create the Gradio interface | |
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() | |