Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,35 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
|
3 |
+
# gr.load("models/AnkitAI/reviews-roberta-base-sentiment-analysis").launch()
|
4 |
+
|
5 |
import gradio as gr
|
6 |
+
from transformers import RobertaTokenizer, RobertaForSequenceClassification
|
7 |
+
import torch
|
8 |
+
|
9 |
+
# Load the model and tokenizer
|
10 |
+
model_name = "AnkitAI/reviews-roberta-base-sentiment-analysis"
|
11 |
+
model = RobertaForSequenceClassification.from_pretrained(model_name)
|
12 |
+
tokenizer = RobertaTokenizer.from_pretrained(model_name)
|
13 |
+
|
14 |
+
# Define a function to perform sentiment analysis and map labels
|
15 |
+
def predict_sentiment(text):
|
16 |
+
inputs = tokenizer(text, return_tensors="pt")
|
17 |
+
outputs = model(**inputs)
|
18 |
+
logits = outputs.logits
|
19 |
+
predicted_class_id = torch.argmax(logits, dim=1).item()
|
20 |
+
|
21 |
+
# Map class id to label
|
22 |
+
labels = ["Negative", "Positive"]
|
23 |
+
return labels[predicted_class_id]
|
24 |
+
|
25 |
+
# Create a Gradio interface
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=predict_sentiment,
|
28 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter a review here..."),
|
29 |
+
outputs="text",
|
30 |
+
title="Reviews Sentiment Analysis",
|
31 |
+
description="Enter an Amazon review to see if it is positive or negative."
|
32 |
+
)
|
33 |
|
34 |
+
# Launch the interface
|
35 |
+
interface.launch()
|