Sirus1 commited on
Commit
1197cde
1 Parent(s): 4a0856a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -168
app.py CHANGED
@@ -1,53 +1,13 @@
1
- import re
2
- import numpy as np
3
- import tensorflow_hub as hub
4
- import openai
5
  import os
6
- import tensorflow_text
7
- from sklearn.neighbors import NearestNeighbors
8
- import gradio as gr
9
- import requests
10
- import json
11
- import fitz
12
-
13
- #这里填写调用openai需要的密钥
14
- openai.api_key = '9481961416fa4c8e883047c5679cf971'
15
- openai.api_base = 'https://demopro-oai-we2.openai.azure.com/'
16
- openai.api_type = 'azure'
17
- openai.api_version = '2022-12-01'
18
-
19
- #将嵌套的列表展平
20
- def flatten(_2d_list):
21
- flat_list = []
22
- for element in _2d_list:
23
- if type(element) is list:
24
- for item in element:
25
- flat_list.append(item)
26
- else:
27
- flat_list.append(element)
28
- return flat_list
29
-
30
 
31
  def preprocess(text):
32
- text = text.replace('\n', ' ')
33
- text = re.sub('\s+', ' ', text)
34
  return text
35
 
36
- #将pdf文档按段落分
37
- # def pdf_to_text(path):
38
- # doc = pdfplumber.open(path)
39
- # pages = doc.pages
40
- # text_list=[]
41
- # for page,d in enumerate(pages):
42
- # d=d.extract_text()
43
- # d=preprocess(d)
44
- # text_list.append(d)
45
- # doc.close()
46
-
47
- # return text_list
48
-
49
-
50
-
51
  def pdf_to_text(path, start_page=1, end_page=None):
52
  doc = fitz.open(path)
53
  total_pages = doc.page_count
@@ -59,125 +19,53 @@ def pdf_to_text(path, start_page=1, end_page=None):
59
 
60
  for i in range(start_page - 1, end_page):
61
  text = doc.load_page(i).get_text("text")
62
- text = preprocess(text)
63
  text_list.append(text)
64
 
65
  doc.close()
66
  return text_list
67
 
 
 
 
 
68
 
69
- def text_to_chunks(texts, word_length=150, start_page=1):
70
- text_toks = [t.split(' ') for t in texts]
71
- page_nums = []
72
- chunks = []
73
-
74
- for idx, words in enumerate(text_toks):
75
- for i in range(0, len(words), word_length):
76
- chunk = words[i : i + word_length]
77
- if (
78
- (i + word_length) > len(words)
79
- and (len(chunk) < word_length)
80
- and (len(text_toks) != (idx + 1))
81
- ):
82
- text_toks[idx + 1] = chunk + text_toks[idx + 1]
83
- continue
84
- chunk = ' '.join(chunk).strip()
85
- chunk = f'[Page no. {idx+start_page}]' + ' ' + '"' + chunk + '"'
86
- chunks.append(chunk)
87
- return chunks
88
 
89
- history=pdf_to_text('The Elements of Statisitcal Learning.pdf',start_page=20)
90
- history=text_to_chunks(history,start_page=1)
91
 
92
 
