|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
from transformers import RobertaForSequenceClassification, RobertaTokenizer |
|
import torch |
|
|
|
|
|
model_name = "AnkitAI/reviews-roberta-base-sentiment-analysis" |
|
model = RobertaForSequenceClassification.from_pretrained(model_name) |
|
tokenizer = RobertaTokenizer.from_pretrained(model_name) |
|
|
|
|
|
def predict_sentiment(text): |
|
inputs = tokenizer(text, return_tensors="pt") |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class = torch.argmax(logits, dim=1).item() |
|
sentiment = "Positive π" if predicted_class == 1 else "Negative π" |
|
return sentiment |
|
|
|
|
|
css = """ |
|
#component-0, #component-1 { |
|
font-size: 20px; |
|
} |
|
.gr-input, .gr-textbox, .gr-output, .gr-inputs, .gr-outputs { |
|
border-radius: 10px; |
|
border: 2px solid #4CAF50; |
|
} |
|
.gr-button { |
|
background-color: #4CAF50; |
|
color: white; |
|
border-radius: 10px; |
|
font-size: 16px; |
|
padding: 10px 24px; |
|
cursor: pointer; |
|
} |
|
.gr-button:hover { |
|
background-color: #45a049; |
|
} |
|
""" |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown("# Sentiment Analysis with RoBERTa") |
|
gr.Markdown("Analyze the sentiment of Amazon reviews using a fine-tuned RoBERTa model. Enter a review to see if it is positive or negative. **LABEL_1 = Positive** and **LABEL_0 = Negative**.") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
input_text = gr.Textbox(lines=5, placeholder="Enter a review...", label="Review Text") |
|
submit_btn = gr.Button("Analyze") |
|
with gr.Column(): |
|
output_text = gr.Textbox(label="Sentiment") |
|
|
|
submit_btn.click(predict_sentiment, inputs=input_text, outputs=output_text) |
|
|
|
demo.launch() |