Spaces:
Runtime error
Runtime error
Commit
·
b489faf
1
Parent(s):
b6da545
上传主文件
Browse files- mian_0611.py +137 -0
mian_0611.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
from langchain.vectorstores import Chroma
|
6 |
+
|
7 |
+
|
8 |
+
import openai
|
9 |
+
import time
|
10 |
+
import gradio as gr
|
11 |
+
import os
|
12 |
+
|
13 |
+
# 输入 API KEY
|
14 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
#读取PDF文件
|
19 |
+
def doc_read_pdf(file):
|
20 |
+
# 读取PDF
|
21 |
+
reader = PdfReader(file)
|
22 |
+
# reader = PdfReader('.\data\資治通鑑全集_部分1.pdf')
|
23 |
+
raw_text = ''
|
24 |
+
for i, page in enumerate(reader.pages):
|
25 |
+
text = page.extract_text()
|
26 |
+
if text:
|
27 |
+
raw_text += text
|
28 |
+
return raw_text
|
29 |
+
|
30 |
+
# 读取txt文件
|
31 |
+
def doc_read_txt(file):
|
32 |
+
with open(file, encoding='utf-8') as f:
|
33 |
+
text = f.read()
|
34 |
+
return text
|
35 |
+
|
36 |
+
#补全
|
37 |
+
|
38 |
+
#从开始到调用openai模型前的一些步骤,主要是文件读取和拆解
|
39 |
+
def doc_split(file):
|
40 |
+
#分解文本
|
41 |
+
text_splitter = CharacterTextSplitter(
|
42 |
+
separator = "\n",
|
43 |
+
chunk_size = 1200,
|
44 |
+
chunk_overlap = 100,
|
45 |
+
length_function = len,
|
46 |
+
)
|
47 |
+
# texts = text_splitter.split_text(raw_text)
|
48 |
+
texts = text_splitter.split_text(doc_read_txt(file))
|
49 |
+
|
50 |
+
return texts
|
51 |
+
|
52 |
+
#将文本向量化
|
53 |
+
def doc_vectorize(texts):
|
54 |
+
embeddings = OpenAIEmbeddings()
|
55 |
+
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))])
|
56 |
+
return docsearch
|
57 |
+
|
58 |
+
#文本被拆解储存在数组texts中
|
59 |
+
texts = doc_split(".\data\资质通鉴_1_残缺.txt")
|
60 |
+
title = "资治通鉴"
|
61 |
+
docsearch = doc_vectorize((texts))
|
62 |
+
|
63 |
+
def openai_reply(word1, word2, word3, temp):
|
64 |
+
words = word1 + "*****" + word2 + "*****" + word3
|
65 |
+
# 文本相似查找,最终结果是一个列表
|
66 |
+
docs = docsearch.similarity_search(words)
|
67 |
+
reference_1 = docs[0].page_content
|
68 |
+
reference_2 = docs[1].page_content
|
69 |
+
reference = reference_1 + reference_2
|
70 |
+
print(words)
|
71 |
+
request = "请使用文言文帮我补全[" + words + "]"
|
72 |
+
response = openai.ChatCompletion.create(
|
73 |
+
model="gpt-3.5-turbo",
|
74 |
+
messages=[
|
75 |
+
{"role": "system",
|
76 |
+
"content": f"""你是一个古汉语与中国历史专家,擅长补全古代文献。在后续的会话中,你需要补全我给你的残缺文本,
|
77 |
+
这些残缺文本用[]包括,并且其中的几段残缺汉字用“*”来代替着,且数量不明。
|
78 |
+
请你阅读并且理解下文背景资料,并且补全我待会在会话中给出的残缺文本。如果你在背景资料中找不到相关文字,请根据你对于背景资料和我给出的残缺文本的理解,
|
79 |
+
使用《资治通鉴》的文言文风格自行补全残缺文本。请注意,不要改动我提供的残缺文本中非“*”的原文,并且一定要用文言文替代“*”!!!
|
80 |
+
\n背景资料:\n{reference}
|
81 |
+
"""},
|
82 |
+
{"role": "user", "content": request},
|
83 |
+
],
|
84 |
+
max_tokens=512,
|
85 |
+
n=1,
|
86 |
+
stop=None,
|
87 |
+
temperature=temp
|
88 |
+
)
|
89 |
+
print(response.choices[0].message['content'])
|
90 |
+
shijian = time.strftime("%Y年%m月%d日%H点%M分",time.localtime())
|
91 |
+
answer = response.choices[0].message['content']
|
92 |
+
return answer, reference, shijian
|
93 |
+
|
94 |
+
|
95 |
+
# 以下是界面搭建
|
96 |
+
headline = '碎片化文本复原'
|
97 |
+
description = """请给出至多三条碎片文本,系统将会根据文献数据库尽可能进行理解和匹配,给出猜想。
|
98 |
+
当然,你也可以上传自己的TXT文件作为数据来源之一。结果仅供参考和启发。"""
|
99 |
+
|
100 |
+
with gr.Blocks() as demo:
|
101 |
+
gr.Markdown(f'<center><h1>{headline}</h1></center>')
|
102 |
+
gr.Markdown(description)
|
103 |
+
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Group():
|
106 |
+
raw_text_1 = gr.Textbox(label='在此输入碎片文本')
|
107 |
+
raw_text_2 = gr.Textbox(label='在此输入碎片文本')
|
108 |
+
raw_text_3 = gr.Textbox(label='在此输入碎片文本')
|
109 |
+
temp = gr.Slider(minimum=0.0, maximum=2.0, value=0.3, label="无序程度(Temperature)")
|
110 |
+
Title = gr.Textbox(label='在此输入提交的材料的标题(无需加《》)')
|
111 |
+
file = gr.File(
|
112 |
+
label='上传你的本地TXT文件', file_types=['.txt']
|
113 |
+
)
|
114 |
+
|
115 |
+
btn = gr.Button(value='提交')
|
116 |
+
btn.style(full_width=True)
|
117 |
+
|
118 |
+
with gr.Group():
|
119 |
+
shijian = gr.Label(label='生成时时间')
|
120 |
+
answer = gr.Textbox(label='回答')
|
121 |
+
reference = gr.Textbox(label='参考材料')
|
122 |
+
|
123 |
+
|
124 |
+
btn.click(
|
125 |
+
openai_reply,
|
126 |
+
inputs= [raw_text_1, raw_text_2, raw_text_3, temp],
|
127 |
+
outputs= [answer, reference, shijian],
|
128 |
+
)
|
129 |
+
|
130 |
+
|
131 |
+
if __name__ == "__main__":
|
132 |
+
demo.launch(server_port=7860, share=True)
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|