Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
# 定义推理函数
|
9 |
def predict(text):
|
10 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
-
# 使用
|
15 |
with gr.Blocks() as demo:
|
16 |
-
# 设置页面标题
|
17 |
gr.Markdown("<h1 style='text-align: center;'>欢迎使用 HaS-820m 模型预测</h1>")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
with gr.Column(): # Column 用于垂直布局
|
22 |
input_text = gr.Textbox(label="输入文本", placeholder="请输入文本进行预测...", lines=4)
|
23 |
output_text = gr.Textbox(label="预测结果", interactive=False)
|
24 |
|
25 |
-
# 添加一个按钮
|
26 |
with gr.Row():
|
27 |
predict_btn = gr.Button("进行预测")
|
28 |
|
29 |
-
# 定义按钮点击后的行为
|
30 |
predict_btn.click(predict, inputs=input_text, outputs=output_text)
|
31 |
|
32 |
# 可选:自定义样式
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# 加载模型和分词器
|
6 |
+
model_name = "SecurityXuanwuLab/HaS-820m"
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
# 定义推理函数
|
11 |
def predict(text):
|
12 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits
|
16 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
17 |
+
return f"预测类别:{predicted_class}"
|
18 |
|
19 |
+
# 使用 Gradio Blocks 构建界面
|
20 |
with gr.Blocks() as demo:
|
|
|
21 |
gr.Markdown("<h1 style='text-align: center;'>欢迎使用 HaS-820m 模型预测</h1>")
|
22 |
|
23 |
+
with gr.Row():
|
24 |
+
with gr.Column():
|
|
|
25 |
input_text = gr.Textbox(label="输入文本", placeholder="请输入文本进行预测...", lines=4)
|
26 |
output_text = gr.Textbox(label="预测结果", interactive=False)
|
27 |
|
|
|
28 |
with gr.Row():
|
29 |
predict_btn = gr.Button("进行预测")
|
30 |
|
|
|
31 |
predict_btn.click(predict, inputs=input_text, outputs=output_text)
|
32 |
|
33 |
# 可选:自定义样式
|