Str0keOOOO commited on
Commit
e289622
·
verified ·
1 Parent(s): 7bf8cbc

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +185 -182
README.md CHANGED
@@ -12,197 +12,200 @@ license: mit
12
  base_model:
13
  - deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
14
  ---
15
- # Model Card for Model ID
16
 
17
- <!-- Provide a quick summary of what the model is/does. -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- This modelcard aims to be a base template for new models. It has been generated using [this raw template](https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/modelcard_template.md?plain=1).
20
 
21
- ## Model Details
 
 
22
 
23
- ### Model Description
 
 
 
 
 
 
24
 
25
- <!-- Provide a longer summary of what this model is. -->
26
 
 
 
 
27
 
 
28
 
29
- - **Developed by:** [More Information Needed]
30
- - **Funded by [optional]:** [More Information Needed]
31
- - **Shared by [optional]:** [More Information Needed]
32
- - **Model type:** [More Information Needed]
33
- - **Language(s) (NLP):** [More Information Needed]
34
- - **License:** [More Information Needed]
35
- - **Finetuned from model [optional]:** [More Information Needed]
36
 
37
- ### Model Sources [optional]
38
 
39
- <!-- Provide the basic links for the model. -->
40
 
41
- - **Repository:** [More Information Needed]
42
- - **Paper [optional]:** [More Information Needed]
43
- - **Demo [optional]:** [More Information Needed]
44
 
45
- ## Uses
46
-
47
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
48
-
49
- ### Direct Use
50
-
51
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
52
-
53
- [More Information Needed]
54
-
55
- ### Downstream Use [optional]
56
-
57
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
58
-
59
- [More Information Needed]
60
-
61
- ### Out-of-Scope Use
62
-
63
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
64
-
65
- [More Information Needed]
66
-
67
- ## Bias, Risks, and Limitations
68
-
69
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
70
-
71
- [More Information Needed]
72
-
73
- ### Recommendations
74
-
75
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
76
-
77
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
78
-
79
- ## How to Get Started with the Model
80
-
81
- Use the code below to get started with the model.
82
-
83
- [More Information Needed]
84
-
85
- ## Training Details
86
-
87
- ### Training Data
88
-
89
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
90
-
91
- [More Information Needed]
92
-
93
- ### Training Procedure
94
-
95
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
96
-
97
- #### Preprocessing [optional]
98
-
99
- [More Information Needed]
100
-
101
-
102
- #### Training Hyperparameters
103
-
104
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
105
-
106
- #### Speeds, Sizes, Times [optional]
107
-
108
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
109
-
110
- [More Information Needed]
111
-
112
- ## Evaluation
113
-
114
- <!-- This section describes the evaluation protocols and provides the results. -->
115
-
116
- ### Testing Data, Factors & Metrics
117
-
118
- #### Testing Data
119
-
120
- <!-- This should link to a Dataset Card if possible. -->
121
-
122
- [More Information Needed]
123
-
124
- #### Factors
125
-
126
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
127
-
128
- [More Information Needed]
129
-
130
- #### Metrics
131
-
132
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
133
-
134
- [More Information Needed]
135
-
136
- ### Results
137
-
138
- [More Information Needed]
139
-
140
- #### Summary
141
-
142
-
143
-
144
- ## Model Examination [optional]
145
-
146
- <!-- Relevant interpretability work for the model goes here -->
147
-
148
- [More Information Needed]
149
-
150
- ## Environmental Impact
151
-
152
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
153
-
154
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
155
-
156
- - **Hardware Type:** [More Information Needed]
157
- - **Hours used:** [More Information Needed]
158
- - **Cloud Provider:** [More Information Needed]
159
- - **Compute Region:** [More Information Needed]
160
- - **Carbon Emitted:** [More Information Needed]
161
-
162
- ## Technical Specifications [optional]
163
-
164
- ### Model Architecture and Objective
165
-
166
- [More Information Needed]
167
-
168
- ### Compute Infrastructure
169
-
170
- [More Information Needed]
171
-
172
- #### Hardware
173
-
174
- [More Information Needed]
175
-
176
- #### Software
177
-
178
- [More Information Needed]
179
-
180
- ## Citation [optional]
181
-
182
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
183
-
184
- **BibTeX:**
185
-
186
- [More Information Needed]
187
-
188
- **APA:**
189
-
190
- [More Information Needed]
191
-
192
- ## Glossary [optional]
193
-
194
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
195
-
196
- [More Information Needed]
197
-
198
- ## More Information [optional]
199
-
200
- [More Information Needed]
201
-
202
- ## Model Card Authors [optional]
203
-
204
- [More Information Needed]
205
-
206
- ## Model Card Contact
207
-
208
- [More Information Needed]
 
12
  base_model:
13
  - deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
14
  ---
 
15
 
