File size: 2,028 Bytes
1bd94fb
2fce71e
 
1bd94fb
159e685
2fce71e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5a04a6
2fce71e
 
e5a04a6
2fce71e
 
 
 
 
 
 
 
 
e5a04a6
2fce71e
 
 
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
import gradio as gr
import torch
from transformers import pipeline

model = pipeline(task="sentiment-analysis", model="tkurtulus/TurkishAirlines-SentimentAnalysisModel")

def sentiment_analysis(text):
    res = model(text)[0]
    res_label = {}
    if res["label"] == "positive":
        res_label["positive"] = res["score"]
        res_label["negative"] = 1 - res["score"]
        res_label["neutral"] = 1 - res["score"]
    if res["label"] == "negative":
        res_label["negative"] = res["score"]
        res_label["positive"] = 1 - res["score"]
        res_label["neutral"] = 1 - res["score"]
    if res["label"] == "neutral":
        res_label["neutral"] = res["score"]
        res_label["positive"] = 1 - res["score"]
        res_label["negative"] = 1 - res["score"]
    return res_label

custom_css = """
#component-0 {
    max-width: 600px;
    margin: 0 auto;
}

h1,h2 {
    text-align: center;
}

a {
    color: #77b3ee !important;
    text-decoration: none !important;
}

a:hover {
    text-decoration: underline !important;
}
"""

browser_tab_title = "Turkish Airlines Customer Reviews Sentiment Analysis"
intro_markdown = """## Sentiment Analysis

Using the [TurkishAirlines-SentimentAnalysisModel](https://huggingface.co/tkurtulus/TurkishAirlines-SentimentAnalysisModel), trained on Twitter customer reviews with 3 sentiment levels (positive, negative, neutral)."""

with gr.Blocks(title=browser_tab_title, css=custom_css) as demo:
    with gr.Row():
        with gr.Column():
            title = gr.Markdown(intro_markdown)
            text_input = gr.Textbox(placeholder="Enter a positive or negative sentence here...", label="Text")
            label_output = gr.Label(label="Sentiment outcome")
            button_run = gr.Button("Compute sentiment")
            button_run.click(sentiment_analysis, inputs=text_input, outputs=label_output)
            gr.Examples(["Such a great experience!", "My luggage was lost.", "Food service was outstanding"], text_input)

if __name__ == "__main__":
    demo.launch()