Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
confidence = top_prob.item()
|
| 21 |
return f"讻讜讜谞讛: {intent}\n专诪转 讘讬讟讞讜谉: {confidence:.2f}"
|
| 22 |
|
| 23 |
# Build Gradio interface
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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()
|