Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 5 |
+
|
| 6 |
+
def analyze_sentiment(text):
|
| 7 |
+
result = sentiment_analyzer(text)[0]
|
| 8 |
+
sentiment_score = result['label']
|
| 9 |
+
|
| 10 |
+
if sentiment_score == '1 star':
|
| 11 |
+
return 1
|
| 12 |
+
elif sentiment_score == '2 stars':
|
| 13 |
+
return 2
|
| 14 |
+
elif sentiment_score == '3 stars':
|
| 15 |
+
return 3
|
| 16 |
+
elif sentiment_score == '4 stars':
|
| 17 |
+
return 4
|
| 18 |
+
else:
|
| 19 |
+
return 5
|
| 20 |
+
|
| 21 |
+
examples = [
|
| 22 |
+
"I love this product! It's amazing!",
|
| 23 |
+
"This was the worst experience I've ever had.",
|
| 24 |
+
"The movie was okay, not great but not bad either.",
|
| 25 |
+
"Absolutely fantastic! I would recommend it to everyone."
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=analyze_sentiment, # Function to call for sentiment analysis
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(label="Enter Text", placeholder="Type or paste a sentence or paragraph here...", lines=5),
|
| 32 |
+
gr.Button("Analyze Sentiment") # Button to trigger analysis
|
| 33 |
+
],
|
| 34 |
+
outputs=gr.Textbox(label="Sentiment Rating (1 to 5 stars)"), # Display sentiment rating
|
| 35 |
+
live=False, # Disable live preview while typing
|
| 36 |
+
examples=examples, # Predefined examples
|
| 37 |
+
description="Sentiment analysis using BERT-based model for multilingual sentiment prediction."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
iface.launch()
|