File size: 2,874 Bytes
4f1a3c2
e03c695
4f1a3c2
e03c695
e25ecd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6afdf6
 
e25ecd5
e03c695
a6afdf6
 
 
 
e03c695
a6afdf6
 
 
 
 
e25ecd5
 
a6afdf6
e03c695
e25ecd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6afdf6
6943fba
 
 
 
 
 
 
 
 
 
8dcece9
6943fba
f528541
6943fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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()