Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,143 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# Load the
|
| 6 |
-
# This model doesn't have pipeline.py - it's just a fine-tuned DistilBERT
|
| 7 |
-
model_id = "samurai9776/thought-classifier-menu-aware"
|
| 8 |
-
|
| 9 |
-
print(f"Loading model: {model_id}")
|
| 10 |
-
|
| 11 |
-
# Load without trust_remote_code (new model doesn't need it)
|
| 12 |
classifier = pipeline(
|
| 13 |
"text-classification",
|
| 14 |
-
model=
|
| 15 |
-
device=0 if torch.cuda.is_available() else -1
|
| 16 |
)
|
| 17 |
|
| 18 |
-
print("β
Model loaded successfully!")
|
| 19 |
-
|
| 20 |
def classify_thought(ai_utterance, cx_utterance):
|
| 21 |
-
"""Classify if the conversation is complete or incomplete"""
|
| 22 |
-
|
| 23 |
if not ai_utterance or not cx_utterance:
|
| 24 |
-
return "Please enter both utterances", 0, 0
|
| 25 |
|
| 26 |
-
# Combine utterances with [SEP] token
|
| 27 |
text = f"{ai_utterance} [SEP] {cx_utterance}"
|
| 28 |
-
|
| 29 |
-
# Get prediction
|
| 30 |
results = classifier(text)
|
| 31 |
|
| 32 |
-
# Extract scores
|
| 33 |
scores = {r['label']: r['score'] for r in results}
|
| 34 |
complete_score = scores.get('COMPLETE', 0)
|
| 35 |
incomplete_score = scores.get('INCOMPLETE', 0)
|
| 36 |
|
| 37 |
-
# Determine prediction
|
| 38 |
prediction = "Complete β" if complete_score > incomplete_score else "Incomplete β οΈ"
|
| 39 |
-
|
| 40 |
-
# Determine confidence
|
| 41 |
-
confidence = max(complete_score, incomplete_score)
|
| 42 |
-
confidence_text = f"{confidence:.1%} confidence"
|
| 43 |
-
|
| 44 |
-
return prediction, complete_score, incomplete_score, confidence_text
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
title="Thought Completion Classifier - Menu Aware v2",
|
| 49 |
-
theme=gr.themes.Soft()
|
| 50 |
-
) as demo:
|
| 51 |
-
gr.Markdown("""
|
| 52 |
-
# π€ Thought Completion Classifier - Menu Aware v2
|
| 53 |
-
|
| 54 |
-
Enhanced model with better context understanding and menu awareness.
|
| 55 |
-
|
| 56 |
-
### β¨ What's New in v2:
|
| 57 |
-
- β
Trained on 15,000+ examples (vs 900 in v1)
|
| 58 |
-
- β
Fixed 3,000+ mislabeled training examples
|
| 59 |
-
- β
Handles ambiguous terms like "chicken" (75 menu items!)
|
| 60 |
-
- β
Better context understanding
|
| 61 |
-
- β
~92% accuracy
|
| 62 |
-
|
| 63 |
-
### π― How it works:
|
| 64 |
-
The model understands context:
|
| 65 |
-
- "What sandwich?" + "Chicken" = **Complete** (context is clear)
|
| 66 |
-
- "Anything else?" + "Chicken" = **Incomplete** (ambiguous)
|
| 67 |
-
""")
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column():
|
| 71 |
-
ai_input = gr.Textbox(
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
lines=2
|
| 75 |
-
)
|
| 76 |
-
cx_input = gr.Textbox(
|
| 77 |
-
label="Customer Utterance",
|
| 78 |
-
placeholder="e.g., Chicken",
|
| 79 |
-
lines=2
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
classify_btn = gr.Button("π Classify", variant="primary", size="lg")
|
| 83 |
|
| 84 |
with gr.Column():
|
| 85 |
-
prediction = gr.Textbox(
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
elem_classes=["prediction-output"]
|
| 89 |
-
)
|
| 90 |
-
with gr.Row():
|
| 91 |
-
complete_score = gr.Number(
|
| 92 |
-
label="Complete Score",
|
| 93 |
-
precision=3,
|
| 94 |
-
interactive=False
|
| 95 |
-
)
|
| 96 |
-
incomplete_score = gr.Number(
|
| 97 |
-
label="Incomplete Score",
|
| 98 |
-
precision=3,
|
| 99 |
-
interactive=False
|
| 100 |
-
)
|
| 101 |
-
confidence = gr.Textbox(
|
| 102 |
-
label="Confidence",
|
| 103 |
-
interactive=False
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
# Examples showing context awareness
|
| 107 |
-
gr.Markdown("### π Try these examples to see context awareness:")
|
| 108 |
-
gr.Examples(
|
| 109 |
-
examples=[
|
| 110 |
-
["What can I get for you?", "Chicken", "Should be INCOMPLETE (ambiguous)"],
|
| 111 |
-
["What sandwich would you like?", "Chicken", "Should be COMPLETE (context clear)"],
|
| 112 |
-
["Chicken or beef?", "Chicken", "Should be COMPLETE (direct answer)"],
|
| 113 |
-
["Chicken or beef?", "Large", "Should be INCOMPLETE (doesn't answer question)"],
|
| 114 |
-
["What size?", "Large", "Should be COMPLETE (answers size question)"],
|
| 115 |
-
["Anything else?", "Large", "Should be INCOMPLETE (size without item)"],
|
| 116 |
-
["Ready to order?", "Spicy Chicken Sandwich Combo", "Should be COMPLETE (full item)"],
|
| 117 |
-
["Anything else?", "That's all", "Should be COMPLETE (clear ending)"],
|
| 118 |
-
],
|
| 119 |
-
inputs=[ai_input, cx_input],
|
| 120 |
-
outputs=[prediction, complete_score, incomplete_score, confidence],
|
| 121 |
-
fn=classify_thought,
|
| 122 |
-
cache_examples=True,
|
| 123 |
-
)
|
| 124 |
|
| 125 |
classify_btn.click(
|
| 126 |
-
|
| 127 |
inputs=[ai_input, cx_input],
|
| 128 |
-
outputs=[prediction, complete_score, incomplete_score
|
| 129 |
)
|
| 130 |
-
|
| 131 |
-
gr.Markdown("""
|
| 132 |
-
---
|
| 133 |
-
### π Model Comparison:
|
| 134 |
-
- **v1**: Rule-based + neural (samurai9776/thought-classifier)
|
| 135 |
-
- **v2**: Pure neural with better training (samurai9776/thought-classifier-menu-aware)
|
| 136 |
-
|
| 137 |
-
### π Links:
|
| 138 |
-
- [Model v2 (Current)](https://huggingface.co/samurai9776/thought-classifier-menu-aware)
|
| 139 |
-
- [Model v1 (Previous)](https://huggingface.co/samurai9776/thought-classifier)
|
| 140 |
-
""")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Load the clean v2 model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
classifier = pipeline(
|
| 6 |
"text-classification",
|
| 7 |
+
model="samurai9776/thought-classifier-menu-aware"
|
|
|
|
| 8 |
)
|
| 9 |
|
|
|
|
|
|
|
| 10 |
def classify_thought(ai_utterance, cx_utterance):
|
|
|
|
|
|
|
| 11 |
if not ai_utterance or not cx_utterance:
|
| 12 |
+
return "Please enter both utterances", 0, 0
|
| 13 |
|
|
|
|
| 14 |
text = f"{ai_utterance} [SEP] {cx_utterance}"
|
|
|
|
|
|
|
| 15 |
results = classifier(text)
|
| 16 |
|
|
|
|
| 17 |
scores = {r['label']: r['score'] for r in results}
|
| 18 |
complete_score = scores.get('COMPLETE', 0)
|
| 19 |
incomplete_score = scores.get('INCOMPLETE', 0)
|
| 20 |
|
|
|
|
| 21 |
prediction = "Complete β" if complete_score > incomplete_score else "Incomplete β οΈ"
|
| 22 |
+
return prediction, complete_score, incomplete_score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
with gr.Blocks(title="Thought Classifier v2") as demo:
|
| 25 |
+
gr.Markdown("# π€ Thought Completion Classifier v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
with gr.Row():
|
| 28 |
with gr.Column():
|
| 29 |
+
ai_input = gr.Textbox(label="AI Utterance", placeholder="What sandwich?")
|
| 30 |
+
cx_input = gr.Textbox(label="Customer Utterance", placeholder="Chicken")
|
| 31 |
+
classify_btn = gr.Button("Classify", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
with gr.Column():
|
| 34 |
+
prediction = gr.Textbox(label="Prediction")
|
| 35 |
+
complete_score = gr.Number(label="Complete Score")
|
| 36 |
+
incomplete_score = gr.Number(label="Incomplete Score")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
classify_btn.click(
|
| 39 |
+
classify_thought,
|
| 40 |
inputs=[ai_input, cx_input],
|
| 41 |
+
outputs=[prediction, complete_score, incomplete_score]
|
| 42 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
demo.launch()
|
|
|