standardteam commited on
Commit
4dd1d1d
1 Parent(s): 1dee7f2

Update GPT-4_PDF_summary.py

Browse files
Files changed (1) hide show
  1. GPT-4_PDF_summary.py +23 -28
GPT-4_PDF_summary.py CHANGED
@@ -1,9 +1,9 @@
1
  #!/usr/bin/env python
2
  # coding: utf-8
3
 
4
- # !pip install langchain openai chromadb tiktoken pypdf panel
5
- # In[ ]:
6
 
 
7
 
8
  import os
9
  from langchain.chains import RetrievalQA
@@ -28,7 +28,7 @@ pn.state.template.param.update(
28
  )
29
 
30
 
31
- # In[3]:
32
 
33
 
34
  file_input = pn.widgets.FileInput(width=300)
@@ -59,27 +59,30 @@ widgets = pn.Row(
59
  )
60
 
61
 
62
- # In[4]:
 
 
63
 
64
 
65
  def qa(file, query, chain_type, k):
66
- # load document
67
  loader = PyPDFLoader(file)
68
  documents = loader.load()
69
- # split the documents into chunks
70
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
71
  texts = text_splitter.split_documents(documents)
72
- # select which embeddings we want to use
73
  embeddings = OpenAIEmbeddings()
74
- # create the vectorestore to use as the index
75
  db = Chroma.from_documents(texts, embeddings)
76
- # expose this index in a retriever interface
77
  retriever = db.as_retriever(
78
  search_type="similarity", search_kwargs={"k": k})
79
- # create a chain to answer questions
80
  qa = RetrievalQA.from_chain_type(
81
  llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
82
  result = qa({"query": query})
 
83
  print(result['result'])
84
  return result
85
 
@@ -87,13 +90,14 @@ def qa(file, query, chain_type, k):
87
  # In[6]:
88
 
89
 
90
- convos = [] # store all panel objects in a list
91
-
92
 
 
93
  def qa_result(_):
 
94
  os.environ["OPENAI_API_KEY"] = openaikey.value
95
 
96
- # save pdf file to a temp file
97
  if file_input.value is not None:
98
  file_input.save("/.cache/temp.pdf")
99
 
@@ -121,32 +125,23 @@ def qa_result(_):
121
  return pn.Column(*convos, margin=15, width=575, min_height=400)
122
 
123
 
124
- # In[7]:
125
-
126
-
127
  qa_interactive = pn.panel(
128
  pn.bind(qa_result, run_button),
129
  loading_indicator=True,
130
  )
131
 
132
 
133
- # In[8]:
134
-
135
-
136
  output = pn.WidgetBox('*Output will show up here:*',
137
  qa_interactive, width=630, scroll=True)
138
 
139
 
140
- # In[9]:
141
-
142
-
143
- # layout
144
  pn.Column(
145
- pn.pane.Markdown("""
146
- ## \U0001F60A! Question Answering with your PDF file
147
-
148
- 1) Upload a PDF. 2) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account). 3) Type a question and click "Run".
149
-
150
  """),
151
  pn.Row(file_input, openaikey),
152
  output,
 
1
  #!/usr/bin/env python
2
  # coding: utf-8
3
 
4
+ #! pip install langchain openai chromadb tiktoken pypdf panel
 
5
 
6
+ # 注释1:首先,代码导入了所需的库和模块。这包括用于处理 PDF 文件、创建文本嵌入、搜索、问答等的模块。
7
 
8
  import os
9
  from langchain.chains import RetrievalQA
 
28
  )
29
 
30
 
31
+ # 注释2:然后,代码设置了一些 Panel 库的参数和小部件,用于创建用户界面。这些小部件包括文件输入框、密码输入框、文本编辑器、按钮、滑块等。这些小部件被组织在一个列中,方便用户输入。
32
 
33
 
34
  file_input = pn.widgets.FileInput(width=300)
 
59
  )
60
 
61
 
62
+ #注释3:qa 函数定义了处理 PDF 文件、创建向量嵌入、执行搜索和返回答案的主要逻辑。它接受一个 PDF 文件、查询问题、搜索类型和返回的结果数量作为输入。
63
+ #然后,使用 OpenAI 的模型为每个文本块创建一个向量嵌入,这些嵌入被用来创建一个向量存储,用于后续的搜索。接着,使用这个向量存储创建一个检索器,然后使用这个检索器和 OpenAI 的模型创建一个问答链来回答问题。
64
+ #最后,函数打印出结果并返回。
65
 
66
 
67
  def qa(file, query, chain_type, k):
68
+ # load document 加载PDF文件
69
  loader = PyPDFLoader(file)
70
  documents = loader.load()
71
+ # split the documents into chunks 将PDF文件分割成小块。
72
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
73
  texts = text_splitter.split_documents(documents)
74
+ # select which embeddings we want to use 使用 OpenAI 的embeddings模型为每个文本块创建一个向量嵌入
75
  embeddings = OpenAIEmbeddings()
76
+ # create the VectorStore to use as the index 这些嵌入被用来创建一个向量存储VectorStore,用于后续的搜索。
77
  db = Chroma.from_documents(texts, embeddings)
78
+ # expose this index in a retriever interface 接着,使用这个向量存储创建一个检索器retriever
79
  retriever = db.as_retriever(
80
  search_type="similarity", search_kwargs={"k": k})
81
+ # create a chain to answer questions 然后使用这个检索器和 OpenAI 的模型创建一个问答链来回答问题。
82
  qa = RetrievalQA.from_chain_type(
83
  llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
84
  result = qa({"query": query})
85
+ # 最后,函数打印出结果并返回。
86
  print(result['result'])
87
  return result
88
 
 
90
  # In[6]:
91
 
92
 
93
+ convos = [] # store all panel objects in a list convos是对话列表的意思
 
94
 
95
+ #qa_result 函数是用于处理用户界面输入和调用 qa 函数的函数。它首先从环境变量中获取 OpenAI 的 API 密钥,然后保存用户上传的 PDF 文件。如果用户输入了问题,函数将调用 qa 函数,并将结果添加到对话列表中。
96
  def qa_result(_):
97
+ #首先从环境变量中获取 OpenAI 的 API 密钥
98
  os.environ["OPENAI_API_KEY"] = openaikey.value
99
 
100
+ # save pdf file to a temp file 保存用户上传的 PDF文件为temp.pdf
101
  if file_input.value is not None:
102
  file_input.save("/.cache/temp.pdf")
103
 
 
125
  return pn.Column(*convos, margin=15, width=575, min_height=400)
126
 
127
 
128
+ # In[7]:创建了一个交互式的 Panel 小部件,当用户点击运行按钮时,会调用 qa_result 函数。
 
 
129
  qa_interactive = pn.panel(
130
  pn.bind(qa_result, run_button),
131
  loading_indicator=True,
132
  )
133
 
134
 
135
+ # In[8]:创建输出框
 
 
136
  output = pn.WidgetBox('*Output will show up here:*',
137
  qa_interactive, width=630, scroll=True)
138
 
139
 
140
+ # 界面设计
 
 
 
141
  pn.Column(
142
+ pn.panel.Markdown("""
143
+ ##你可以问我关于你上传的PDF文件的任何信息!
144
+ 1) 上传一个PDF文件. 2)输入你的OpenAI API key.这将产生费用 3) 输入问题然后点击"Run".
 
 
145
  """),
146
  pn.Row(file_input, openaikey),
147
  output,