Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Initialize the sentiment analysis pipeline
|
5 |
+
# Model: nlptown/bert-base-multilingual-uncased-sentiment
|
6 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
7 |
+
|
8 |
+
def analyze_sentiment(text):
|
9 |
+
"""
|
10 |
+
Returns the predicted sentiment as a label ranging from 1 to 5 stars.
|
11 |
+
"""
|
12 |
+
result = sentiment_analyzer(text)[0]
|
13 |
+
label = result["label"] # e.g., "1 star", "2 stars", "3 stars", "4 stars", or "5 stars"
|
14 |
+
return f"Predicted sentiment: {label}"
|
15 |
+
|
16 |
+
# Predefined examples
|
17 |
+
examples = [
|
18 |
+
["I love this product! It's amazing!"],
|
19 |
+
["This was the worst experience I've ever had."],
|
20 |
+
["The movie was okay, not great but not bad either."],
|
21 |
+
["Absolutely fantastic! I would recommend it to everyone."]
|
22 |
+
]
|
23 |
+
|
24 |
+
# Create the Gradio interface
|
25 |
+
demo = gr.Interface(
|
26 |
+
fn=analyze_sentiment,
|
27 |
+
inputs=gr.Textbox(lines=3, label="Enter Your Text Here"),
|
28 |
+
outputs=gr.Textbox(label="Predicted Sentiment"),
|
29 |
+
title="Multilingual Sentiment Analysis",
|
30 |
+
description=(
|
31 |
+
"This app uses the 'nlptown/bert-base-multilingual-uncased-sentiment' model "
|
32 |
+
"to predict sentiment on a scale of 1 to 5 stars."
|
33 |
+
),
|
34 |
+
examples=examples,
|
35 |
+
)
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch()
|