samurai9776 commited on
Commit
d5691ce
Β·
verified Β·
1 Parent(s): 97751dd

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +51 -67
app.py CHANGED
@@ -2,14 +2,21 @@ import gradio as gr
2
  from transformers import pipeline
3
  import torch
4
 
5
- # Load the NEW menu-aware model
 
6
  model_id = "samurai9776/thought-classifier-menu-aware"
 
 
 
 
7
  classifier = pipeline(
8
  "text-classification",
9
  model=model_id,
10
  device=0 if torch.cuda.is_available() else -1
11
  )
12
 
 
 
13
  def classify_thought(ai_utterance, cx_utterance):
14
  """Classify if the conversation is complete or incomplete"""
15
 
@@ -30,40 +37,37 @@ def classify_thought(ai_utterance, cx_utterance):
30
  # Determine prediction
31
  prediction = "Complete βœ“" if complete_score > incomplete_score else "Incomplete ⚠️"
32
 
33
- # Determine method used
34
  confidence = max(complete_score, incomplete_score)
35
- if confidence > 0.95:
36
- method = "High confidence prediction"
37
- else:
38
- method = "Moderate confidence prediction"
39
 
40
- return prediction, complete_score, incomplete_score, method
41
 
42
  # Create Gradio interface
43
  with gr.Blocks(
44
  title="Thought Completion Classifier - Menu Aware v2",
45
- theme=gr.themes.Soft(),
46
- css="""
47
- .gradio-container {
48
- font-family: 'IBM Plex Sans', sans-serif;
49
- }
50
- """
51
  ) as demo:
52
  gr.Markdown("""
53
  # πŸ€– Thought Completion Classifier - Menu Aware v2
54
 
55
- This enhanced model determines if a conversation represents a **complete** or **incomplete** thought.
56
- Now with improved context understanding and menu-aware predictions!
57
 
58
- ### 🎯 What's New:
 
59
  - βœ… Fixed 3,000+ mislabeled training examples
60
- - βœ… Handles ambiguous terms (e.g., "chicken" could mean 75 different items!)
61
- - βœ… Context-aware: "What sandwich?" + "Chicken" = Complete
62
- - βœ… High precision for incomplete thought detection
 
 
 
 
 
63
  """)
64
 
65
  with gr.Row():
66
- with gr.Column(scale=1):
67
  ai_input = gr.Textbox(
68
  label="AI Utterance",
69
  placeholder="e.g., What sandwich would you like?",
@@ -75,14 +79,13 @@ with gr.Blocks(
75
  lines=2
76
  )
77
 
78
- with gr.Row():
79
- classify_btn = gr.Button("πŸ” Classify", variant="primary", scale=2)
80
- clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary", scale=1)
81
 
82
- with gr.Column(scale=1):
83
  prediction = gr.Textbox(
84
  label="Prediction",
85
- interactive=False
 
86
  )
87
  with gr.Row():
88
  complete_score = gr.Number(
@@ -95,65 +98,46 @@ with gr.Blocks(
95
  precision=3,
96
  interactive=False
97
  )
98
- method = gr.Textbox(
99
- label="Confidence Level",
100
  interactive=False
101
  )
102
 
103
- # Examples
104
- gr.Markdown("### πŸ“ Try These Examples:")
105
  gr.Examples(
106
  examples=[
107
- ["What can I get for you?", "Chicken"],
108
- ["What sandwich would you like?", "Chicken"],
109
- ["Chicken or beef?", "Chicken"],
110
- ["Chicken or beef?", "Large"],
111
- ["What size?", "Large"],
112
- ["Anything else?", "Large"],
113
- ["Ready to order?", "Spicy Chicken Sandwich Combo"],
114
- ["Anything else?", "That's all"],
115
  ],
116
  inputs=[ai_input, cx_input],
117
- outputs=[prediction, complete_score, incomplete_score, method],
118
  fn=classify_thought,
119
  cache_examples=True,
120
  )
121
 
122
- # Event handlers
123
  classify_btn.click(
124
  fn=classify_thought,
125
  inputs=[ai_input, cx_input],
126
- outputs=[prediction, complete_score, incomplete_score, method]
127
  )
128
 
129
- clear_btn.click(
130
- fn=lambda: ("", "", "", 0, 0, ""),
131
- inputs=[],
132
- outputs=[ai_input, cx_input, prediction, complete_score, incomplete_score, method]
133
- )
134
 
135
- # Model info
136
- with gr.Accordion("πŸ“Š Model Information", open=False):
137
- gr.Markdown("""
138
- ### Menu-Aware Model v2
139
- - **Training samples:** 15,000+ (with 3,000+ corrections)
140
- - **Accuracy:** ~92%
141
- - **Base model:** DistilBERT
142
- - **Special focus:** High precision for incomplete thoughts
143
-
144
- ### How it works:
145
- 1. **Context matters**: "What sandwich?" + "Chicken" = Complete
146
- 2. **Ambiguity detection**: "Anything else?" + "Chicken" = Incomplete
147
- 3. **Menu awareness**: Trained on 363 menu items with variations
148
-
149
- ### API Usage:
150
- ```python
151
- from transformers import pipeline
152
- classifier = pipeline("text-classification",
153
- model="samurai9776/thought-classifier-menu-aware")
154
- result = classifier("What sandwich? [SEP] Chicken")
155
- ```
156
- """)
157
 
158
  if __name__ == "__main__":
159
  demo.launch()
 
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
 
 
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?",
 
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(
 
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()