Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,26 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# Load model and tokenizer from Hugging Face
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
# Use id2label from the config
|
| 11 |
-
|
| 12 |
|
| 13 |
# Inference function
|
| 14 |
def classify_text(text):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Gradio interface
|
| 26 |
demo = gr.Interface(
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# Load model and tokenizer from Hugging Face
|
| 6 |
+
model_name = "iro-malta07/distilbert-base-german-lang-level-class" # replace with your model path
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
|
| 10 |
# Use id2label from the config
|
| 11 |
+
id2label = model.config.id2label
|
| 12 |
|
| 13 |
# Inference function
|
| 14 |
def classify_text(text):
|
| 15 |
+
if not text.endswith("."):
|
| 16 |
+
text = text + "."
|
| 17 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 21 |
+
pred_class = torch.argmax(probs, dim=1).item()
|
| 22 |
+
confidence = probs[0, pred_class].item()
|
| 23 |
+
label = id2label[pred_class]
|
| 24 |
+
return f"Predicted Level: {label} (Confidence: {confidence:.2f})"
|
| 25 |
+
|
| 26 |
|
| 27 |
# Gradio interface
|
| 28 |
demo = gr.Interface(
|