Spaces:
Running
Running
File size: 1,661 Bytes
3598b04 1d72653 3598b04 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch
# Load model and tokenizer
model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
sentiments = ['Negative', 'Neutral', 'Positive']
result = {sentiments[i]: float(predictions[0][i]) for i in range(len(sentiments))}
return result
def custom_theme():
"""Define a custom theme for the Gradio app."""
return gr.Theme(
# Define your color scheme
primary='#FF6347',
text_on_primary='#FFFFFF',
background='#F0F8FF',
card_background='#FAEBD7',
text='#2F4F4F',
icon='light',
)
# Create Gradio interface
iface = gr.Interface(fn=predict_sentiment,
inputs=gr.Textbox(lines=2, placeholder="Type your sentence here..."),
outputs=gr.outputs.Label(num_top_classes=3),
theme=custom_theme(),
title="Sentiment Analysis",
description="Analyze the sentiment of your text.",
article="<p style='text-align: center'>Enter a sentence to get its sentiment. The model categorizes sentiments into Negative, Neutral, and Positive.</p>")
if __name__ == "__main__":
iface.launch()
|