93
- def encoder(text):
94
- embed=openai.Embedding.create(input=text, engine="text-embedding-ada-002")
95
- return embed.get('data')[0].get('embedding')
96
-
97
-
98
- #定义语义搜索类
99
- class SemanticSearch:
100
-
101
- def __init__(self):
102
- #类初始化,使用google公司的多语言语句编码,第一次运行时需要十几分钟的时间下载
103
- self.use =hub.load('https://tfhub.dev/google/universal-sentence-encoder-multilingual/3')
104
- self.fitted = False
105
-
106
- def get_text_embedding(self, texts, batch=1000):
107
- embeddings = []
108
- for i in range(0, len(texts), batch):
109
- text_batch = texts[i : (i + batch)]
110
- emb_batch = self.use(text_batch)
111
- embeddings.append(emb_batch)
112
- embeddings = np.vstack(embeddings)
113
- return embeddings
114
-
115
-
116
-
117
- #K近邻算法,找到与问题最相似的 k 个段落,这里的 k 即n_neighbors=10
118
- def fit(self, data, batch=1000, n_neighbors=5):
119
- self.data = data
120
- self.embeddings = self.get_text_embedding(data, batch=batch)
121
- n_neighbors = min(n_neighbors, len(self.embeddings))
122
- self.nn = NearestNeighbors(n_neighbors=n_neighbors)
123
- self.nn.fit(self.embeddings)
124
- self.fitted = True
125
-
126
- #定义了该方法后,实例就可以被当作函数调用,text参数即用户提出的问题,inp_emb为其转化成的向量
127
- def __call__(self, text, return_data=True):
128
- inp_emb = self.use([text])
129
 
130
 
131
- neighbors = self.nn.kneighbors(inp_emb, return_distance=False)[0]
132
-
133
- if return_data:
134
- return [self.data[i] for i in neighbors]
135
- else:
136
- return neighbors
137
-
138
-
139
- #openai的api接口,engine参数为我们选择的大语言模型,prompt即提示词
140
- def generate_text(prompt, engine="text-davinci-003"):
141
- completions = openai.Completion.create(
142
- engine=engine,
143
- prompt=prompt,
144
- max_tokens=512,
145
- n=1,
146
- stop=None,
147
- temperature=0.7,
148
- )
149
- message = completions.choices[0].text
150
- return message
151
 
152
 
153
  def generate_answer(question):
154
- #匹配与问题最相近的n个段落,前面定义了n=10
155
- topn_chunks = recommender(question)
156
- prompt = ""
157
- prompt += 'search results:\n\n'
158
-
159
- #把匹配到的段落加进提示词
160
- for c in topn_chunks:
161
- prompt += c + '\n\n'
162
-
163
- #提示词
164
- prompt += '''
165
- Instructions: 如果搜索结果中找不到相关信息,只需要回答'未在该文档中找到相关信息'。
166
- 如果找到了相关信息,请使用中文回答,回答尽量精确简洁。并在句子的末尾使用[页码]符号引用每个参考文献(每个结果的开头都有这个编号)
167
- 如果不确定答案是否正确,就仅给出相似段落的来源,不要回复错误的答案。
168
- \n\nQuery: {question}\nAnswer:
169
  '''
170
-
171
- prompt += f"Query: {question}\nAnswer:"
172
- answer = generate_text(prompt,"text-davinci-003")
173
  return answer
174
-
175
-
176
- recommender = SemanticSearch()
177
- recommender.fit(history)
178
-
179
-
180
- #以下为web客户端搭建,运行后产生客户端界面
181
  def ask_api(question):
182
 
183
  if question.strip() == '':
@@ -185,29 +73,24 @@ def ask_api(question):
185
 
186
  return generate_answer(question)
187
 
188
- title = 'Chat With Statistical Learning'
189
- description = """ 该机器人将以Trevor Hastie等人所著的The Elements of Statistical Learning Data Mining, Inference, and Prediction
190
- (即我们上课所用的课本)为主题回答你的问题,如果所问问题与书的内容无关,将会返回"未在该文档中找到相关信息"
191
- """
192
 
193
- with gr.Blocks() as demo:
194
- gr.Markdown(f'<center><h1>{title}</h1></center>')
195
- gr.Markdown(description)
196
 
197
- with gr.Row():
198
- with gr.Group():
199
- question = gr.Textbox(label='请输入你的问题')
200
- btn = gr.Button(value='提交')
201
- btn.style(full_width=True)
 
 
 
202
 
203
- with gr.Group():
204
- answer = gr.Textbox(label='回答:')
 
 
 
 
 
205
 
