Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langgraph.graph import StateGraph, START
|
| 3 |
+
from langgraph.types import Command
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
# Define HuggingFaceEndpoint for urgency classification
|
| 13 |
+
classifier = HuggingFaceEndpoint(
|
| 14 |
+
repo_id="bert-base-uncased", # You can use any model fine-tuned for text classification
|
| 15 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
| 16 |
+
temperature=0.5,
|
| 17 |
+
max_new_tokens=10, # For short classification outputs
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Define HuggingFaceEndpoint for response generation
|
| 21 |
+
llm = HuggingFaceEndpoint(
|
| 22 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 23 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
max_new_tokens=200,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Define state
|
| 29 |
+
class State(dict):
|
| 30 |
+
issue: str
|
| 31 |
+
priority: str
|
| 32 |
+
response: str
|
| 33 |
+
escalation_needed: bool
|
| 34 |
+
|
| 35 |
+
# Create the graph
|
| 36 |
+
builder = StateGraph(State)
|
| 37 |
+
|
| 38 |
+
# Define nodes (agents)
|
| 39 |
+
def ticket_creation_agent(state: State) -> Command[Literal["priority_classification_agent"]]:
|
| 40 |
+
"""Create a ticket based on customer issue."""
|
| 41 |
+
return Command(update={"issue": state["issue"]}, goto="priority_classification_agent")
|
| 42 |
+
|
| 43 |
+
def priority_classification_agent(state: State) -> Command[Literal["response_generation_agent"]]:
|
| 44 |
+
"""Classify ticket priority based on NLP analysis of the issue."""
|
| 45 |
+
issue = state["issue"]
|
| 46 |
+
|
| 47 |
+
# Use the model to classify urgency level
|
| 48 |
+
prompt = f"Classify the urgency of this issue: {issue}"
|
| 49 |
+
priority = classifier(prompt)
|
| 50 |
+
|
| 51 |
+
# Use the classification result to assign priority
|
| 52 |
+
if "urgent" in priority.lower():
|
| 53 |
+
priority = "High"
|
| 54 |
+
elif "critical" in priority.lower():
|
| 55 |
+
priority = "Critical"
|
| 56 |
+
else:
|
| 57 |
+
priority = "Normal"
|
| 58 |
+
|
| 59 |
+
return Command(update={"priority": priority}, goto="response_generation_agent")
|
| 60 |
+
|
| 61 |
+
def response_generation_agent(state: State) -> Command[Literal["escalation_agent"]]:
|
| 62 |
+
"""Generate response based on issue and priority."""
|
| 63 |
+
prompt = f"""
|
| 64 |
+
Customer Issue: {state['issue']}
|
| 65 |
+
Priority: {state['priority']}
|
| 66 |
+
|
| 67 |
+
You are a customer service representative. Provide a detailed response to this issue based on its priority.
|
| 68 |
+
"""
|
| 69 |
+
response = llm(prompt)
|
| 70 |
+
return Command(update={"response": response, "escalation_needed": state['priority'] == "High"}, goto="escalation_agent")
|
| 71 |
+
|
| 72 |
+
def escalation_agent(state: State):
|
| 73 |
+
"""Decide if the ticket needs to be escalated based on priority."""
|
| 74 |
+
escalation = "Yes" if state["escalation_needed"] else "No"
|
| 75 |
+
return {"response": state["response"], "escalation": escalation}
|
| 76 |
+
|
| 77 |
+
# Add nodes to the graph
|
| 78 |
+
builder.add_edge(START, "ticket_creation_agent")
|
| 79 |
+
builder.add_node("ticket_creation_agent", ticket_creation_agent)
|
| 80 |
+
builder.add_node("priority_classification_agent", priority_classification_agent)
|
| 81 |
+
builder.add_node("response_generation_agent", response_generation_agent)
|
| 82 |
+
builder.add_node("escalation_agent", escalation_agent)
|
| 83 |
+
|
| 84 |
+
# Compile the graph
|
| 85 |
+
graph = builder.compile()
|
| 86 |
+
|
| 87 |
+
# Gradio Interface
|
| 88 |
+
def process_ticket(issue: str):
|
| 89 |
+
"""Run the multi-agent customer support flow with user input."""
|
| 90 |
+
state = {"issue": issue}
|
| 91 |
+
result = graph.invoke(state)
|
| 92 |
+
|
| 93 |
+
# Return the customer service response and escalation info
|
| 94 |
+
return result["response"], result["escalation"]
|
| 95 |
+
|
| 96 |
+
iface = gr.Interface(
|
| 97 |
+
fn=process_ticket,
|
| 98 |
+
inputs=[
|
| 99 |
+
gr.Textbox(
|
| 100 |
+
label="Enter Customer Issue",
|
| 101 |
+
placeholder="Describe the issue that needs to be addressed...",
|
| 102 |
+
),
|
| 103 |
+
],
|
| 104 |
+
outputs=[
|
| 105 |
+
gr.Textbox(label="Generated Customer Service Response"),
|
| 106 |
+
gr.Textbox(label="Escalation Status"),
|
| 107 |
+
],
|
| 108 |
+
title="Customer Support Multi-Agent System",
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
iface.launch()
|