File size: 1,538 Bytes
df6e58e
 
 
258a52c
df6e58e
 
258a52c
df6e58e
258a52c
 
df6e58e
258a52c
 
df6e58e
258a52c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df6e58e
258a52c
 
 
 
 
 
df6e58e
258a52c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
from transformers import pipeline

# Load the sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis")

# Function to process input text
def analyze_sentiment(text):
    if not text.strip():
        return "⚠️ Please enter some text."
    result = sentiment_pipeline(text)[0]
    label_emoji = "😊" if result["label"] == "POSITIVE" else "😞"
    return f"{label_emoji} **{result['label']}** (confidence: `{round(result['score'], 3)}`)"

# Create a more styled interface using Gradio Blocks
with gr.Blocks(title="Sentiment Analyzer") as demo:
    gr.Markdown(
        """
        # 🧠 Sentiment Analysis App
        _Enter a sentence to discover its emotional tone!_  
        Uses `distilbert-base-uncased-finetuned-sst-2-english` from Hugging Face πŸ€—
        ---
        """
    )
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                placeholder="Type something like 'I love this app!'",
                label="Your Text",
                lines=3
            )
            submit_btn = gr.Button("πŸ” Analyze")
        with gr.Column():
            output = gr.Markdown(label="Sentiment Result")

    submit_btn.click(fn=analyze_sentiment, inputs=input_text, outputs=output)

    gr.Markdown("---")
    gr.Markdown("Made with ❀️ using [Gradio](https://gradio.app) and [Hugging Face Transformers](https://huggingface.co./transformers/)")

# For local testing; not required for Spaces
if __name__ == "__main__":
    demo.launch()