Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,15 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
|
5 |
-
# Initialize device
|
6 |
-
device =
|
7 |
print(f"Using device: {device}")
|
8 |
|
9 |
# Load model and tokenizer
|
10 |
model_name = "tabularisai/robust-sentiment-analysis"
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
|
|
|
13 |
|
14 |
# Define sentiment mapping
|
15 |
SENTIMENT_MAP = {
|
@@ -29,12 +30,12 @@ def analyze_sentiment(text, show_probabilities=False):
|
|
29 |
text = text.lower()
|
30 |
|
31 |
# Tokenize and prepare input
|
32 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
33 |
|
34 |
with torch.no_grad():
|
35 |
outputs = model(**inputs)
|
36 |
|
37 |
-
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1).
|
38 |
predicted_class = probabilities.argmax()
|
39 |
predicted_sentiment = SENTIMENT_MAP[predicted_class]
|
40 |
confidence = probabilities[predicted_class]
|
@@ -61,20 +62,8 @@ def analyze_sentiment(text, show_probabilities=False):
|
|
61 |
except Exception as e:
|
62 |
return f"An error occurred during sentiment analysis: {str(e)}"
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
body_background_fill="*radial-gradient(circle at top left, #f3e7e9, #e3eeff)",
|
67 |
-
block_background_fill="rgba(255, 255, 255, 0.95)",
|
68 |
-
block_border_width="0px",
|
69 |
-
block_shadow="*0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1)",
|
70 |
-
button_primary_background_fill="*linear-gradient(90deg, #4F46E5, #7C3AED)",
|
71 |
-
button_primary_background_fill_hover="*linear-gradient(90deg, #4338CA, #6D28D9)",
|
72 |
-
button_primary_text_color="white",
|
73 |
-
input_background_fill="white",
|
74 |
-
)
|
75 |
-
|
76 |
-
# Create Gradio interface using Blocks for better layout control
|
77 |
-
with gr.Blocks(theme=custom_theme) as demo:
|
78 |
gr.Markdown(
|
79 |
"""
|
80 |
# 🎭 Sentiment Analysis Wizard
|
@@ -123,6 +112,13 @@ with gr.Blocks(theme=custom_theme) as demo:
|
|
123 |
outputs=output
|
124 |
)
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
# Launch the interface
|
128 |
demo.launch()
|
|
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
|
5 |
+
# Initialize device - force CPU usage
|
6 |
+
device = "cpu"
|
7 |
print(f"Using device: {device}")
|
8 |
|
9 |
# Load model and tokenizer
|
10 |
model_name = "tabularisai/robust-sentiment-analysis"
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name) # Remove .to(device)
|
13 |
+
model.eval() # Set model to evaluation mode
|
14 |
|
15 |
# Define sentiment mapping
|
16 |
SENTIMENT_MAP = {
|
|
|
30 |
text = text.lower()
|
31 |
|
32 |
# Tokenize and prepare input
|
33 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
34 |
|
35 |
with torch.no_grad():
|
36 |
outputs = model(**inputs)
|
37 |
|
38 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1).numpy()[0]
|
39 |
predicted_class = probabilities.argmax()
|
40 |
predicted_sentiment = SENTIMENT_MAP[predicted_class]
|
41 |
confidence = probabilities[predicted_class]
|
|
|
62 |
except Exception as e:
|
63 |
return f"An error occurred during sentiment analysis: {str(e)}"
|
64 |
|
65 |
+
# Create Gradio interface using Blocks
|
66 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
gr.Markdown(
|
68 |
"""
|
69 |
# 🎭 Sentiment Analysis Wizard
|
|
|
112 |
outputs=output
|
113 |
)
|
114 |
|
115 |
+
gr.Markdown(
|
116 |
+
"""
|
117 |
+
<div style='text-align: center; padding: 1rem; margin-top: 2rem; font-size: 0.9em; color: #666;'>
|
118 |
+
Developed with ❤️ using Gradio and Transformers by Hugging Face
|
119 |
+
</div>
|
120 |
+
"""
|
121 |
+
)
|
122 |
|
123 |
# Launch the interface
|
124 |
demo.launch()
|