import gradio as ui from transformers import pipeline # 1. Load the sentiment analysis pipeline # (Hugging Face Spaces will cache this so it only loads once on startup) pipe = pipeline( "text-classification", model="tabularisai/multilingual-sentiment-analysis" ) # 2. Define the prediction function def analyze_sentiment(text): if not text.strip(): return "Please enter some text to analyze." # Run the pipeline result = pipe(text)[0] # Extract label and score label = result["label"] score = result["score"] # Return a cleanly formatted string return f"Prediction: {label} | Confidence: {score:.2%}" # 3. Create the Gradio Interface demo = ui.Interface( fn=analyze_sentiment, inputs=ui.Textbox( lines=3, placeholder="Enter text here...", label="Input Text" ), outputs=ui.Textbox(label="Sentiment Analysis Result"), title="Multilingual Sentiment Analysis", description="Enter text in various languages to detect the underlying sentiment using the `tabularisai/multilingual-sentiment-analysis` model.", examples=[ ["I love this product! It's amazing and works perfectly."], ["Ce produit est terrible, je déteste ça."], ["Este producto es increíble y funciona a la perfección."], ], ) # 4. Launch the app if __name__ == "__main__": demo.launch()