Update app.py
Browse files
app.py
CHANGED
|
@@ -2,48 +2,43 @@ import torch
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
| 9 |
-
|
| 10 |
-
# Define label mapping
|
| 11 |
-
id2label = model.config.id2label or {
|
| 12 |
-
0: "Negative",
|
| 13 |
-
1: "Neutral",
|
| 14 |
-
2: "Positive"
|
| 15 |
-
}
|
| 16 |
|
|
|
|
| 17 |
def classify(text):
|
| 18 |
-
inputs = tokenizer(text, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
with torch.no_grad():
|
| 20 |
outputs = model(**inputs)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if __name__ == "__main__":
|
| 49 |
-
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load model & tokenizer from HF or local path
|
| 6 |
+
model_name = "ogflash/yelp_review_classifier" # Change if needed
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Fix for DistilBERT models that don't accept token_type_ids
|
| 11 |
def classify(text):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 13 |
+
# Remove token_type_ids if not supported
|
| 14 |
+
if "token_type_ids" in inputs and "token_type_ids" not in model.forward.__code__.co_varnames:
|
| 15 |
+
del inputs["token_type_ids"]
|
| 16 |
+
|
| 17 |
with torch.no_grad():
|
| 18 |
outputs = model(**inputs)
|
| 19 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 20 |
+
top_class = torch.argmax(probs, dim=1).item()
|
| 21 |
+
confidence = probs[0][top_class].item() * 100
|
| 22 |
+
|
| 23 |
+
# Reliable label mapping
|
| 24 |
+
id2label = model.config.id2label
|
| 25 |
+
if not id2label or not isinstance(id2label, dict) or len(id2label) == 0:
|
| 26 |
+
id2label = {
|
| 27 |
+
0: "Negative",
|
| 28 |
+
1: "Neutral",
|
| 29 |
+
2: "Positive"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
label_name = id2label.get(top_class, f"LABEL_{top_class}")
|
| 33 |
+
return f"{label_name} ({confidence:.2f}%)"
|
| 34 |
+
|
| 35 |
+
# UI with Gradio
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=classify,
|
| 38 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter text to analyze..."),
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="Sentiment Classifier",
|
| 41 |
+
description="Predicts sentiment using a BERT-based model.",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|