Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
def analyze_sentiment(text):
|
5 |
+
sentiment_analyzer = pipeline(
|
6 |
+
"sentiment-analysis",
|
7 |
+
model="nlptown/bert-base-multilingual-uncased-sentiment"
|
8 |
+
)
|
9 |
+
result = sentiment_analyzer(text, return_all_scores=True)
|
10 |
+
score = int(result[0]['score'] * 5) # Convert probability to 5-star scale
|
11 |
+
stars = "⭐" * score
|
12 |
+
|
13 |
+
return stars
|
14 |
+
|
15 |
+
with gr.Blocks() as demo:
|
16 |
+
gr.Markdown("## Sentiment Analysis Demo")
|
17 |
+
with gr.Row():
|
18 |
+
input_text = gr.Textbox(
|
19 |
+
label="Enter your text here",
|
20 |
+
placeholder="Type or paste your text...",
|
21 |
+
lines=3,
|
22 |
+
examples=[
|
23 |
+
"I love this product! It's amazing!",
|
24 |
+
"This was the worst experience I've ever had.",
|
25 |
+
"The movie was okay, not great but not bad either.",
|
26 |
+
"Absolutely fantastic! I would recommend it to everyone."
|
27 |
+
]
|
28 |
+
)
|
29 |
+
with gr.Row():
|
30 |
+
analyze_button = gr.Button("Analyze Sentiment", variant="primary")
|
31 |
+
with gr.Row():
|
32 |
+
output_text = gr.Textbox(
|
33 |
+
label="Sentiment (Stars)",
|
34 |
+
lines=1
|
35 |
+
)
|
36 |
+
|
37 |
+
analyze_button.click(
|
38 |
+
fn=analyze_sentiment,
|
39 |
+
inputs=input_text,
|
40 |
+
outputs=output_text
|
41 |
+
)
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
demo.launch()
|