fabiochiu commited on
Commit
5199728
1 Parent(s): 260bea6
Files changed (1) hide show
  1. main.py +53 -0
main.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ model = pipeline(task="sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
5
+
6
+ def sentiment_analysis(text):
7
+ res = model(text)[0]
8
+ res_label = {}
9
+ if res["label"] == "POSITIVE":
10
+ res_label["POSITIVE"] = res["score"]
11
+ res_label["NEGATIVE"] = 1 - res["score"]
12
+ if res["label"] == "NEGATIVE":
13
+ res_label["NEGATIVE"] = res["score"]
14
+ res_label["POSITIVE"] = 1 - res["score"]
15
+ return res_label
16
+
17
+ custom_css = """
18
+ #component-0 {
19
+ max-width: 600px;
20
+ margin: 0 auto;
21
+ }
22
+
23
+ h1,h2 {
24
+ text-align: center;
25
+ }
26
+
27
+ a {
28
+ color: #77b3ee !important;
29
+ text-decoration: none !important;
30
+ }
31
+
32
+ a:hover {
33
+ text-decoration: underline !important;
34
+ }
35
+ """
36
+
37
+ browser_tab_title = "Sentiment Analysis"
38
+ intro_markdown = """## Sentiment Analysis
39
+
40
+ Using the [distilbert-base-uncased-finetuned-sst-2-english](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english) model, trained on movie reviews."""
41
+
42
+ with gr.Blocks(title=browser_tab_title, css=custom_css) as demo:
43
+ with gr.Row():
44
+ with gr.Column():
45
+ title = gr.Markdown(intro_markdown)
46
+ text_input = gr.Textbox(placeholder="Enter a positive or negative sentence here...", label="Text")
47
+ label_output = gr.Label(label="Sentiment outcome")
48
+ button_run = gr.Button("Compute sentiment")
49
+ button_run.click(sentiment_analysis, inputs=text_input, outputs=label_output)
50
+ gr.Examples(["That's great!", "The movie was bad.", "How are you"], text_input)
51
+
52
+ if __name__ == "__main__":
53
+ demo.launch()