✨ Uploaded core app files for real-time sentiment analysis
Browse filesIncludes app.py and requirements.txt using Gradio & Transformers
- app.py +42 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the sentiment classification model
|
| 5 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 6 |
+
|
| 7 |
+
# Define the function for Gradio interface
|
| 8 |
+
def classify_sentiment(text):
|
| 9 |
+
if not text.strip():
|
| 10 |
+
return "⚠️ Please enter a sentence to analyze.", ""
|
| 11 |
+
result = classifier(text)[0]
|
| 12 |
+
label = result['label']
|
| 13 |
+
confidence = round(result['score'] * 100, 2)
|
| 14 |
+
return f"**Prediction:** {label}", f"**Confidence:** {confidence}%"
|
| 15 |
+
|
| 16 |
+
# Create Gradio interface
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown("# 🔍 Real-Time Sentiment Classifier")
|
| 19 |
+
gr.Markdown("""
|
| 20 |
+
### 💡 Model Instruction:
|
| 21 |
+
This model is trained for binary sentiment classification — **Positive** and **Negative** only.
|
| 22 |
+
Neutral or mixed opinions may be interpreted as leaning toward one side.
|
| 23 |
+
For best results, input clearly positive or negative sentences.
|
| 24 |
+
""")
|
| 25 |
+
|
| 26 |
+
with gr.Row():
|
| 27 |
+
with gr.Column():
|
| 28 |
+
text_input = gr.Textbox(
|
| 29 |
+
label="Enter your sentence below 👇",
|
| 30 |
+
placeholder='Example: "I love the features of this app!" or "The update ruined the experience."',
|
| 31 |
+
lines=3
|
| 32 |
+
)
|
| 33 |
+
analyze_button = gr.Button("Analyze Sentiment")
|
| 34 |
+
|
| 35 |
+
with gr.Column():
|
| 36 |
+
prediction_output = gr.Markdown()
|
| 37 |
+
confidence_output = gr.Markdown()
|
| 38 |
+
|
| 39 |
+
analyze_button.click(fn=classify_sentiment, inputs=text_input, outputs=[prediction_output, confidence_output])
|
| 40 |
+
|
| 41 |
+
# Launch the app
|
| 42 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|