File size: 4,499 Bytes
fa4bc6d
 
ce799e4
fa4bc6d
ce799e4
 
 
fa4bc6d
 
ce799e4
 
 
 
 
 
fa4bc6d
 
863cce8
ce799e4
 
 
 
 
0e8ac48
 
fa4bc6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce799e4
fa4bc6d
 
ce799e4
fa4bc6d
863cce8
fa4bc6d
ce799e4
fa4bc6d
 
ce799e4
 
 
fa4bc6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce799e4
fa4bc6d
 
 
 
 
 
 
ce799e4
02bf6a8
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from pathlib import Path
from collections import Counter
import gradio as gr
import pandas as pd
import numpy
from scipy.special import softmax

import torch
from transformers import pipeline, TextClassificationPipeline
from transformers import (
    AutoTokenizer,
    AutoConfig,
    AutoModelForSequenceClassification
)

# model_path = "EmotiScan/amazon-comments-bert" # To use your model, on line 55, you'll have to change i to i+1
# model_path = "nlptown/bert-base-multilingual-uncased-sentiment"
model_path = "Mofe/emotiscan_model_2"

tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)


description = f"""# <h2 style="text-align: center;"> Opinion orbit Amazon Products Review</h2>
<p style="text-align: center;font-size:15px;"> Input a single review to predict sentiment or multiple from a text file.</p>
"""

sample_reviews = [
    "A comfortable pair of boots, but a little difficult to \
    remove without unlacing. So be prepared to unlace before \
    removing. Takes a few seconds to get them on and off, but \
        otherwise a very comfortable, sturdy product.",
    "Very nice boot, it's narrow whc I wasn't expecting, more \
     for a not so wide of a foot. My foot is not that wide, but \
    with socks on it will feel a little tight in the middle and back \
     of the heel, I would go a half or whole size up.",
    "If you like to continue having dry skin AND to smell like \
    the inside of a Shoppers Drug Mart then this product is for you!! \
     Sadly, I don't care for either of those things. I will continue \
     to hunt for a moisturizer/shower oil",
    "I really dislike this product.",
    "This is the best product ever!."
]

def upload_file(file):
    return file.name

def get_sentiments(text):
    encoded_input = tokenizer(text, return_tensors='pt')
    with torch.no_grad():
        logits = model(**encoded_input).logits

    logits_list = logits[0].tolist()
    logits_labels = [model.config.id2label[i]
                     for i in range(0, len(logits_list))]

    probs = softmax(logits_list)
    scores = {l:float(s) for (l,s) in zip(logits_labels, probs)}

    return scores

def get_multiple_sentiments(input_file):
    with open(input_file) as fn:
        reviews = fn.readlines()
        reviews = [review.strip() for review in reviews]

    classifier = pipeline('text-classification',
                          model=model_path)
    scores = classifier(reviews)
    scores = [pred['label'] for pred in scores]

    preds = dict(Counter(scores))
    labels = preds.keys()
    counts = preds.values()
    review_counts = pd.DataFrame(
    {
        "labels": labels,
        "counts": counts,
    }
    )
    total = len(scores)

    return gr.BarPlot(
            review_counts,
            x="labels",
            y="counts",
            title=f"# of Reviews: {total}",
            tooltip=["labels", "counts"],
            y_lim=[0, total + 10],
            min_width=500
        )

with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown(value=description)

    with gr.Row():
        with gr.Column():
            with gr.Tab("Single Input"):
                single = True
                input_text = gr.Textbox(label="Input Text",
                                placeholder="Input the product review...")
                predict_btn = gr.Button("Get Sentiment")
                with gr.Accordion("Here are some sample reviews!"):
                    examples = gr.Examples(examples=sample_reviews,
                                           inputs=[input_text])
            with gr.Tab("File Upload"):
                multiple = True
                file_output = gr.File()
                upload_button = gr.UploadButton("Upload a .txt file with each review on a line.",
                                                file_types=["text"])
                upload_button.upload(upload_file, upload_button, file_output)
                file_predict_btn = gr.Button("Get Sentiments")

        with gr.Column():
            output = gr.Label(label="Single Prediction")
            plots = gr.BarPlot(label="Plot Multiple Reviews")

    predict_btn.click(fn=get_sentiments,
                    inputs=input_text,
                    outputs=output,)
                    # api_name="product_review")
    file_predict_btn.click(fn=get_multiple_sentiments,
                    inputs=file_output,
                    outputs=plots)

demo.launch(share=True)