chomakov commited on
Commit
6a23cd5
1 Parent(s): 7db9219

Upload GPT-4_PDF_summary.py

Browse files
Files changed (1) hide show
  1. GPT-4_PDF_summary.py +18 -19
GPT-4_PDF_summary.py CHANGED
@@ -1,16 +1,11 @@
1
  #!/usr/bin/env python
2
  # coding: utf-8
3
 
 
4
  # In[ ]:
5
 
6
 
7
- get_ipython().system('pip install langchain openai chromadb tiktoken pypdf panel')
8
-
9
-
10
- # In[ ]:
11
-
12
-
13
- import os
14
  from langchain.chains import RetrievalQA
15
  from langchain.llms import OpenAI
16
  from langchain.document_loaders import TextLoader
@@ -50,7 +45,7 @@ select_k = pn.widgets.IntSlider(
50
  name="Number of relevant chunks", start=1, end=5, step=1, value=2
51
  )
52
  select_chain_type = pn.widgets.RadioButtonGroup(
53
- name='Chain type',
54
  options=['stuff', 'map_reduce', "refine", "map_rerank"]
55
  )
56
 
@@ -79,8 +74,9 @@ def qa(file, query, chain_type, k):
79
  # create the vectorestore to use as the index
80
  db = Chroma.from_documents(texts, embeddings)
81
  # expose this index in a retriever interface
82
- retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
83
- # create a chain to answer questions
 
84
  qa = RetrievalQA.from_chain_type(
85
  llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
86
  result = qa({"query": query})
@@ -93,16 +89,18 @@ def qa(file, query, chain_type, k):
93
 
94
  convos = [] # store all panel objects in a list
95
 
 
96
  def qa_result(_):
97
  os.environ["OPENAI_API_KEY"] = openaikey.value
98
-
99
- # save pdf file to a temp file
100
  if file_input.value is not None:
101
  file_input.save("/.cache/temp.pdf")
102
-
103
  prompt_text = prompt.value
104
  if prompt_text:
105
- result = qa(file="/.cache/temp.pdf", query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
 
106
  convos.extend([
107
  pn.Row(
108
  pn.panel("\U0001F60A", width=10),
@@ -114,11 +112,12 @@ def qa_result(_):
114
  pn.Column(
115
  result["result"],
116
  "Relevant source text:",
117
- pn.pane.Markdown('\n--------------------------------------------------------------------\n'.join(doc.page_content for doc in result["source_documents"]))
 
118
  )
119
  )
120
  ])
121
- #return convos
122
  return pn.Column(*convos, margin=15, width=575, min_height=400)
123
 
124
 
@@ -134,7 +133,8 @@ qa_interactive = pn.panel(
134
  # In[8]:
135
 
136
 
137
- output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)
 
138
 
139
 
140
  # In[9]:
@@ -148,9 +148,8 @@ pn.Column(
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,
153
  widgets
154
 
155
  ).servable()
156
-
 
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
10
  from langchain.llms import OpenAI
11
  from langchain.document_loaders import TextLoader
 
45
  name="Number of relevant chunks", start=1, end=5, step=1, value=2
46
  )
47
  select_chain_type = pn.widgets.RadioButtonGroup(
48
+ name='Chain type',
49
  options=['stuff', 'map_reduce', "refine", "map_rerank"]
50
  )
51
 
 
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})
 
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
+
100
  prompt_text = prompt.value
101
  if prompt_text:
102
+ result = qa(file="/.cache/temp.pdf", query=prompt_text,
103
+ chain_type=select_chain_type.value, k=select_k.value)
104
  convos.extend([
105
  pn.Row(
106
  pn.panel("\U0001F60A", width=10),
 
112
  pn.Column(
113
  result["result"],
114
  "Relevant source text:",
115
+ pn.pane.Markdown('\n--------------------------------------------------------------------\n'.join(
116
+ doc.page_content for doc in result["source_documents"]))
117
  )
118
  )
119
  ])
120
+ # return convos
121
  return pn.Column(*convos, margin=15, width=575, min_height=400)
122
 
123
 
 
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]:
 
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,
153
  widgets
154
 
155
  ).servable()