Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
|
5 |
+
# --- Load model ---
|
6 |
+
MODEL_NAME = "tabularisai/multilingual-sentiment-analysis"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
9 |
+
|
10 |
+
# --- Sentiment analysis function ---
|
11 |
+
def analyze_sentiment(text):
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
16 |
+
sentiment_map = {
|
17 |
+
0: "Very Negative",
|
18 |
+
1: "Negative",
|
19 |
+
2: "Neutral",
|
20 |
+
3: "Positive",
|
21 |
+
4: "Very Positive"
|
22 |
+
}
|
23 |
+
prediction = torch.argmax(probabilities, dim=-1).item()
|
24 |
+
return sentiment_map.get(prediction, "Unknown")
|
25 |
+
|
26 |
+
# --- Gradio UI ---
|
27 |
+
demo = gr.Interface(
|
28 |
+
fn=analyze_sentiment,
|
29 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter your sentence..."),
|
30 |
+
outputs="text",
|
31 |
+
title="Sentiment Analyzer",
|
32 |
+
description="Enter a sentence to analyze its sentiment using a multilingual model."
|
33 |
+
)
|
34 |
+
|
35 |
+
demo.launch()
|