bhaskartripathi commited on
Commit
632bc83
·
1 Parent(s): 6aa5a94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -88,16 +88,13 @@ def load_recommender(paths, start_page=1):
88
 
89
  for path in paths:
90
  texts = pdf_to_text(path, start_page=start_page)
91
- all_texts.extend([(text, path) for text in texts])
92
-
93
- chunks = []
94
- for text, path in all_texts:
95
- pdf_chunks = text_to_chunks([text], start_page=start_page)
96
- pdf_chunks = [f'[PDF {os.path.basename(path)}, {chunk}]' for chunk in pdf_chunks]
97
- chunks.extend(pdf_chunks)
98
 
 
99
  recommender.fit(chunks)
100
  return 'Corpus Loaded.'
 
 
101
  def generate_text(openAI_key, prompt, engine="text-davinci-003"):
102
  openai.api_key = openAI_key
103
  completions = openai.Completion.create(
@@ -133,32 +130,33 @@ def generate_answer(question, openAI_key):
133
 
134
  def question_answer(url, files, question, openAI_key):
135
  if openAI_key.strip() == '':
136
- return '[ERROR]: Please enter your Open AI Key. Get your key here: https://platform.openai.com/account/api-keys'
137
  if url.strip() == '' and not files:
138
  return '[ERROR]: Both URL and PDF are empty. Provide at least one.'
139
-
140
  if url.strip() != '' and files:
141
  return '[ERROR]: Both URL and PDF are provided. Please provide only one (either URL or PDF).'
142
 
 
 
143
  if url.strip() != '':
144
  glob_url = url
145
- download_pdf(glob_url, 'corpus.pdf')
146
- load_recommender(['corpus.pdf'])
 
147
 
148
  else:
149
- file_paths = []
150
  for file in files:
151
  old_file_name = file.name
152
  file_name = file.name
153
  file_name = file_name[:-12] + file_name[-4:]
154
  os.rename(old_file_name, file_name)
155
- file_paths.append(file_name)
156
-
157
- load_recommender(file_paths)
158
 
159
  if question.strip() == '':
160
  return '[ERROR]: Question field is empty'
161
 
 
162
  return generate_answer(question, openAI_key)
163
 
164
  recommender = SemanticSearch()
 
88
 
89
  for path in paths:
90
  texts = pdf_to_text(path, start_page=start_page)
91
+ all_texts.extend(texts)
 
 
 
 
 
 
92
 
93
+ chunks = text_to_chunks(all_texts, start_page=start_page)
94
  recommender.fit(chunks)
95
  return 'Corpus Loaded.'
96
+
97
+
98
  def generate_text(openAI_key, prompt, engine="text-davinci-003"):
99
  openai.api_key = openAI_key
100
  completions = openai.Completion.create(
 
130
 
131
  def question_answer(url, files, question, openAI_key):
132
  if openAI_key.strip() == '':
133
+ return '[ERROR]: Please enter your Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
134
  if url.strip() == '' and not files:
135
  return '[ERROR]: Both URL and PDF are empty. Provide at least one.'
136
+
137
  if url.strip() != '' and files:
138
  return '[ERROR]: Both URL and PDF are provided. Please provide only one (either URL or PDF).'
139
 
140
+ pdf_paths = []
141
+
142
  if url.strip() != '':
143
  glob_url = url
144
+ output_path = 'corpus.pdf'
145
+ download_pdf(glob_url, output_path)
146
+ pdf_paths.append(output_path)
147
 
148
  else:
 
149
  for file in files:
150
  old_file_name = file.name
151
  file_name = file.name
152
  file_name = file_name[:-12] + file_name[-4:]
153
  os.rename(old_file_name, file_name)
154
+ pdf_paths.append(file_name)
 
 
155
 
156
  if question.strip() == '':
157
  return '[ERROR]: Question field is empty'
158
 
159
+ load_recommender(pdf_paths)
160
  return generate_answer(question, openAI_key)
161
 
162
  recommender = SemanticSearch()