s2049's picture
Update app.py
ae02fae verified
raw
history blame
1.38 kB
import gradio as gr
from transformers import pipeline
def analyze_sentiment(text):
sentiment_analyzer = pipeline(
"sentiment-analysis",
model="nlptown/bert-base-multilingual-uncased-sentiment"
)
result = sentiment_analyzer(text, return_all_scores=True)
score = int(result[0]['score'] * 5)
sentiment_stars = "⭐" * score
return sentiment_stars
with gr.Blocks() as demo:
gr.Markdown("## Sentiment Analysis Demo")
gr.Markdown("##### Example Inputs:")
gr.Markdown("- \"I love this product! It's amazing!\"")
gr.Markdown("- \"This was the worst experience I've ever had.\"")
gr.Markdown("- \"The movie was okay, not great but not bad either.\"")
gr.Markdown("- \"Absolutely fantastic! I would recommend it to everyone.\"")
with gr.Row():
input_text = gr.Textbox(
label="Enter your text here",
placeholder="Type or paste your text...",
lines=3
)
with gr.Row():
analyze_button = gr.Button("Analyze Sentiment", variant="primary")
with gr.Row():
output_text = gr.Textbox(
label="Sentiment (Stars)",
lines=1
)
analyze_button.click(
fn=analyze_sentiment,
inputs=input_text,
outputs=output_text
)
if __name__ == "__main__":
demo.launch()