Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,103 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
os.system('git clone https://huggingface.co/souljoy/chatGPT')
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
# for line in f:
|
| 10 |
-
# authorization = line.strip()
|
| 11 |
-
|
| 12 |
-
url = 'https://api.openai.com/v1/chat/completions'
|
| 13 |
headers = {
|
| 14 |
'Content-Type': 'application/json',
|
| 15 |
-
'Authorization': 'Bearer ' + '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'
|
| 16 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
data=json.dumps(data),
|
| 31 |
headers=headers
|
| 32 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return
|
| 38 |
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
-
state = gr.State([])
|
| 42 |
with gr.Row():
|
| 43 |
with gr.Column():
|
| 44 |
-
|
| 45 |
-
txt = gr.Textbox(label='
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Column():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
| 4 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
+
from sentence_transformers import util
|
| 6 |
|
| 7 |
+
url = 'https://souljoy-my-api.hf.space/qa_maker'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
headers = {
|
| 9 |
'Content-Type': 'application/json',
|
|
|
|
| 10 |
}
|
| 11 |
+
thread_pool_executor = ThreadPoolExecutor(max_workers=16)
|
| 12 |
+
history_max_len = 500
|
| 13 |
+
all_max_len = 2000
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_emb(text):
|
| 17 |
+
emb_url = 'https://souljoy-my-api.hf.space/embeddings'
|
| 18 |
+
data = {"content": text}
|
| 19 |
+
result = requests.post(url=emb_url,
|
| 20 |
+
data=json.dumps(data),
|
| 21 |
+
headers=headers
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
return result.json()['data'][0]['embedding']
|
| 25 |
+
|
| 26 |
|
| 27 |
+
def doc_emb(doc: str):
|
| 28 |
+
texts = doc.split('\n')
|
| 29 |
+
futures = []
|
| 30 |
+
for text in texts:
|
| 31 |
+
futures.append(thread_pool_executor.submit(get_emb, text))
|
| 32 |
+
emb_list = []
|
| 33 |
+
for f in futures:
|
| 34 |
+
emb_list.append(f.result())
|
| 35 |
+
print('\n'.join(texts))
|
| 36 |
+
return texts, emb_list, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.Markdown.update(
|
| 37 |
+
visible=True)
|
| 38 |
|
| 39 |
+
|
| 40 |
+
def get_response(msg, bot, doc_text_list, doc_embeddings):
|
| 41 |
+
future = thread_pool_executor.submit(get_emb, msg)
|
| 42 |
+
now_len = len(msg)
|
| 43 |
+
req_json = {'question': msg}
|
| 44 |
+
his_bg = -1
|
| 45 |
+
for i in range(len(bot) - 1, -1, -1):
|
| 46 |
+
if now_len + len(bot[i][0]) + len(bot[i][1]) > history_max_len:
|
| 47 |
+
break
|
| 48 |
+
now_len += len(bot[i][0]) + len(bot[i][1])
|
| 49 |
+
his_bg = i
|
| 50 |
+
req_json['history'] = [] if his_bg == -1 else bot[his_bg:]
|
| 51 |
+
query_embedding = future.result()
|
| 52 |
+
cos_scores = util.cos_sim(query_embedding, doc_embeddings)[0]
|
| 53 |
+
score_index = [[score, index] for score, index in zip(cos_scores, [i for i in range(len(cos_scores))])]
|
| 54 |
+
score_index.sort(key=lambda x: x[0], reverse=True)
|
| 55 |
+
print('score_index:\n', score_index)
|
| 56 |
+
index_list, sub_doc_list = [], []
|
| 57 |
+
for s_i in score_index:
|
| 58 |
+
doc = doc_text_list[s_i[1]]
|
| 59 |
+
if now_len + len(doc) > all_max_len:
|
| 60 |
+
break
|
| 61 |
+
index_list.append(s_i[1])
|
| 62 |
+
now_len += len(doc)
|
| 63 |
+
index_list.sort()
|
| 64 |
+
for i in index_list:
|
| 65 |
+
sub_doc_list.append(doc_text_list[i])
|
| 66 |
+
req_json['doc'] = '' if len(sub_doc_list) == 0 else '\n'.join(sub_doc_list)
|
| 67 |
+
data = {"content": json.dumps(req_json)}
|
| 68 |
+
print('data:\n', req_json)
|
| 69 |
+
result = requests.post(url='https://souljoy-my-api.hf.space/chatpdf',
|
| 70 |
data=json.dumps(data),
|
| 71 |
headers=headers
|
| 72 |
)
|
| 73 |
+
res = result.json()['content']
|
| 74 |
+
bot.append([msg, res])
|
| 75 |
+
return bot[max(0, len(bot) - 3):], gr.Markdown.update(visible=False)
|
| 76 |
+
|
| 77 |
|
| 78 |
+
def up_file(files):
|
| 79 |
+
for idx, file in enumerate(files):
|
| 80 |
+
print(file.name)
|
| 81 |
+
return gr.Button.update(visible=True)
|
| 82 |
|
| 83 |
|
| 84 |
with gr.Blocks() as demo:
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column():
|
| 87 |
+
file = gr.File(file_types=['.pdf'], label='上传PDF')
|
| 88 |
+
txt = gr.Textbox(label='PDF解析结果', visible=False)
|
| 89 |
+
doc_bu = gr.Button(value='提交', visible=False)
|
| 90 |
+
md = gr.Markdown("""#### 文档提交成功 🙋 """, visible=False)
|
| 91 |
+
doc_text_state = gr.State([])
|
| 92 |
+
doc_emb_state = gr.State([])
|
| 93 |
with gr.Column():
|
| 94 |
+
chat_bot = gr.Chatbot()
|
| 95 |
+
msg_txt = gr.Textbox(label='消息框', placeholder='输入消息,点击发送', visible=False)
|
| 96 |
+
chat_bu = gr.Button(value='发送', visible=False)
|
| 97 |
|
| 98 |
+
doc_bu.click(doc_emb, [txt], [doc_text_state, doc_emb_state, msg_txt, chat_bu, md])
|
| 99 |
+
chat_bu.click(get_response, [msg_txt, chat_bot, doc_text_state, doc_emb_state], [chat_bot, md])
|
| 100 |
+
file.change(up_file, [file], [doc_bu])
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.queue().launch()
|
| 103 |
+
# demo.queue().launch(share=False, server_name='172.22.2.54', server_port=9191)
|