AnkitAI's picture
Update app.py
e03c695 verified
raw
history blame
1.15 kB
# import gradio as gr
# gr.load("models/AnkitAI/reviews-roberta-base-sentiment-analysis").launch()
import gradio as gr
from transformers import RobertaTokenizer, RobertaForSequenceClassification
import torch
# Load the 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 to perform sentiment analysis and map labels
def predict_sentiment(text):
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
predicted_class_id = torch.argmax(logits, dim=1).item()
# Map class id to label
labels = ["Negative", "Positive"]
return labels[predicted_class_id]
# Create a Gradio interface
interface = gr.Interface(
fn=predict_sentiment,
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter a review here..."),
outputs="text",
title="Reviews Sentiment Analysis",
description="Enter an Amazon review to see if it is positive or negative."
)
# Launch the interface
interface.launch()