Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 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':
|
|
@@ -18,6 +21,7 @@ def analyze_sentiment(text):
|
|
| 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.",
|
|
@@ -25,6 +29,7 @@ examples = [
|
|
| 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=[
|
|
@@ -37,4 +42,5 @@ iface = gr.Interface(
|
|
| 37 |
description="Sentiment analysis using BERT-based model for multilingual sentiment prediction."
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load sentiment analysis model from Hugging Face
|
| 5 |
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 6 |
|
| 7 |
+
# Function to analyze sentiment and convert it to star rating (1-5)
|
| 8 |
def analyze_sentiment(text):
|
| 9 |
result = sentiment_analyzer(text)[0]
|
| 10 |
sentiment_score = result['label']
|
| 11 |
|
| 12 |
+
# Convert sentiment score to numeric star rating (1-5 stars)
|
| 13 |
if sentiment_score == '1 star':
|
| 14 |
return 1
|
| 15 |
elif sentiment_score == '2 stars':
|
|
|
|
| 21 |
else:
|
| 22 |
return 5
|
| 23 |
|
| 24 |
+
# Define example sentences for easy testing
|
| 25 |
examples = [
|
| 26 |
"I love this product! It's amazing!",
|
| 27 |
"This was the worst experience I've ever had.",
|
|
|
|
| 29 |
"Absolutely fantastic! I would recommend it to everyone."
|
| 30 |
]
|
| 31 |
|
| 32 |
+
# Build the Gradio interface
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=analyze_sentiment, # Function to call for sentiment analysis
|
| 35 |
inputs=[
|
|
|
|
| 42 |
description="Sentiment analysis using BERT-based model for multilingual sentiment prediction."
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Launch the Gradio interface
|
| 46 |
iface.launch()
|