Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# -
|
| 5 |
-
# Step 1: Load Zero-Shot Classification Model
|
| 6 |
-
# -----------------------------
|
| 7 |
-
# Load the facebook/bart-large-mnli model once at startup
|
| 8 |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# Step 2: Define Complaint Categories
|
| 12 |
-
# -----------------------------
|
| 13 |
categories = [
|
| 14 |
"Network Issue",
|
| 15 |
"Billing Issue",
|
|
@@ -27,56 +26,39 @@ routing_teams = {
|
|
| 27 |
"Device Issue": "Technical Support"
|
| 28 |
}
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
# Step 3: Define Prediction Function
|
| 32 |
-
# -----------------------------
|
| 33 |
def classify_complaint(complaint_text):
|
| 34 |
-
"""
|
| 35 |
-
Classify the customer complaint into a category and suggest routing team.
|
| 36 |
-
"""
|
| 37 |
if not complaint_text.strip():
|
| 38 |
-
return "No input provided", 0.0, "N/A"
|
| 39 |
-
|
| 40 |
-
# Use Hugging Face zero-shot classifier
|
| 41 |
result = classifier(complaint_text, candidate_labels=categories)
|
| 42 |
-
|
| 43 |
-
# Get the top category and its confidence score
|
| 44 |
top_category = result['labels'][0]
|
| 45 |
confidence_score = result['scores'][0]
|
| 46 |
-
|
| 47 |
-
# Map to routing team
|
| 48 |
suggested_team = routing_teams.get(top_category, "General Support")
|
| 49 |
-
|
| 50 |
-
# Format confidence score as percentage
|
| 51 |
confidence_percent = f"{confidence_score*100:.2f}%"
|
| 52 |
-
|
| 53 |
return top_category, confidence_percent, suggested_team
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
# Step 4: Build Gradio UI
|
| 57 |
-
# -----------------------------
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
gr.Markdown("## 📞 Telecom Customer Complaint Classification and Routing")
|
| 60 |
-
gr.Markdown("Enter a customer complaint below
|
| 61 |
-
|
| 62 |
with gr.Row():
|
| 63 |
complaint_input = gr.Textbox(label="Customer Complaint", placeholder="Type your complaint here...", lines=4)
|
| 64 |
submit_btn = gr.Button("Submit")
|
| 65 |
-
|
| 66 |
with gr.Row():
|
| 67 |
category_output = gr.Textbox(label="Predicted Category")
|
| 68 |
confidence_output = gr.Textbox(label="Confidence Score")
|
| 69 |
team_output = gr.Textbox(label="Suggested Routing Team")
|
| 70 |
-
|
| 71 |
-
# Connect button to function
|
| 72 |
submit_btn.click(
|
| 73 |
classify_complaint,
|
| 74 |
inputs=complaint_input,
|
| 75 |
outputs=[category_output, confidence_output, team_output]
|
| 76 |
)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
# Step 5: Launch App
|
| 80 |
-
# -----------------------------
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# Telecom Customer Complaint Classification and Routing App
|
| 3 |
+
# Using Hugging Face Transformers + Gradio
|
| 4 |
+
|
| 5 |
from transformers import pipeline
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
# Load zero-shot classification model
|
|
|
|
|
|
|
|
|
|
| 9 |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
|
| 11 |
+
# Complaint categories
|
|
|
|
|
|
|
| 12 |
categories = [
|
| 13 |
"Network Issue",
|
| 14 |
"Billing Issue",
|
|
|
|
| 26 |
"Device Issue": "Technical Support"
|
| 27 |
}
|
| 28 |
|
| 29 |
+
# Function to classify complaint
|
|
|
|
|
|
|
| 30 |
def classify_complaint(complaint_text):
|
|
|
|
|
|
|
|
|
|
| 31 |
if not complaint_text.strip():
|
| 32 |
+
return "No input provided", "0.0%", "N/A"
|
| 33 |
+
|
|
|
|
| 34 |
result = classifier(complaint_text, candidate_labels=categories)
|
|
|
|
|
|
|
| 35 |
top_category = result['labels'][0]
|
| 36 |
confidence_score = result['scores'][0]
|
|
|
|
|
|
|
| 37 |
suggested_team = routing_teams.get(top_category, "General Support")
|
|
|
|
|
|
|
| 38 |
confidence_percent = f"{confidence_score*100:.2f}%"
|
| 39 |
+
|
| 40 |
return top_category, confidence_percent, suggested_team
|
| 41 |
|
| 42 |
+
# Build Gradio UI
|
|
|
|
|
|
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown("## 📞 Telecom Customer Complaint Classification and Routing")
|
| 45 |
+
gr.Markdown("Enter a customer complaint below to get category, confidence, and routing team.")
|
| 46 |
+
|
| 47 |
with gr.Row():
|
| 48 |
complaint_input = gr.Textbox(label="Customer Complaint", placeholder="Type your complaint here...", lines=4)
|
| 49 |
submit_btn = gr.Button("Submit")
|
| 50 |
+
|
| 51 |
with gr.Row():
|
| 52 |
category_output = gr.Textbox(label="Predicted Category")
|
| 53 |
confidence_output = gr.Textbox(label="Confidence Score")
|
| 54 |
team_output = gr.Textbox(label="Suggested Routing Team")
|
| 55 |
+
|
|
|
|
| 56 |
submit_btn.click(
|
| 57 |
classify_complaint,
|
| 58 |
inputs=complaint_input,
|
| 59 |
outputs=[category_output, confidence_output, team_output]
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Launch the app
|
|
|
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|