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