AnkitAI commited on
Commit
a6afdf6
·
verified ·
1 Parent(s): 2a836bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
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
- # 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()
 
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()