Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the sentiment analysis pipeline
|
| 5 |
+
sentiment_pipeline = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 6 |
+
|
| 7 |
+
def analyze_sentiment(text):
|
| 8 |
+
if not text.strip():
|
| 9 |
+
return "Please enter some text to analyze."
|
| 10 |
+
|
| 11 |
+
result = sentiment_pipeline(text)[0]
|
| 12 |
+
return f"Predicted Sentiment: {result['label']} stars"
|
| 13 |
+
|
| 14 |
+
# Define the Gradio interface
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
gr.Markdown("# Sentiment Analysis using BERT Model")
|
| 17 |
+
gr.Markdown("Enter a sentence or paragraph below and click 'Analyze' to get the predicted sentiment (1 to 5 stars).")
|
| 18 |
+
|
| 19 |
+
text_input = gr.Textbox(label="Input Text", placeholder="Enter your text here...", lines=3)
|
| 20 |
+
analyze_button = gr.Button("Analyze Sentiment")
|
| 21 |
+
output_text = gr.Textbox(label="Predicted Sentiment", interactive=False)
|
| 22 |
+
|
| 23 |
+
examples = [
|
| 24 |
+
"I love this product! It's amazing!",
|
| 25 |
+
"This was the worst experience I've ever had.",
|
| 26 |
+
"The movie was okay, not great but not bad either.",
|
| 27 |
+
"Absolutely fantastic! I would recommend it to everyone."
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
gr.Examples(examples=examples, inputs=text_input)
|
| 31 |
+
analyze_button.click(analyze_sentiment, inputs=text_input, outputs=output_text)
|
| 32 |
+
|
| 33 |
+
# Launch the Gradio app
|
| 34 |
+
demo.launch()
|