Duplicate from souljoy/chatPDF
Browse filesCo-authored-by: 小爷毛毛-卓寿杰 <[email protected]>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +166 -0
- requirements.txt +3 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: ChatPDF
|
3 |
+
emoji: 💻
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.20.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
duplicated_from: souljoy/chatPDF
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
# from concurrent.futures import ThreadPoolExecutor
|
5 |
+
import pdfplumber
|
6 |
+
import pandas as pd
|
7 |
+
import time
|
8 |
+
from cnocr import CnOcr
|
9 |
+
from sentence_transformers import SentenceTransformer, models, util
|
10 |
+
word_embedding_model = models.Transformer('uer/sbert-base-chinese-nli', do_lower_case=True)
|
11 |
+
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), pooling_mode='cls')
|
12 |
+
embedder = SentenceTransformer(modules=[word_embedding_model, pooling_model])
|
13 |
+
ocr = CnOcr()
|
14 |
+
# chat_url = 'https://souljoy-my-api.hf.space/sale'
|
15 |
+
chat_url = 'https://souljoy-my-api.hf.space/chatpdf'
|
16 |
+
headers = {
|
17 |
+
'Content-Type': 'application/json',
|
18 |
+
}
|
19 |
+
# thread_pool_executor = ThreadPoolExecutor(max_workers=4)
|
20 |
+
history_max_len = 500
|
21 |
+
all_max_len = 3000
|
22 |
+
|
23 |
+
|
24 |
+
def get_emb(text):
|
25 |
+
emb_url = 'https://souljoy-my-api.hf.space/embeddings'
|
26 |
+
data = {"content": text}
|
27 |
+
try:
|
28 |
+
result = requests.post(url=emb_url,
|
29 |
+
data=json.dumps(data),
|
30 |
+
headers=headers
|
31 |
+
)
|
32 |
+
return result.json()['data'][0]['embedding']
|
33 |
+
except Exception as e:
|
34 |
+
print('data', data, 'result json', result.json())
|
35 |
+
|
36 |
+
|
37 |
+
def doc_emb(doc: str):
|
38 |
+
texts = doc.split('\n')
|
39 |
+
# futures = []
|
40 |
+
emb_list = embedder.encode(texts)
|
41 |
+
# for text in texts:
|
42 |
+
# futures.append(thread_pool_executor.submit(get_emb, text))
|
43 |
+
# for f in futures:
|
44 |
+
# emb_list.append(f.result())
|
45 |
+
print('\n'.join(texts))
|
46 |
+
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
47 |
+
value="""操作说明 step 3:PDF解析提交成功! 🙋 可以开始对话啦~"""), gr.Chatbot.update(visible=True)
|
48 |
+
|
49 |
+
|
50 |
+
def get_response(msg, bot, doc_text_list, doc_embeddings):
|
51 |
+
# future = thread_pool_executor.submit(get_emb, msg)
|
52 |
+
now_len = len(msg)
|
53 |
+
req_json = {'question': msg}
|
54 |
+
his_bg = -1
|
55 |
+
for i in range(len(bot) - 1, -1, -1):
|
56 |
+
if now_len + len(bot[i][0]) + len(bot[i][1]) > history_max_len:
|
57 |
+
break
|
58 |
+
now_len += len(bot[i][0]) + len(bot[i][1])
|
59 |
+
his_bg = i
|
60 |
+
req_json['history'] = [] if his_bg == -1 else bot[his_bg:]
|
61 |
+
# query_embedding = future.result()
|
62 |
+
query_embedding = embedder.encode([msg])
|
63 |
+
cos_scores = util.cos_sim(query_embedding, doc_embeddings)[0]
|
64 |
+
score_index = [[score, index] for score, index in zip(cos_scores, [i for i in range(len(cos_scores))])]
|
65 |
+
score_index.sort(key=lambda x: x[0], reverse=True)
|
66 |
+
print('score_index:\n', score_index)
|
67 |
+
index_set, sub_doc_list = set(), []
|
68 |
+
for s_i in score_index:
|
69 |
+
doc = doc_text_list[s_i[1]]
|
70 |
+
if now_len + len(doc) > all_max_len:
|
71 |
+
break
|
72 |
+
index_set.add(s_i[1])
|
73 |
+
now_len += len(doc)
|
74 |
+
# 可能段落截断错误,所以把上下段也加入进来
|
75 |
+
if s_i[1] > 0 and s_i[1] -1 not in index_set:
|
76 |
+
doc = doc_text_list[s_i[1]-1]
|
77 |
+
if now_len + len(doc) > all_max_len:
|
78 |
+
break
|
79 |
+
index_set.add(s_i[1]-1)
|
80 |
+
now_len += len(doc)
|
81 |
+
if s_i[1] + 1 < len(doc_text_list) and s_i[1] + 1 not in index_set:
|
82 |
+
doc = doc_text_list[s_i[1]+1]
|
83 |
+
if now_len + len(doc) > all_max_len:
|
84 |
+
break
|
85 |
+
index_set.add(s_i[1]+1)
|
86 |
+
now_len += len(doc)
|
87 |
+
|
88 |
+
index_list = list(index_set)
|
89 |
+
index_list.sort()
|
90 |
+
for i in index_list:
|
91 |
+
sub_doc_list.append(doc_text_list[i])
|
92 |
+
req_json['doc'] = '' if len(sub_doc_list) == 0 else '\n'.join(sub_doc_list)
|
93 |
+
data = {"content": json.dumps(req_json)}
|
94 |
+
print('data:\n', req_json)
|
95 |
+
result = requests.post(url=chat_url,
|
96 |
+
data=json.dumps(data),
|
97 |
+
headers=headers
|
98 |
+
)
|
99 |
+
res = result.json()['content']
|
100 |
+
bot.append([msg, res])
|
101 |
+
return bot[max(0, len(bot) - 3):]
|
102 |
+
|
103 |
+
|
104 |
+
def up_file(files):
|
105 |
+
doc_text_list = []
|
106 |
+
for idx, file in enumerate(files):
|
107 |
+
print(file.name)
|
108 |
+
with pdfplumber.open(file.name) as pdf:
|
109 |
+
for i in range(len(pdf.pages)):
|
110 |
+
# 读取PDF文档第i+1页
|
111 |
+
page = pdf.pages[i]
|
112 |
+
res_list = page.extract_text().split('\n')[:-1]
|
113 |
+
|
114 |
+
for j in range(len(page.images)):
|
115 |
+
# 获取图片的二进制流
|
116 |
+
img = page.images[j]
|
117 |
+
file_name = '{}-{}-{}.png'.format(str(time.time()), str(i), str(j))
|
118 |
+
with open(file_name, mode='wb') as f:
|
119 |
+
f.write(img['stream'].get_data())
|
120 |
+
try:
|
121 |
+
res = ocr.ocr(file_name)
|
122 |
+
except Exception as e:
|
123 |
+
res = []
|
124 |
+
if len(res) > 0:
|
125 |
+
res_list.append(' '.join([re['text'] for re in res]))
|
126 |
+
|
127 |
+
tables = page.extract_tables()
|
128 |
+
for table in tables:
|
129 |
+
# 第一��当成表头:
|
130 |
+
df = pd.DataFrame(table[1:], columns=table[0])
|
131 |
+
try:
|
132 |
+
records = json.loads(df.to_json(orient="records", force_ascii=False))
|
133 |
+
for rec in records:
|
134 |
+
res_list.append(json.dumps(rec, ensure_ascii=False))
|
135 |
+
except Exception as e:
|
136 |
+
res_list.append(str(df))
|
137 |
+
|
138 |
+
doc_text_list += res_list
|
139 |
+
doc_text_list = [str(text).strip() for text in doc_text_list if len(str(text).strip()) > 0]
|
140 |
+
print(doc_text_list)
|
141 |
+
return gr.Textbox.update(value='\n'.join(doc_text_list), visible=True), gr.Button.update(
|
142 |
+
visible=True), gr.Markdown.update(
|
143 |
+
value="操作说明 step 2:确认PDF解析结果(可修正),点击“提交解析结果”,随后进行对话")
|
144 |
+
|
145 |
+
|
146 |
+
with gr.Blocks() as demo:
|
147 |
+
with gr.Row():
|
148 |
+
with gr.Column():
|
149 |
+
file = gr.File(file_types=['.pdf'], label='点击上传PDF,进行解析(支持多文档、表格、OCR)', file_count='multiple')
|
150 |
+
doc_bu = gr.Button(value='提交解析结果', visible=False)
|
151 |
+
txt = gr.Textbox(label='PDF解析结果', visible=False)
|
152 |
+
doc_text_state = gr.State([])
|
153 |
+
doc_emb_state = gr.State([])
|
154 |
+
with gr.Column():
|
155 |
+
md = gr.Markdown("""操作说明 step 1:点击左侧区域,上传PDF,进行解析""")
|
156 |
+
chat_bot = gr.Chatbot(visible=False)
|
157 |
+
msg_txt = gr.Textbox(label='消息框', placeholder='输入消息,点击发送', visible=False)
|
158 |
+
chat_bu = gr.Button(value='发送', visible=False)
|
159 |
+
|
160 |
+
file.change(up_file, [file], [txt, doc_bu, md])
|
161 |
+
doc_bu.click(doc_emb, [txt], [doc_text_state, doc_emb_state, msg_txt, chat_bu, md, chat_bot])
|
162 |
+
chat_bu.click(get_response, [msg_txt, chat_bot, doc_text_state, doc_emb_state], [chat_bot])
|
163 |
+
|
164 |
+
if __name__ == "__main__":
|
165 |
+
demo.queue().launch()
|
166 |
+
# demo.queue().launch(share=False, server_name='172.22.2.54', server_port=9191)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pdfplumber
|
2 |
+
sentence_transformers
|
3 |
+
cnocr
|