import gradio as gr from transformers import pipeline # Load Hugging Face sentiment model SENTIMENT_MODEL_NAME = "distilbert/distilbert-base-uncased-finetuned-sst-2-english" sentiment_classifier = pipeline("sentiment-analysis", model=SENTIMENT_MODEL_NAME) # Function for prediction def analyze_sentiment(text): if not text.strip(): return "⚠️ Please enter some text." result = sentiment_classifier(text)[0] label = result["label"] score = result["score"] # Color-coded emoji results if label == "POSITIVE": return f"😊 Positive ({score:.2f})" else: return f"😡 Negative ({score:.2f})" # Custom CSS to style the app custom_css = """ #component-0 textarea { font-size: 16px !important; border: 2px solid #4CAF50 !important; border-radius: 10px !important; padding: 10px !important; } button { background-color: #4CAF50 !important; color: white !important; font-size: 16px !important; border-radius: 8px !important; padding: 10px 20px !important; } """ # Gradio Interface demo = gr.Interface( fn=analyze_sentiment, inputs=gr.Textbox(lines=4, placeholder="Type your feedback here..."), outputs=gr.Textbox(label="Sentiment Result"), title="✨ AI Sentiment Analysis App", description="Enter text below to analyze its sentiment (Positive/Negative) powered by Hugging Face Transformers.", theme="default", css=custom_css ) if __name__ == "__main__": demo.launch()