bhaskartripathi commited on
Commit
e0a4aa4
1 Parent(s): ca8acf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import urllib.request
2
  import fitz
3
  import re
@@ -136,12 +137,19 @@ def generate_answer(question,openAI_key):
136
  return answer
137
 
138
 
139
- def question_answer(directory_path, question, openAI_key):
140
  if openAI_key.strip() == '':
141
  return '[ERROR]: Please enter your Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
142
 
143
- if not os.path.isdir(directory_path):
144
- return '[ERROR]: Invalid directory path.'
 
 
 
 
 
 
 
145
 
146
  load_recommender(directory_path)
147
 
@@ -166,15 +174,13 @@ with gr.Blocks() as demo:
166
  with gr.Group():
167
  gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
168
  openAI_key = gr.Textbox(label='Enter your OpenAI API key here')
169
- directory_path = gr.Textbox(label='Enter the directory path containing PDF files')
170
  question = gr.Textbox(label='Enter your question here')
171
  btn = gr.Button(value='Submit')
172
 
173
  with gr.Group():
174
  answer = gr.Textbox(label='The answer to your question is :')
175
 
176
- #btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
177
- btn.click(question_answer, inputs=[directory_path, question, openAI_key], outputs=[answer])
178
-
179
  #openai.api_key = os.getenv('Your_Key_Here')
180
  demo.launch()
 
1
+ import glob
2
  import urllib.request
3
  import fitz
4
  import re
 
137
  return answer
138
 
139
 
140
+ def question_answer(files, question, openAI_key):
141
  if openAI_key.strip() == '':
142
  return '[ERROR]: Please enter your Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
143
 
144
+ if len(files) == 0:
145
+ return '[ERROR]: No PDF files uploaded.'
146
+
147
+ directory_path = os.path.join(os.getcwd(), 'uploaded_files')
148
+ if not os.path.exists(directory_path):
149
+ os.makedirs(directory_path)
150
+
151
+ for file in files:
152
+ file.save(os.path.join(directory_path, file.name))
153
 
154
  load_recommender(directory_path)
155
 
 
174
  with gr.Group():
175
  gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
176
  openAI_key = gr.Textbox(label='Enter your OpenAI API key here')
177
+ files = gr.Filebox(label='Upload PDF files', accept='.pdf', multiple=True)
178
  question = gr.Textbox(label='Enter your question here')
179
  btn = gr.Button(value='Submit')
180
 
181
  with gr.Group():
182
  answer = gr.Textbox(label='The answer to your question is :')
183
 
184
+ btn.click(question_answer, inputs=[files, question, openAI_key], outputs=[answer])
 
 
185
  #openai.api_key = os.getenv('Your_Key_Here')
186
  demo.launch()