import gradio as gr from transformers import pipeline # Initialize the sentiment-analysis pipeline classifier = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english") def respond(message): # Get the sentiment analysis result sentiment_result = classifier(message) # Process the result to extract the sentiment label and score sentiment_label = sentiment_result[0]['label'] sentiment_score = sentiment_result[0]['score'] # Generate a response based on the sentiment analysis response = f"Sentiment: {sentiment_label} (Confidence: {sentiment_score * 100:.2f}%)" return response demo = gr.Interface( fn=respond, inputs=gr.Textbox(label="Input Text"), outputs=gr.Textbox(label="Sentiment Analysis Result"), allow_flagging="never", # Disable flagging (optional, but good for API usage) ) if __name__ == "__main__": demo.launch(share=True) # Use share=True to expose the interface publicly