Update app.py
Browse files
app.py
CHANGED
|
@@ -7,18 +7,12 @@ sentiment_pipeline = pipeline("sentiment-analysis", model="nlptown/bert-base-mul
|
|
| 7 |
# Define a function for sentiment analysis
|
| 8 |
def analyze_sentiment(text):
|
| 9 |
result = sentiment_pipeline(text)
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Map the
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# Convert label to sentiment score
|
| 16 |
-
if label == 'negative':
|
| 17 |
-
sentiment_score = -score # Map negative to -1 to 0
|
| 18 |
-
elif label == 'neutral':
|
| 19 |
-
sentiment_score = 0.0 # Neutral is 0
|
| 20 |
-
elif label == 'positive':
|
| 21 |
-
sentiment_score = score # Map positive to 0 to 1
|
| 22 |
|
| 23 |
return sentiment_score
|
| 24 |
|
|
@@ -38,7 +32,13 @@ interface = gr.Interface(
|
|
| 38 |
outputs=gr.Number(label="Sentiment Score (-1 to 1)"),
|
| 39 |
examples=examples,
|
| 40 |
title="Sentiment Analysis with Hugging Face Transformers",
|
| 41 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the Gradio app
|
|
|
|
| 7 |
# Define a function for sentiment analysis
|
| 8 |
def analyze_sentiment(text):
|
| 9 |
result = sentiment_pipeline(text)
|
| 10 |
+
label = result[0]['label'] # Get the label, e.g., "1 star", "5 stars"
|
| 11 |
+
score = int(label.split()[0]) # Extract the numerical part of the label (e.g., 1, 2, ..., 5)
|
| 12 |
|
| 13 |
+
# Map the score from 1-5 to -1 to 1:
|
| 14 |
+
# 1 star -> -1.0, 2 stars -> -0.5, 3 stars -> 0.0, 4 stars -> 0.5, 5 stars -> 1.0
|
| 15 |
+
sentiment_score = (score - 3) / 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
return sentiment_score
|
| 18 |
|
|
|
|
| 32 |
outputs=gr.Number(label="Sentiment Score (-1 to 1)"),
|
| 33 |
examples=examples,
|
| 34 |
title="Sentiment Analysis with Hugging Face Transformers",
|
| 35 |
+
description="""
|
| 36 |
+
Enter a sentence to analyze its sentiment. The model will output a sentiment score between -1 and 1:
|
| 37 |
+
- **-1**: Extremely Negative
|
| 38 |
+
- **0**: Neutral
|
| 39 |
+
- **+1**: Extremely Positive
|
| 40 |
+
This uses the `nlptown/bert-base-multilingual-uncased-sentiment` model.
|
| 41 |
+
"""
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the Gradio app
|