LukeJacob2023 commited on
Commit
c8f549e
·
verified ·
1 Parent(s): 892bc21

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import BertTokenizer, BertForSequenceClassification
4
+
5
+ model_path = "LukeJacob2023/religion-classifier"
6
+
7
+ # 分类名称
8
+ labels = ["基督教", "佛教", "无信仰"]
9
+ # 1. 加载tokenizer和模型
10
+ tokenizer = BertTokenizer.from_pretrained(model_path)
11
+ model = BertForSequenceClassification.from_pretrained(model_path)
12
+
13
+ # 确保模型在评估模式
14
+ model.eval()
15
+
16
+
17
+ def predict(text):
18
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
19
+ with torch.no_grad():
20
+ outputs = model(**inputs)
21
+ probabilities = torch.nn.functional.softmax(outputs.logits / 5.0, dim=-1)[0]
22
+ return {label: float(prob) for label, prob in zip(labels, probabilities)}
23
+
24
+
25
+ # 创建Gradio接口
26
+ iface = gr.Interface(
27
+ fn=predict,
28
+ inputs=gr.Textbox(lines=2, label="Input Text"),
29
+ outputs=gr.Label(num_top_classes=3, label="Predictions", flagged=False),
30
+ title="Religion Classification",
31
+ description="请输入内容(繁体中文)"
32
+ )
33
+
34
+ # 启动Gradio应用
35
+ iface.launch()