ppw2004 commited on
Commit
9f5355a
·
verified ·
1 Parent(s): b850426

Upload folder using huggingface_hub

Browse files
.ipynb_checkpoints/app-checkpoint.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # 【修改1】设置 HF 镜像,防止下载分词器超时
3
+ os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
4
+ import gradio as gr
5
+ import torch
6
+ from transformers import BertTokenizer, BertForSequenceClassification
7
+ import torch.nn.functional as F
8
+
9
+
10
+ # 1. 配置路径
11
+ # 注意:这里需要改为你运行main.py后生成的模型文件夹路径
12
+ # 如果你是在本地运行,可能是 "./experiments" 或者具体的checkpoint文件夹
13
+ model_path = "./experiments"
14
+ base_model_name = "bert-base-uncased"
15
+
16
+ # 2. 加载模型和分词器
17
+ # 加载分词器 (通常使用基础模型的tokenizer)
18
+ tokenizer = BertTokenizer.from_pretrained(base_model_name)
19
+
20
+ # 加载你微调后的模型
21
+ # 注意:确保你的模型文件夹里有 config.json 和 pytorch_model.bin
22
+ model = BertForSequenceClassification.from_pretrained(model_path, num_labels=2)
23
+ model.eval() # 切换到评估模式
24
+
25
+ # 3. 定义预测函数
26
+ def predict(text):
27
+ # 数据预处理
28
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
29
+
30
+ # 模型推理
31
+ with torch.no_grad():
32
+ outputs = model(**inputs)
33
+ logits = outputs.logits
34
+
35
+ # 获取概率 (Softmax)
36
+ probabilities = F.softmax(logits, dim=1)
37
+
38
+ # 构建返回结果 (假设是二分类:0-Real, 1-Fake,根据你的实际标签调整)
39
+ # labels 需要和你训练时的标签对应
40
+ labels = ["Real News", "Fake News"]
41
+
42
+ # Gradio 需要返回一个 {label: probability} 的字典
43
+ return {labels[i]: float(probabilities[0][i]) for i in range(len(labels))}
44
+
45
+ # 4. 创建 Gradio 界面
46
+ iface = gr.Interface(
47
+ fn=predict,
48
+ inputs=gr.Textbox(lines=2, placeholder="请输入一段新闻文本..."),
49
+ outputs=gr.Label(num_top_classes=2),
50
+ title="虚假新闻检测 Demo",
51
+ description="输入新闻文本,模型将判断其真伪。",
52
+ examples=[["这里可以填入一些测试用的示例文本"]]
53
+ )
54
+
55
+ # 5. 启动应用
56
+ if __name__ == "__main__":
57
+ iface.launch(share=True)
.ipynb_checkpoints/requirements-checkpoint.txt ADDED
File without changes
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ # 【修改1】设置 HF 镜像,防止下载分词器超时
3
+ os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
4
+ import gradio as gr
5
+ import torch
6
+ from transformers import BertTokenizer, BertForSequenceClassification
7
+ import torch.nn.functional as F
8
+
9
+
10
+ # 1. 配置路径
11
+ # 注意:这里需要改为你运行main.py后生成的模型文件夹路径
12
+ # 如果你是在本地运行,可能是 "./experiments" 或者具体的checkpoint文件夹
13
+ model_path = "./experiments"
14
+ base_model_name = "bert-base-uncased"
15
+
16
+ # 2. 加载模型和分词器
17
+ # 加载分词器 (通常使用基础模型的tokenizer)
18
+ tokenizer = BertTokenizer.from_pretrained(base_model_name)
19
+
20
+ # 加载你微调后的模型
21
+ # 注意:确保你的模型文件夹里有 config.json 和 pytorch_model.bin
22
+ model = BertForSequenceClassification.from_pretrained(model_path, num_labels=2)
23
+ model.eval() # 切换到评估模式
24
+
25
+ # 3. 定义预测函数
26
+ def predict(text):
27
+ # 数据预处理
28
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
29
+
30
+ # 模型推理
31
+ with torch.no_grad():
32
+ outputs = model(**inputs)
33
+ logits = outputs.logits
34
+
35
+ # 获取概率 (Softmax)
36
+ probabilities = F.softmax(logits, dim=1)
37
+
38
+ # 构建返回结果 (假设是二分类:0-Real, 1-Fake,根据你的实际标签调整)
39
+ # labels 需要和你训练时的标签对应
40
+ labels = ["Real News", "Fake News"]
41
+
42
+ # Gradio 需要返回一个 {label: probability} 的字典
43
+ return {labels[i]: float(probabilities[0][i]) for i in range(len(labels))}
44
+
45
+ # 4. 创建 Gradio 界面
46
+ iface = gr.Interface(
47
+ fn=predict,
48
+ inputs=gr.Textbox(lines=2, placeholder="请输入一段新闻文本..."),
49
+ outputs=gr.Label(num_top_classes=2),
50
+ title="虚假新闻检测 Demo",
51
+ description="输入新闻文本,模型将判断其真伪。",
52
+ examples=[["这里可以填入一些测试用的示例文本"]]
53
+ )
54
+
55
+ # 5. 启动应用
56
+ if __name__ == "__main__":
57
+ iface.launch(share=True)
experiments/config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "bert-base-uncased",
3
+ "architectures": [
4
+ "BertForSequenceClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "classifier_dropout": null,
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 3072,
14
+ "layer_norm_eps": 1e-12,
15
+ "max_position_embeddings": 512,
16
+ "model_type": "bert",
17
+ "num_attention_heads": 12,
18
+ "num_hidden_layers": 12,
19
+ "pad_token_id": 0,
20
+ "position_embedding_type": "absolute",
21
+ "problem_type": "single_label_classification",
22
+ "torch_dtype": "float32",
23
+ "transformers_version": "4.44.2",
24
+ "type_vocab_size": 2,
25
+ "use_cache": true,
26
+ "vocab_size": 30522
27
+ }
experiments/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:83c9722a969a7ea6a886fb57e99583099f33c86efca0123f135ced27f27ca9ea
3
+ size 438003950
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 核心库:锁定版本以避免那个 torch.load 安全报错
2
+ transformers==4.44.2
3
+
4
+ # 深度学习框架
5
+ torch>=2.0.0
6
+
7
+ # Web界面库
8
+ gradio
9
+
10
+ # 其他常用依赖 (通常 transformers 需要)
11
+ accelerate
12
+ safetensors