Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
-
import torch
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
loaded_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
|
| 9 |
|
| 10 |
|
| 11 |
def predict_sentiment(text):
|
| 12 |
-
"""Predicts the sentiment of a given text."""
|
| 13 |
-
inputs = loaded_tokenizer(text, return_tensors="pt", padding=True, truncation=True) #
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
sentiment = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
else:
|
| 24 |
-
sentiment = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Return probabilities as well for a more informative output
|
| 27 |
-
return {
|
| 28 |
-
"Negative": float(probabilities[0][0]),
|
| 29 |
-
"Positive": float(probabilities[0][1]),
|
| 30 |
-
"Neutral": float(probabilities[0][2]),
|
| 31 |
-
}, sentiment
|
| 32 |
|
| 33 |
# Create example sentences
|
| 34 |
examples = [
|
| 35 |
-
["
|
| 36 |
-
["
|
| 37 |
-
["
|
| 38 |
-
["
|
| 39 |
-
|
| 40 |
]
|
| 41 |
|
| 42 |
|
|
@@ -46,15 +62,13 @@ iface = gr.Interface(
|
|
| 46 |
inputs=gr.Textbox(label="Enter Persian Text", lines=5, placeholder="Type your text here..."),
|
| 47 |
outputs=[
|
| 48 |
gr.Label(label="Sentiment Probabilities"),
|
| 49 |
-
gr.Textbox(label="Predicted Sentiment")
|
| 50 |
-
|
| 51 |
],
|
| 52 |
-
title="Persian
|
| 53 |
-
description="Enter a Persian sentence and get its sentiment (
|
| 54 |
examples=examples,
|
| 55 |
-
live=False #
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
if __name__ == "__main__":
|
| 60 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np # Import numpy
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# Check for GPU availability and set device
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
print(f"Using device: {device}")
|
| 10 |
+
|
| 11 |
+
# Load the model and tokenizer
|
| 12 |
+
model_name = "explorewithai/PersianSwear-Detector" # Corrected model name
|
| 13 |
+
loaded_model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device) # Move model to device
|
| 14 |
loaded_tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
|
| 16 |
|
| 17 |
|
| 18 |
def predict_sentiment(text):
|
| 19 |
+
"""Predicts the sentiment (Bad Word, Good Word, Neutral Word) of a given text."""
|
| 20 |
+
inputs = loaded_tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device) # Move inputs to GPU
|
| 21 |
+
with torch.no_grad(): # Ensure no gradients are calculated
|
| 22 |
+
outputs = loaded_model(**inputs)
|
| 23 |
+
logits = outputs.logits
|
| 24 |
+
probabilities = torch.nn.functional.softmax(logits, dim=-1) # Get probabilities
|
| 25 |
+
prediction = torch.argmax(logits, dim=-1).item()
|
| 26 |
+
|
| 27 |
+
# Map numeric labels to meaningful strings and get probabilities
|
| 28 |
+
if prediction == 4:
|
| 29 |
+
sentiment = "Bad Word"
|
| 30 |
+
elif prediction == 0:
|
| 31 |
+
sentiment = "Good Word"
|
| 32 |
+
elif prediction == 3:
|
| 33 |
+
sentiment = "Neutral Word"
|
| 34 |
else:
|
| 35 |
+
sentiment = "Unknown" # Should not happen, but good practice
|
| 36 |
+
|
| 37 |
+
# Create a dictionary for the probabilities
|
| 38 |
+
prob_dict = {}
|
| 39 |
+
if "Bad Word" in ["Bad Word", "Good Word", "Neutral Word"]:
|
| 40 |
+
prob_dict["Bad Word"] = float(probabilities[0][4]) if 4 < probabilities.shape[1] else 0.0
|
| 41 |
+
if "Good Word" in ["Bad Word", "Good Word", "Neutral Word"]:
|
| 42 |
+
prob_dict["Good Word"] = float(probabilities[0][0]) if 0 < probabilities.shape[1] else 0.0
|
| 43 |
+
if "Neutral Word" in ["Bad Word", "Good Word", "Neutral Word"]:
|
| 44 |
+
prob_dict["Neutral Word"] = float(probabilities[0][3]) if 3 < probabilities.shape[1] else 0.0
|
| 45 |
+
|
| 46 |
+
return prob_dict, sentiment
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Create example sentences
|
| 50 |
examples = [
|
| 51 |
+
["چه کت و شلوار زیبایی"], # Good word example
|
| 52 |
+
["این فیلم خیلی زیبا بود"], # Good word example
|
| 53 |
+
["میز"], # Neutral word example
|
| 54 |
+
["کثافت"], # Bad word example
|
| 55 |
+
["هوا خوب است."] #neutral
|
| 56 |
]
|
| 57 |
|
| 58 |
|
|
|
|
| 62 |
inputs=gr.Textbox(label="Enter Persian Text", lines=5, placeholder="Type your text here..."),
|
| 63 |
outputs=[
|
| 64 |
gr.Label(label="Sentiment Probabilities"),
|
| 65 |
+
gr.Textbox(label="Predicted Sentiment") # Output component for the sentiment string
|
|
|
|
| 66 |
],
|
| 67 |
+
title="Persian Swear Word Detection",
|
| 68 |
+
description="Enter a Persian sentence and get its sentiment (Good Word, Bad Word, or Neutral Word).",
|
| 69 |
examples=examples,
|
| 70 |
+
live=False # Set to True for automatic updates as you type
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
if __name__ == "__main__":
|
| 74 |
iface.launch()
|