File size: 4,797 Bytes
b489faf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f99882d
52c6b42
b489faf
 
f99882d
b489faf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f99882d
b489faf
 
 
 
b9a9eab
 
 
 
b489faf
 
 
 
 
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
# -*- coding: utf-8 -*-
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma

import openai
import time
import gradio as gr
import os

# 输入 API KEY
os.environ["OPENAI_API_KEY"] = "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"



#读取PDF文件
def doc_read_pdf(file):
    # 读取PDF
    reader = PdfReader(file)
    # reader = PdfReader('.\data\資治通鑑全集_部分1.pdf')
    raw_text = ''
    for i, page in enumerate(reader.pages):
        text = page.extract_text()
        if text:
            raw_text += text
    return raw_text

# 读取txt文件
def doc_read_txt(file):
    with open(file, encoding='utf-8') as f:
        text = f.read()
    return text

#补全

#从开始到调用openai模型前的一些步骤,主要是文件读取和拆解
def doc_split(file):
    #分解文本
    text_splitter = CharacterTextSplitter(
        separator = "\n",
        chunk_size = 1200,
        chunk_overlap  = 100,
        length_function = len,
    )
    # texts = text_splitter.split_text(raw_text)
    texts = text_splitter.split_text(doc_read_txt(file))

    return texts

#将文本向量化
def doc_vectorize(texts):
    embeddings = OpenAIEmbeddings()
    docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))])
    return docsearch

#文本被拆解储存在数组texts中
# raw_file = 
texts = doc_split("./资治通鉴_1_残缺.txt")
docsearch = doc_vectorize((texts))

def openai_reply(word1, word2, word3, temp, file):
    words = word1 + "*****" + word2 + "*****" + word3
    # 文本相似查找,最终结果是一个列表
    docs = docsearch.similarity_search(words)
    reference_1 = docs[0].page_content
    reference_2 = docs[1].page_content
    reference = reference_1 + reference_2
    print(words)
    request = "请使用文言文帮我补全[" + words + "]"
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system",
            "content": f"""你是一个古汉语与中国历史专家,擅长补全古代文献。在后续的会话中,你需要补全我给你的残缺文本,
            这些残缺文本用[]包括,并且其中的几段残缺汉字用“*”来代替着,且数量不明。
            请你阅读并且理解下文背景资料,并且补全我待会在会话中给出的残缺文本。如果你在背景资料中找不到相关文字,请根据你对于背景资料和我给出的残缺文本的理解,
            使用《资治通鉴》的文言文风格自行补全残缺文本。请注意,不要改动我提供的残缺文本中非“*”的原文,并且一定要用文言文替代“*”!!!
            \n背景资料:\n{reference}
            """},
            {"role": "user", "content": request},
        ],
        max_tokens=512,
        n=1,
        stop=None,
        temperature=temp
    )
    print(response.choices[0].message['content'])
    shijian = time.strftime("%Y年%m月%d日%H点%M分",time.localtime())
    answer = response.choices[0].message['content']
    return answer, reference, shijian


# 以下是界面搭建
headline = '碎片化文本复原'
description = """请给出至多三条碎片文本,系统将会根据文献数据库尽可能进行理解和匹配,给出猜想。
当然,你也可以上传自己的TXT文件作为数据来源之一。结果仅供参考和启发。"""

with gr.Blocks() as demo:
    gr.Markdown(f'<center><h1>{headline}</h1></center>')
    gr.Markdown(description)

    with gr.Row():
        with gr.Group():
            raw_text_1 = gr.Textbox(label='在此输入碎片文本')
            raw_text_2 = gr.Textbox(label='在此输入碎片文本')
            raw_text_3 = gr.Textbox(label='在此输入碎片文本')
            temp = gr.Slider(minimum=0.0, maximum=2.0, value=0.3, label="无序程度(Temperature)")
            Title = gr.Textbox(label='在此输入提交的材料的标题(无需加《》)')
            file = gr.File(
                label='上传你的本地TXT文件', file_types=['.txt']
            )
            
            btn = gr.Button(value='提交')
            btn.style(full_width=True)

        with gr.Group():
            shijian = gr.Label(label='生成时时间')
            answer = gr.Textbox(label='回答')
            reference = gr.Textbox(label='参考材料')

        
        btn.click(
            openai_reply,
            inputs= [raw_text_1, raw_text_2, raw_text_3, temp, file],
            outputs= [answer, reference, shijian],
        )


demo.launch()

# if __name__ == "__main__":
#     demo.launch(server_port=7860, share=True)