Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,29 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="chenglu/my_awesome_model")
|
10 |
+
|
11 |
+
# input:输入文本
|
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, api_name="predict")
|
28 |
+
|
29 |
+
demo.launch(debug=True)
|