# -*- coding: utf-8 -*- """ Created on Mon Aug 25 14:10:33 2025 @author: shuangshuang """ from transformers import pipeline import gradio as gr # Load model classifier = pipeline( "sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment", device=-1, ) # Map labels label_map = { "1 star": "very bad", "2 stars": "bad", "3 stars": "neutral", "4 stars": "good", "5 stars": "very good", } # Prediction function def predict(text): text = text.strip() if not text: return "Please enter some text." result = classifier(text)[0] label = result["label"] score = result["score"] mapped = label_map[label] return f"Prediction: {mapped}\nConfidence: {score:.2f}" # Gradio interface interface = gr.Interface( fn=predict, inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."), outputs="text", title="Sentiment Analysis", description="Enter a sentence to analyze its sentiment.", examples=["I love this product!", "This is terrible."], ) # Launch (Hugging Face Spaces doesn't need special params) if __name__ == "__main__": interface.launch(ssr_mode=False)