King-8 commited on
Commit
e7d2b7d
·
verified ·
1 Parent(s): cba3001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -1,22 +1,20 @@
1
- import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
 
5
  # Load model and tokenizer
6
- model_name = "King-8/confidence-classifier" # change to your actual model path
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
- # Label mapping (update if yours are flipped)
11
- id2label = model.config.id2label
12
-
13
  label_map = {
14
  "LABEL_0": "confident",
15
  "LABEL_1": "not confident"
16
  }
17
 
18
  def classify_confidence(text):
19
- # Tokenize input (exclude token_type_ids)
20
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
21
  if "token_type_ids" in inputs:
22
  del inputs["token_type_ids"]
@@ -27,17 +25,17 @@ def classify_confidence(text):
27
  logits = outputs.logits
28
  probs = torch.nn.functional.softmax(logits, dim=1)
29
  predicted_class = torch.argmax(probs).item()
30
- label = id2label[predicted_class]
31
- prediction = label_map[label]
32
  score = round(probs[0][predicted_class].item() * 100, 2)
 
33
  color = "green" if label == "LABEL_0" else "red"
34
 
35
- return f"<b>Prediction:</b> <span style='color: {color}; font-weight: bold'>{label_text}</span> ({score}%)"
36
 
37
  iface = gr.Interface(
38
  fn=classify_confidence,
39
  inputs=gr.Textbox(lines=3, placeholder="Enter a statement..."),
40
- outputs="text",
41
  title="🌟 Confidence Classifier",
42
  description="""
43
  Enter a statement, and this model will predict whether it shows confidence or not.
@@ -47,4 +45,4 @@ Enter a statement, and this model will predict whether it shows confidence or no
47
  """,
48
  )
49
 
50
- iface.launch()
 
1
+ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
 
5
  # Load model and tokenizer
6
+ model_name = "King-8/confidence-classifier"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
+ # Label mapping
 
 
11
  label_map = {
12
  "LABEL_0": "confident",
13
  "LABEL_1": "not confident"
14
  }
15
 
16
  def classify_confidence(text):
17
+ # Tokenize input
18
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
19
  if "token_type_ids" in inputs:
20
  del inputs["token_type_ids"]
 
25
  logits = outputs.logits
26
  probs = torch.nn.functional.softmax(logits, dim=1)
27
  predicted_class = torch.argmax(probs).item()
28
+ label = model.config.id2label[predicted_class]
 
29
  score = round(probs[0][predicted_class].item() * 100, 2)
30
+ label_text = label_map[label]
31
  color = "green" if label == "LABEL_0" else "red"
32
 
33
+ return f"<b>Prediction:</b> <span style='color: {color}; font-weight: bold'>{label_text}</span> ({score}%)"
34
 
35
  iface = gr.Interface(
36
  fn=classify_confidence,
37
  inputs=gr.Textbox(lines=3, placeholder="Enter a statement..."),
38
+ outputs="html", # changed from "text" to "html"
39
  title="🌟 Confidence Classifier",
40
  description="""
41
  Enter a statement, and this model will predict whether it shows confidence or not.
 
45
  """,
46
  )
47
 
48
+ iface.launch()