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()