Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
# Load
|
5 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
6 |
|
7 |
-
#
|
8 |
def analyze_sentiment(text):
|
|
|
|
|
9 |
result = sentiment_pipeline(text)[0]
|
10 |
-
|
|
|
11 |
|
12 |
-
# Create Gradio
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
if __name__ == "__main__":
|
21 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load the sentiment analysis pipeline
|
5 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
6 |
|
7 |
+
# Function to process input text
|
8 |
def analyze_sentiment(text):
|
9 |
+
if not text.strip():
|
10 |
+
return "⚠️ Please enter some text."
|
11 |
result = sentiment_pipeline(text)[0]
|
12 |
+
label_emoji = "😊" if result["label"] == "POSITIVE" else "😞"
|
13 |
+
return f"{label_emoji} **{result['label']}** (confidence: `{round(result['score'], 3)}`)"
|
14 |
|
15 |
+
# Create a more styled interface using Gradio Blocks
|
16 |
+
with gr.Blocks(title="Sentiment Analyzer") as demo:
|
17 |
+
gr.Markdown(
|
18 |
+
"""
|
19 |
+
# 🧠 Sentiment Analysis App
|
20 |
+
_Enter a sentence to discover its emotional tone!_
|
21 |
+
Uses `distilbert-base-uncased-finetuned-sst-2-english` from Hugging Face 🤗
|
22 |
+
---
|
23 |
+
"""
|
24 |
+
)
|
25 |
+
|
26 |
+
with gr.Row():
|
27 |
+
with gr.Column():
|
28 |
+
input_text = gr.Textbox(
|
29 |
+
placeholder="Type something like 'I love this app!'",
|
30 |
+
label="Your Text",
|
31 |
+
lines=3
|
32 |
+
)
|
33 |
+
submit_btn = gr.Button("🔍 Analyze")
|
34 |
+
with gr.Column():
|
35 |
+
output = gr.Markdown(label="Sentiment Result")
|
36 |
|
37 |
+
submit_btn.click(fn=analyze_sentiment, inputs=input_text, outputs=output)
|
38 |
+
|
39 |
+
gr.Markdown("---")
|
40 |
+
gr.Markdown("Made with ❤️ using [Gradio](https://gradio.app) and [Hugging Face Transformers](https://huggingface.co/transformers/)")
|
41 |
+
|
42 |
+
# For local testing; not required for Spaces
|
43 |
if __name__ == "__main__":
|
44 |
+
demo.launch()
|