File size: 1,582 Bytes
f531dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

# 加载模型和分词器
model_name = "SecurityXuanwuLab/HaS-820m"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# 定义推理函数
def predict(text):
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    with torch.no_grad():
        outputs = model(**inputs)
    logits = outputs.logits
    predicted_class = torch.argmax(logits, dim=-1).item()
    return f"预测类别:{predicted_class}"

theme = gr.themes.Monochrome(
    primary_hue="stone",
    secondary_hue="stone",
    neutral_hue="stone",
).set(
    body_background_fill='*secondary_200',
    body_background_fill_dark='*secondary_200',
    body_text_color='*secondary_950',
    body_text_color_dark='*secondary_200',
    body_text_color_subdued='*secondary_500',
    body_text_color_subdued_dark='*secondary_500'
)

# 使用 Gradio 的内置主题构建界面
with gr.Blocks(theme=theme) as demo:
    gr.Markdown("<h1 style='text-align: center;'>模型试用:腾讯玄武</h1>")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(label="输入文本", placeholder="请输入文本处理...", lines=4)
            output_text = gr.Textbox(label="处理结果", interactive=False)
    
    with gr.Row():
        predict_btn = gr.Button("进行处理")
    
    predict_btn.click(predict, inputs=input_text, outputs=output_text)

# 启动界面
demo.launch()