Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the sentiment analysis pipeline with the specified model
|
| 5 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 6 |
+
|
| 7 |
+
# Define the sentiment analysis function
|
| 8 |
+
def analyze_sentiment(text):
|
| 9 |
+
# Perform sentiment analysis
|
| 10 |
+
result = sentiment_analyzer(text)[0]
|
| 11 |
+
# Extract label (e.g., "1 star", "2 stars", etc.) and return it
|
| 12 |
+
return f"Predicted Sentiment: {result['label']}"
|
| 13 |
+
|
| 14 |
+
# Define input and output components with clear labels
|
| 15 |
+
input_text = gr.Textbox(lines=5, label="Enter Your Text", placeholder="Type a sentence or paragraph here...")
|
| 16 |
+
output_sentiment = gr.Textbox(label="Sentiment Result")
|
| 17 |
+
|
| 18 |
+
# Define example inputs
|
| 19 |
+
examples = [
|
| 20 |
+
"I love this product! It's amazing!",
|
| 21 |
+
"This was the worst experience I've ever had.",
|
| 22 |
+
"The movie was okay, not great but not bad either.",
|
| 23 |
+
"Absolutely fantastic! I would recommend it to everyone."
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Create the Gradio interface
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=analyze_sentiment,
|
| 29 |
+
inputs=input_text,
|
| 30 |
+
outputs=output_sentiment,
|
| 31 |
+
title="Sentiment Analyzer",
|
| 32 |
+
description="Enter text to analyze its sentiment (1 to 5 stars) using a BERT-based model.",
|
| 33 |
+
examples=examples,
|
| 34 |
+
theme="default" # Ensures a clean, responsive design
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Launch the interface
|
| 38 |
+
interface.launch()
|