Spaces:
Running
Running
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 f"Sentiment: {result['label']} | Confidence: {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="text", | |
examples=examples, | |
title="Tweet Sentiment Analyzer", | |
description="A simple Hugging Face Space to analyze sentiment in tweets using CardiffNLP's RoBERTa model." | |
) | |
if __name__ == "__main__": | |
demo.launch() | |