AnkitAI's picture
Update app.py
6943fba verified
# import gradio as gr
# gr.load("models/AnkitAI/reviews-roberta-base-sentiment-analysis").launch()
# import gradio as gr
# from transformers import RobertaForSequenceClassification, RobertaTokenizer
# # Load model and tokenizer
# model_name = "AnkitAI/reviews-roberta-base-sentiment-analysis"
# model = RobertaForSequenceClassification.from_pretrained(model_name)
# tokenizer = RobertaTokenizer.from_pretrained(model_name)
# # Define a function for prediction
# def predict_sentiment(text):
# inputs = tokenizer(text, return_tensors="pt")
# outputs = model(**inputs)
# logits = outputs.logits
# predicted_class = logits.argmax().item()
# sentiment = "positive" if predicted_class == 1 else "negative"
# return sentiment
# # Create a Gradio interface
# interface = gr.Interface(
# fn=predict_sentiment,
# inputs=gr.inputs.Textbox(lines=2, placeholder="Enter a review..."),
# outputs="text",
# title="Reviews Sentiment Analysis",
# description="Enter a review to analyze its sentiment. LABEL_1 = positive, LABEL_0 = negative."
# )
# # Launch the interface
# interface.launch()
import gradio as gr
from transformers import RobertaForSequenceClassification, RobertaTokenizer
import torch
# Load model and tokenizer
model_name = "AnkitAI/reviews-roberta-base-sentiment-analysis"
model = RobertaForSequenceClassification.from_pretrained(model_name)
tokenizer = RobertaTokenizer.from_pretrained(model_name)
# Define a function for prediction
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
# Customize CSS
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;
}
"""
# Create a Gradio interface
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()