samurai9776 commited on
Commit
19f6906
Β·
verified Β·
1 Parent(s): d5691ce

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +15 -114
app.py CHANGED
@@ -1,143 +1,44 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import torch
4
 
5
- # Load the NEW menu-aware model (v2)
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=model_id,
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
- # Create Gradio interface
47
- with gr.Blocks(
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
- label="AI Utterance",
73
- placeholder="e.g., What sandwich would you like?",
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
- label="Prediction",
87
- interactive=False,
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
- fn=classify_thought,
127
  inputs=[ai_input, cx_input],
128
- outputs=[prediction, complete_score, incomplete_score, confidence]
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
- if __name__ == "__main__":
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()