Commit
·
8c5cc7e
1
Parent(s):
8cfc0f9
Upload 2 files
Browse files- app.py +36 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
tokenizer_review_feedback_sentiment = AutoTokenizer.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
|
6 |
+
model_review_feedback_sentiment = AutoModelForSequenceClassification.from_pretrained('nlptown/bert-base-multilingual-uncased-sentiment')
|
7 |
+
|
8 |
+
|
9 |
+
def review_feedback_sentiment(text, tokenizer, model):
|
10 |
+
inputs = tokenizer.encode_plus(text, padding='max_length', max_length=512, return_tensors="pt")
|
11 |
+
with torch.no_grad():
|
12 |
+
result = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
13 |
+
logits = result.logits.detach()
|
14 |
+
probs = torch.softmax(logits, dim=1).detach().numpy()[0]
|
15 |
+
categories = ['Terrible', 'Poor', 'Average', 'Good', 'Excellent']
|
16 |
+
output_dict = {}
|
17 |
+
for i in range(len(categories)):
|
18 |
+
output_dict[categories[i]] = [round(float(probs[i]), 2)]
|
19 |
+
return output_dict
|
20 |
+
|
21 |
+
def review_feed_back(text):
|
22 |
+
result = review_feedback_sentiment(text,tokenizer_review_feedback_sentiment,model_review_feedback_sentiment)
|
23 |
+
return result
|
24 |
+
|
25 |
+
with gr.Blocks(title="Feedback",css="footer {visibility: hidden}") as demo:
|
26 |
+
with gr.Row():
|
27 |
+
with gr.Column():
|
28 |
+
gr.Markdown("## Review Feedback sentiment")
|
29 |
+
with gr.Row():
|
30 |
+
with gr.Column():
|
31 |
+
inputs = gr.TextArea(label="sentence",value="I'm so impressed with your product! It's exactly what I needed and it's working great.",interactive=True)
|
32 |
+
btn = gr.Button(value="RUN")
|
33 |
+
with gr.Column():
|
34 |
+
output = gr.Label(label="output")
|
35 |
+
btn.click(fn=review_feed_back,inputs=[inputs],outputs=[output])
|
36 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.32.0
|
2 |
+
torch==2.0.0
|
3 |
+
transformers==4.28.1
|