Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,12 @@ import gradio as gr
|
|
| 5 |
from transformers import AutoTokenizer
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
-
# Silence background warning
|
| 9 |
os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
|
| 10 |
|
| 11 |
-
# Target model repository
|
| 12 |
REPO_ID = "Havoc999/distilbert-imdb-sentiment-onnx"
|
| 13 |
|
| 14 |
-
print("Loading model
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
|
| 16 |
model_path = hf_hub_download(repo_id=REPO_ID, filename="model.onnx")
|
| 17 |
session = ort.InferenceSession(model_path)
|
|
@@ -20,7 +19,7 @@ def predict_sentiment(text):
|
|
| 20 |
if not text.strip():
|
| 21 |
return {"Negative π¬β": 0.5, "Positive πΏβ
": 0.5}
|
| 22 |
|
| 23 |
-
# 1. Tokenize
|
| 24 |
inputs = tokenizer(
|
| 25 |
text,
|
| 26 |
return_tensors="np",
|
|
@@ -34,26 +33,23 @@ def predict_sentiment(text):
|
|
| 34 |
"attention_mask": inputs["attention_mask"].astype(np.int64)
|
| 35 |
}
|
| 36 |
|
| 37 |
-
# 2.
|
| 38 |
-
onnx_outputs = session.run(
|
| 39 |
|
| 40 |
-
# 3.
|
| 41 |
-
logits = onnx_outputs.flatten()
|
| 42 |
|
| 43 |
-
# 4.
|
| 44 |
exp_logits = np.exp(logits - np.max(logits))
|
| 45 |
probabilities = exp_logits / np.sum(exp_logits)
|
| 46 |
|
| 47 |
-
# 5.
|
| 48 |
-
neg_score = float(probabilities)
|
| 49 |
-
pos_score = float(probabilities)
|
| 50 |
-
|
| 51 |
return {
|
| 52 |
-
"Negative π¬β":
|
| 53 |
-
"Positive πΏβ
":
|
| 54 |
}
|
| 55 |
|
| 56 |
-
# 6.
|
| 57 |
demo = gr.Interface(
|
| 58 |
fn=predict_sentiment,
|
| 59 |
inputs=gr.Textbox(lines=4, placeholder="Type an IMDB movie review here...", label="Your Movie Review"),
|
|
|
|
| 5 |
from transformers import AutoTokenizer
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
+
# Silence background warning logs
|
| 9 |
os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
|
| 10 |
|
|
|
|
| 11 |
REPO_ID = "Havoc999/distilbert-imdb-sentiment-onnx"
|
| 12 |
|
| 13 |
+
print("Loading model assets locally...")
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
|
| 15 |
model_path = hf_hub_download(repo_id=REPO_ID, filename="model.onnx")
|
| 16 |
session = ort.InferenceSession(model_path)
|
|
|
|
| 19 |
if not text.strip():
|
| 20 |
return {"Negative π¬β": 0.5, "Positive πΏβ
": 0.5}
|
| 21 |
|
| 22 |
+
# 1. Tokenize text inputs
|
| 23 |
inputs = tokenizer(
|
| 24 |
text,
|
| 25 |
return_tensors="np",
|
|
|
|
| 33 |
"attention_mask": inputs["attention_mask"].astype(np.int64)
|
| 34 |
}
|
| 35 |
|
| 36 |
+
# 2. Force ONNX to return ONLY the classification logits array
|
| 37 |
+
onnx_outputs = session.run(["logits"], onnx_inputs)
|
| 38 |
|
| 39 |
+
# 3. Safely convert the output to a flat 1D numpy array, regardless of nested lists
|
| 40 |
+
logits = np.asarray(onnx_outputs).flatten()
|
| 41 |
|
| 42 |
+
# 4. Softmax probability conversion
|
| 43 |
exp_logits = np.exp(logits - np.max(logits))
|
| 44 |
probabilities = exp_logits / np.sum(exp_logits)
|
| 45 |
|
| 46 |
+
# 5. Return pure Python float scalars to the Gradio UI
|
|
|
|
|
|
|
|
|
|
| 47 |
return {
|
| 48 |
+
"Negative π¬β": float(probabilities),
|
| 49 |
+
"Positive πΏβ
": float(probabilities)
|
| 50 |
}
|
| 51 |
|
| 52 |
+
# 6. Initialize UI
|
| 53 |
demo = gr.Interface(
|
| 54 |
fn=predict_sentiment,
|
| 55 |
inputs=gr.Textbox(lines=4, placeholder="Type an IMDB movie review here...", label="Your Movie Review"),
|