humy65 commited on
Commit
d2a6d58
verified
1 Parent(s): ef2c6c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
 
5
  # Load model and tokenizer from Hugging Face Hub
@@ -7,8 +7,15 @@ model_name = "humy65/hebrew-intent-classifier"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
- # Define intent labels (adjust to match your model's output)
11
- intent_labels = ["砖讻讞转 住讬住诪讛", "讘讬讟讜诇 诪谞讜讬", "砖讗诇讛 讻诇诇讬转", "转诪讬讻讛 讟讻谞讬转"]
 
 
 
 
 
 
 
12
 
13
  def classify_intent(text):
14
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
@@ -16,18 +23,25 @@ def classify_intent(text):
16
  outputs = model(**inputs)
17
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
18
  top_prob, top_label = torch.max(probs, dim=1)
19
- intent = intent_labels[top_label.item()]
 
 
 
20
  confidence = top_prob.item()
21
  return f"讻讜讜谞讛: {intent}\n专诪转 讘讬讟讞讜谉: {confidence:.2f}"
22
 
23
  # Build Gradio interface
24
- demo = gr.Interface(
25
- fn=classify_intent,
26
- inputs=gr.Textbox(label="讛拽诇讚 砖讗诇讛 讘注讘专讬转", placeholder="诇讚讜讙诪讛: 砖讻讞转讬 讗转 讛住讬住诪讛 砖诇讬"),
27
- outputs=gr.Textbox(label="转讜爪讗讛"),
28
- title="馃攳 诪住讜讜讙 讻讜讜谞讜转 讘注讘专讬转",
29
- description="诪讜讚诇 诇讝讬讛讜讬 讻讜讜谞讜转 讘砖驻讛 讛注讘专讬转. 讛拽诇讚 砖讗诇讛 讜专讗讛 讗转 讛讻讜讜谞讛 讛诪砖讜注专转.",
30
- theme="soft"
31
- )
 
 
 
 
32
 
33
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
3
  import torch
4
 
5
  # Load model and tokenizer from Hugging Face Hub
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
 
10
+ # Load config and extract label mapping
11
+ config = AutoConfig.from_pretrained(model_name)
12
+
13
+ # Try to get label names from config.id2label (if available)
14
+ if hasattr(config, "id2label") and config.id2label:
15
+ intent_labels = [config.id2label[i] for i in range(config.num_labels)]
16
+ else:
17
+ # Fallback: generate generic labels
18
+ intent_labels = [f"讻讜讜谞讛 {i}" for i in range(config.num_labels)]
19
 
20
  def classify_intent(text):
21
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
 
23
  outputs = model(**inputs)
24
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
25
  top_prob, top_label = torch.max(probs, dim=1)
26
+ try:
27
+ intent = intent_labels[top_label.item()]
28
+ except IndexError:
29
+ intent = "讻讜讜谞讛 诇讗 诪讝讜讛讛"
30
  confidence = top_prob.item()
31
  return f"讻讜讜谞讛: {intent}\n专诪转 讘讬讟讞讜谉: {confidence:.2f}"
32
 
33
  # Build Gradio interface
34
+ with gr.Blocks(theme="soft") as demo:
35
+ gr.Markdown("## 馃攳 诪住讜讜讙 讻讜讜谞讜转 讘注讘专讬转")
36
+ gr.Markdown("诪讜讚诇 诇讝讬讛讜讬 讻讜讜谞讜转 讘砖驻讛 讛注讘专讬转. 讛拽诇讚 砖讗诇讛 讜专讗讛 讗转 讛讻讜讜谞讛 讛诪砖讜注专转.")
37
+
38
+ with gr.Row():
39
+ input_text = gr.Textbox(label="讛拽诇讚 砖讗诇讛 讘注讘专讬转", placeholder="诇讚讜讙诪讛: 砖讻讞转讬 讗转 讛住讬住诪讛 砖诇讬")
40
+ intent_list = gr.Dropdown(label="讻讜讜谞讜转 讝诪讬谞讜转", choices=intent_labels, interactive=False)
41
+
42
+ output_text = gr.Textbox(label="转讜爪讗讛")
43
+ submit_btn = gr.Button("住讜讜讙")
44
+
45
+ submit_btn.click(fn=classify_intent, inputs=input_text, outputs=output_text)
46
 
47
  demo.launch()