206
- btn.click(
207
- ask_api,
208
- inputs=[question],
209
- outputs=[answer]
210
- )
211
 
212
- #参数share=True会产生一个公开网页,别人可以通过访问该网页使用你的模型,前提是你需要正在运行这段代码(将自己的电脑当作服务器)
213
  demo.launch()
 
1
+ from langchain.embeddings import OpenAIEmbeddings
2
+ from langchain.vectorstores import Chroma
3
+ from langchain.llms import OpenAI
4
+ from langchain.chains.question_answering import load_qa_chain
5
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def preprocess(text):
8
+ text = text.replace('\n', '')
 
9
  return text
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def pdf_to_text(path, start_page=1, end_page=None):
12
  doc = fitz.open(path)
13
  total_pages = doc.page_count
 
19
 
20
  for i in range(start_page - 1, end_page):
21
  text = doc.load_page(i).get_text("text")
 
22
  text_list.append(text)
23
 
24
  doc.close()
25
  return text_list
26
 
27
+ def law_split(path,name):
28
+ text_list=pdf_to_text(path)
29
+ text= ''.join(text_list)
30
+ text_split=re.split(r'第.+条\s',text)[1:]
31
 
32
+ for index, text in enumerate(text_split):
33
+ text=preprocess(text)
34
+ text_split[index]=f'《中华人民共和国{name}》 第{index+1}条 '+text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ return text_split
 
37
 
38
 
39
+ def folder_read(path):
40
+ text_list=[]
41
+ paths=os.listdir(path)
42
+ for file in paths:
43
+ name=file.split('.')[0]
44
+ suffix=file.split('.')[-1]
45
+ if suffix=='pdf':
46
+ text_list+=law_split(f'{path}/{file}',name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
 
49
+ return text_list
50
+
51
+
52
+ text_list=folder_read('laws')
53
+ embeddings = OpenAIEmbeddings()
54
+ vectordb = Chroma.from_texts(texts=text_list, embedding=embeddings)
55
+ llm = OpenAI(temperature=0.5,max_tokens=1024)
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
 
58
  def generate_answer(question):
59
+ prompt='''
60
+ 请根据给出的法律条文回答问题,给出适当的法律建议。回答时要说出你引用的法律条文是第几条,并说出引用的每一条是哪部法律中的。
61
+ 引用的法律条文不要超过两条,回答尽量简明扼要
62
+ 如果问题与搜索结果无关,就仅回答"该问题与青少年法律无关"即可。
 
 
 
 
 
 
 
 
 
 
 
63
  '''
64
+ most_relevant_texts = vectordb.max_marginal_relevance_search(question, k=5)
65
+ chain = load_qa_chain(llm)
66
+ answer = chain.run(input_documents=most_relevant_texts, question=question+prompt)
67
  return answer
68
+
 
 
 
 
 
 
69
  def ask_api(question):
70
 
71
  if question.strip() == '':
 
73
 
74
  return generate_answer(question)
75
 
 
 
 
 
76
 
 
 
 
77
 
78
+ title = '青少年法律科普问答'
79
+ description = """ 本bot旨在根据中华人民共和国的法律回答有关青少年的问题,目前囊括的法律有\n
80
+ 《未成年人保护法》\n
81
+ 《义务教育法》\n
82
+ 《预防未成年人犯罪法》\n
83
+ 《妇女儿童权益保护法》
84
+ """
85
+
86
 
87
+ demo = gr.Interface(
88
+ title=title,
89
+ description=description,
90
+ fn=ask_api,
91
+ inputs=gr.Textbox(label="请输入与青少年法律相关的问题",lines=2),
92
+ outputs=gr.outputs.Textbox(label="参考回答"),
93
+ examples=[["未成年遭受网络欺凌该怎么办?"],['年满多少岁的儿童应当接受义务教育?'],['若发现离家出走的未成年人,应如何处理?']])
94
 
 
 
 
 
 
95
 
 
96
  demo.launch()