16
+ # 金融聊天机器人
17
+
18
+ ## 使用方式
19
+
20
+
21
+ ```python
22
+ import os
23
+ from transformers import AutoTokenizer, AutoModelForCausalLM
24
+ import torch
25
+
26
+ # 设置环境变量,解决OpenMP错误
27
+ # 这个环境变量设置允许程序在检测到多个OpenMP库时继续运行,避免出现冲突错误
28
+ os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
29
+
30
+
31
+ # 设置模型和数据路径
32
+ MODEL_PATH = "Fintech-Dreamer/FinSynth_model_chatbot"
33
+
34
+
35
+ def generate_response(model, tokenizer, instruction, input_text, max_length=512):
36
+ """
37
+ 使用模型生成客服回答
38
+
39
+ 参数:
40
+ model: 加载的语言模型实例
41
+ tokenizer: 模型对应的分词器
42
+ instruction: 指令部分文本,一般是客户的问题
43
+ input_text: 输入文本,作为参考上下文或背景信息
44
+ max_length: 生成文本的最大长度,默认为512个token
45
+
46
+ 返回:
47
+ prompt: 完整的输入提示词
48
+ response: 模型生成的回答内容(仅包含模型生成部分,不包含输入提示词)
49
+ """
50
+ # 构造提示词格式 - 使用特殊标记组织对话形式
51
+ # <|begin of sentence|>标记句子开始,和/分别标记用户和助手角色
52
+ # 这种特殊标记格式是某些模型预训练时使用的对话格式,需要严格遵循
53
+ prompt = f"<|begin of sentence|> {instruction}\n{input_text} <|Assistant|>"
54
+
55
+ # 编码输入,将文本转换为模型可以理解的token序列
56
+ # add_special_tokens=True确保添加特殊标记如开始和结束标记
57
+ # truncation=True确保输入不超过模型的最大处理长度
58
+ # padding=True确保所有输入长度一致
59
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, add_special_tokens=True)
60
+
61
+ # 将输入移动到模型所在的设备(CPU/GPU)
62
+ # 这确保了模型和输入在同一设备上,避免跨设备操作导致的错误
63
+ inputs = inputs.to(model.device)
64
+
65
+ # 使用torch.no_grad()避免计算梯度,节省内存并加速推理过程
66
+ # 在推理阶段不需要计算梯度,这可以显著减少内存使用并提高速度
67
+ with torch.no_grad():
68
+ # 调用模型的generate方法生成回答
69
+ # 这里设置了多个生成参数来控制输出的质量和特性
70
+ outputs = model.generate(
71
+ **inputs,
72
+ max_length=max_length, # 设置生成文本的最大长度
73
+ num_return_sequences=1, # 只返回一个生成序列
74
+ do_sample=True, # 使用采样策略,增加多样性
75
+ temperature=0.6, # 温度参数,控制生成文本的随机性(与模型配置一致)
76
+ top_p=0.95, # 使用nucleus sampling,只考虑概率和超过0.95的token(与模型配置一致)
77
+ top_k=20, # 只考虑概率最高的20个token,增加生成文本的可控性
78
+ repetition_penalty=1.1, # 重复惩罚系数,降低模型重复同一内容的可能性
79
+ pad_token_id=151643, # 填充标记ID(与模型配置中的eos_token_id一致)
80
+ bos_token_id=151646, # 句子开始标记ID(与模型配置一致)
81
+ eos_token_id=151643, # 句子结束标记ID(与模型配置一致)
82
+ use_cache=True, # 使用缓存加速生成过程
83
+ )
84
+
85
+ # 将生成的token序列解码为文本
86
+ # skip_special_tokens=True会跳过特殊标记,只保留实际文本内容
87
+ # clean_up_tokenization_spaces=True会清理分词过程中产生的额外空格
88
+ full_response = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
89
+
90
+ # 分离模型输入和输出
91
+ # 如果在完整响应中找到了助手标记后的内容,则提取出来
92
+ # 否则尝试找出与输入不同的部分作为输出
93
+ if "<|Assistant|>" in full_response:
94
+ response = full_response.split("<|Assistant|>")[1].strip()
95
+ else:
96
+ input_without_assistant = prompt.split("<|Assistant|>")[0]
97
+ if full_response.startswith(input_without_assistant):
98
+ response = full_response[len(input_without_assistant) :].strip()
99
+ else:
100
+ response = "[无法分离模型生成内容] " + full_response
101
+
102
+ # 返回两个独立的结果:输入提示词和模型生成的回答
103
+ return prompt, response
104
+
105
+
106
+ def process_test_data():
107
+ """
108
+ 处理测试数据集并生成客服回答
109
+
110
+ 功能:
111
+ - 加载客服问答测试数据集
112
+ - 初始化模型和分词器
113
+ - 对每个测试样本生成客服回答
114
+ - 清晰区分并打印模型的输入提示词和输出结果
115
+
116
+ 返回:
117
+ None,结果直接打印
118
+ """
119
+ # 加载测试数据
120
+
121
+ # 加载模型和分词器
122
+ print(f"加载模型: {MODEL_PATH}")
123
+
124
+ print("正在加载分词器...")
125
+ # 加载预训练的分词器,使用local_files_only=True确保只从本地加载
126
+ # 分词器负责将文本转换为数字token序列,这是模型处理文本的第一步
127
+ tokenizer = AutoTokenizer.from_pretrained(
128
+ MODEL_PATH,
129
+ trust_remote_code=True, # 允许使用模型自定义的代码
130
+ padding_side="left", # 在左侧进行填充,适合生成任务
131
+ truncation_side="left", # 在左侧进行截断,保留最新的内容
132
+ )
133
+
134
+ print("正在加载模型...")
135
+ # 加载预训练的语言模型,同样使用local_files_only=True
136
+ # 模型是实际执行推理的部分,加载到合适的设备(CPU/GPU)上
137
+ model = AutoModelForCausalLM.from_pretrained(
138
+ MODEL_PATH,
139
+ trust_remote_code=True, # 允许使用模型自定义的代码
140
+ device_map="auto", # 自动选择可用的设备(CPU/GPU)
141
+ torch_dtype=torch.bfloat16, # 使用bfloat16精度,在保持准确性的同时减少内存占用
142
+ use_cache=True, # 启用缓存以提高生成速度
143
+ )
144
+
145
+ # 设置模型为评估模式,关闭dropout等训练特性,提高推理性能
146
+ # 评估模式下模型行为更加确定,适合推理任务
147
+ model.eval()
148
+
149
+ # 处理每个测试样本
150
+ print("开始生成客服回答...")
151
+
152
+ try:
153
+ # 提取指令和输入文本
154
+ instruction = "What types of grants are included in the full grant date fair value calculation?" # 指令部分,通常是客户问题
155
+ input_text = "(1) Amounts shown in this column do not reflect dollar amounts actually received by the NEO. Instead, these amounts reflect the aggregate full grant date fair value calculated in accordance with ASC 718 for the respective fiscal year for grants of RSUs, SY PSUs, and MY PSUs, as applicable. The assumptions used in the calculation of values of the awards are set forth under Note 4 to our consolidated financial statements titled Stock-Based Compensation in our Form 10-K. With regard to the stock awards with performance-based vesting conditions, the reported grant date fair value assumes the probable outcome of the conditions at Base Compensation Plan for SY PSUs and MY PSUs, determined in accordance with applicable accounting standards."
156
+
157
+ # 生成回答
158
+ print("\n===== 模型预测 =====")
159
+
160
+ print("\n正在生成客服回答...")
161
+
162
+ # 构造完整提示并生成回答
163
+ prompt, response = generate_response(model, tokenizer, instruction, input_text)
164
+
165
+ # 清晰区分模型输入和输出(只打印一次)
166
+ print("\n\n==================== 模型输入 ====================")
167
+ print(prompt)
168
+
169
+ print("\n\n==================== 模型输出(仅包含生成部分)====================")
170
+ print(response)
171
+
172
+ except Exception as e:
173
+ # 异常处理,确保一个样本的错误不会导致整个程序崩溃
174
+ # 这对于批量处理多个样本时非常重要
175
+ print(f"\n处理样本时出错: {str(e)}")
176
+ import traceback
177
+
178
+ traceback.print_exc() # 打印详细错误信息,便于调试
179
+
180
+ print("\n客服回答生成完成!")
181
+ return None
182
 
 
183
 
184
+ def main():
185
+ """
186
+ 主函数,程序入口点
187
 
188
+ 功能:
189
+ - 启动客服聊天机器人测试流程
190
+ - 处理测试数据并生成回答
191
+ - 控制整个程序的执行流程
192
+ """
193
+ print("===== 客服聊天机器人模型调用 =====")
194
+ process_test_data()
195
 
 
196
 
197
+ if __name__ == "__main__":
198
+ main()
199
+ ```
200
 
201
+ ## 数据集参考
202
 
203
+ [Fintech-Dreamer/FinSynth_data · Datasets at Hugging Face](https://huggingface.co/datasets/Fintech-Dreamer/FinSynth_data)
 
 
 
 
 
 
204
 
205
+ ## 前端框架参考
206
 
207
+ [Fintech-Dreamer/FinSynth](https://github.com/Fintech-Dreamer/FinSynth)
208
 
209
+ ## 数据处理方式参考
 
 
210
 
211
+ [Fintech-Dreamer/FinSynth-Data-Processing](https://github.com/Fintech-Dreamer/FinSynth-Data-Processing)