forked from shibing624/ChatPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatpdf.py
271 lines (249 loc) · 9.91 KB
/
chatpdf.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description:
"""
import argparse
from typing import Union, List
import torch
from loguru import logger
from peft import PeftModel
from similarities import Similarity
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel
from transformers.generation.utils import GenerationConfig
PROMPT_TEMPLATE = """基于以下已知信息,简洁和专业的来回答用户的问题。
如果无法从中得到答案,请说 "根据已知信息无法回答该问题" 或 "没有提供足够的相关信息",不允许在答案中添加编造成分,答案请使用中文。
已知内容:
{context_str}
问题:
{query_str}
"""
class ChatPDF:
def __init__(
self,
sim_model_name_or_path: str = "shibing624/text2vec-base-chinese",
gen_model_type: str = "baichuan",
gen_model_name_or_path: str = "baichuan-inc/Baichuan-13B-Chat",
lora_model_name_or_path: str = None,
device: str = None,
int8: bool = False,
int4: bool = False,
):
default_device = 'cpu'
if torch.cuda.is_available():
default_device = 'cuda'
elif torch.backends.mps.is_available():
default_device = 'mps'
self.device = device or default_device
self.sim_model = Similarity(model_name_or_path=sim_model_name_or_path, device=self.device)
self.gen_model, self.tokenizer = self._init_gen_model(
gen_model_type,
gen_model_name_or_path,
peft_name=lora_model_name_or_path,
int8=int8,
int4=int4,
)
self.history = None
self.doc_files = None
def _init_gen_model(
self,
gen_model_type: str,
gen_model_name_or_path: str,
peft_name: str = None,
int8: bool = False,
int4: bool = False,
):
"""Init generate model."""
if int8 or int4:
device_map = None
else:
device_map = "auto"
if gen_model_type == "chatglm":
model = AutoModel.from_pretrained(
gen_model_name_or_path,
torch_dtype=torch.float16,
device_map=device_map,
trust_remote_code=True
)
else:
model = AutoModelForCausalLM.from_pretrained(
gen_model_name_or_path,
torch_dtype=torch.float16,
device_map=device_map,
trust_remote_code=True
)
if int4:
model = model.quantize(4).cuda()
elif int8:
model = model.quantize(8).cuda()
model.generation_config = GenerationConfig.from_pretrained(gen_model_name_or_path)
tokenizer = AutoTokenizer.from_pretrained(
gen_model_name_or_path,
use_fast=False,
trust_remote_code=True
)
if peft_name:
model = PeftModel.from_pretrained(
model,
peft_name,
torch_dtype=torch.float16,
)
logger.info(f"Loaded peft model from {peft_name}")
return model, tokenizer
@torch.inference_mode()
def generate_answer(
self,
prompt,
max_new_tokens=512,
temperature=0.7,
top_k=40,
top_p=0.9,
do_sample=True,
repetition_penalty=1.0,
context_len=2048
):
generation_config = dict(
max_new_tokens=max_new_tokens,
temperature=temperature,
top_k=top_k,
top_p=top_p,
do_sample=do_sample,
repetition_penalty=repetition_penalty,
)
input_ids = self.tokenizer(prompt).input_ids
max_src_len = context_len - max_new_tokens - 8
input_ids = input_ids[-max_src_len:]
generation_output = self.gen_model.generate(
input_ids=torch.as_tensor([input_ids]).to(self.device),
**generation_config,
)
output_ids = generation_output[0]
output = self.tokenizer.decode(output_ids, skip_special_tokens=False)
stop_str = self.tokenizer.eos_token
l_prompt = len(self.tokenizer.decode(input_ids, skip_special_tokens=False))
pos = output.rfind(stop_str, l_prompt)
if pos != -1:
output = output[l_prompt:pos]
else:
output = output[l_prompt:]
return output.strip()
def load_doc_files(self, doc_files: Union[str, List[str]]):
"""Load document files."""
if isinstance(doc_files, str):
doc_files = [doc_files]
for doc_file in doc_files:
if doc_file.endswith('.pdf'):
corpus = self.extract_text_from_pdf(doc_file)
elif doc_file.endswith('.docx'):
corpus = self.extract_text_from_docx(doc_file)
elif doc_file.endswith('.md'):
corpus = self.extract_text_from_markdown(doc_file)
else:
corpus = self.extract_text_from_txt(doc_file)
self.sim_model.add_corpus(corpus)
self.doc_files = doc_files
@staticmethod
def extract_text_from_pdf(file_path: str):
"""Extract text content from a PDF file."""
import PyPDF2
contents = []
with open(file_path, 'rb') as f:
pdf_reader = PyPDF2.PdfReader(f)
for page in pdf_reader.pages:
page_text = page.extract_text().strip()
raw_text = [text.strip() for text in page_text.splitlines() if text.strip()]
new_text = ''
for text in raw_text:
new_text += text
if text[-1] in ['.', '!', '?', '。', '!', '?', '…', ';', ';', ':', ':', '”', '’', ')', '】', '》', '」',
'』', '〕', '〉', '》', '〗', '〞', '〟', '»', '"', "'", ')', ']', '}']:
contents.append(new_text)
new_text = ''
if new_text:
contents.append(new_text)
return contents
@staticmethod
def extract_text_from_txt(file_path: str):
"""Extract text content from a TXT file."""
contents = []
with open(file_path, 'r', encoding='utf-8') as f:
contents = [text.strip() for text in f.readlines() if text.strip()]
return contents
@staticmethod
def extract_text_from_docx(file_path: str):
"""Extract text content from a DOCX file."""
import docx
document = docx.Document(file_path)
contents = [paragraph.text.strip() for paragraph in document.paragraphs if paragraph.text.strip()]
return contents
@staticmethod
def extract_text_from_markdown(file_path: str):
"""Extract text content from a Markdown file."""
import markdown
from bs4 import BeautifulSoup
with open(file_path, 'r', encoding='utf-8') as f:
markdown_text = f.read()
html = markdown.markdown(markdown_text)
soup = BeautifulSoup(html, 'html.parser')
contents = [text.strip() for text in soup.get_text().splitlines() if text.strip()]
return contents
@staticmethod
def _add_source_numbers(lst):
"""Add source numbers to a list of strings."""
return [f'[{idx + 1}]\t "{item}"' for idx, item in enumerate(lst)]
def query(
self,
query: str,
topn: int = 5,
max_length: int = 512,
max_input_size: int = 1024,
):
"""Query from corpus."""
sim_contents = self.sim_model.most_similar(query, topn=topn)
reference_results = []
for query_id, id_score_dict in sim_contents.items():
for corpus_id, s in id_score_dict.items():
reference_results.append(self.sim_model.corpus[corpus_id])
if not reference_results:
return '没有提供足够的相关信息', reference_results
reference_results = self._add_source_numbers(reference_results)
context_str = '\n'.join(reference_results)[:(max_input_size - len(PROMPT_TEMPLATE))]
prompt = PROMPT_TEMPLATE.format(context_str=context_str, query_str=query)
response = self.generate_answer(prompt, max_new_tokens=max_length)
return response, reference_results
def save_index(self, index_path=None):
"""Save model."""
if index_path is None:
index_path = '.'.join(self.doc_files.split('.')[:-1]) + '_index.json'
self.sim_model.save_index(index_path)
def load_index(self, index_path=None):
"""Load model."""
if index_path is None:
index_path = '.'.join(self.doc_files.split('.')[:-1]) + '_index.json'
self.sim_model.load_index(index_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--sim_model", type=str, default="shibing624/text2vec-base-chinese")
parser.add_argument("--gen_model_type", type=str, default="baichuan")
parser.add_argument("--gen_model", type=str, default="baichuan-inc/Baichuan-13B-Chat")
parser.add_argument("--lora_model", type=str, default=None)
parser.add_argument("--device", type=str, default=None)
parser.add_argument("--int4", action='store_true', help="use int4 quantization")
parser.add_argument("--int8", action='store_true', help="use int8 quantization")
args = parser.parse_args()
print(args)
m = ChatPDF(
sim_model_name_or_path=args.sim_model,
gen_model_type=args.gen_model_type,
gen_model_name_or_path=args.gen_model,
lora_model_name_or_path=args.lora_model,
device=args.device,
int4=args.int4,
int8=args.int8
)
m.load_doc_files(doc_files='sample.pdf')
response = m.query('自然语言中的非平行迁移是指什么?')
print(response)
print(response[0])
response = m.query('本文作者是谁?')
print(response)