chjun commited on
Commit
b0ab40d
1 Parent(s): c869bf6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ id2label = {0: "NEGATIVE", 1: "POSITIVE"}
6
+ label2id = {"NEGATIVE": 0, "POSITIVE": 1}
7
+
8
+ # 导入在 HuggingFace 我们刚刚训练好而且上传成功的模型 chjun/my_awesome_model
9
+ classifier = pipeline("sentiment-analysis", model="chjun/my_awesome_model")
10
+
11
+ # inputs:输入文本
12
+ def predict(inputs):
13
+ label_score = classifier(inputs)
14
+ scaled = 0
15
+ if label_score[0]["label"] == "NEGATIVE":
16
+ scaled = 1 - label_score[0]["score"]
17
+ else:
18
+ scaled = label_score[0]["score"]
19
+
20
+ # 解码返回值得到输出
21
+ return round(scaled * 5)
22
+
23
+ with gr.Blocks() as demo:
24
+ review = gr.Textbox(label="用户评论。注:此模型只使用了英文数据 Finetune")
25
+ output = gr.Textbox(label="颗星")
26
+ submit_btn = gr.Button("提交")
27
+ submit_btn.click(fn=predict, inputs=review, outputs=output)
28
+
29
+ demo.launch(debug=True)