import gradio as gr from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline # Load model MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest" tokenizer = AutoTokenizer.from_pretrained(MODEL) model = AutoModelForSequenceClassification.from_pretrained(MODEL) sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) # Function for Gradio def analyze_sentiment(text): result = sentiment_model(text)[0] return { "Sentiment": result["label"], "Confidence": f"{result['score']:.2f}" } # Example texts examples = [ ["I absolutely love this new phone, the camera is stunning!"], ["I hate the way this app keeps crashing."], ["It’s fine, nothing special but not terrible either."], ] # Gradio UI demo = gr.Interface( fn=analyze_sentiment, inputs=gr.Textbox(lines=3, placeholder="Type a sentence here..."), outputs="label", examples=examples, title="Sentiment Analyzer", description="" ) if __name__ == "__main__": demo.launch()