dlflannery commited on
Commit
beb50d0
·
verified ·
1 Parent(s): 519099f

Update app.py

Browse files

multiple file and image uploads

Files changed (1) hide show
  1. app.py +95 -91
app.py CHANGED
@@ -183,33 +183,35 @@ def clean_up_files():
183
  except:
184
  pass
185
 
186
- def load_image(image, user, output_window):
187
  # status = #'OK, image is ready! Enter prompt and tap submit button'
188
  try:
189
  with open(image, 'rb') as image_file:
190
  base64_image = base64.b64encode(image_file.read()).decode('utf-8')
191
- fpath = user + '_image.b64'
192
  with open(fpath, 'wt') as fp:
193
  fp.write(base64_image)
194
- output_window += md('\nImage loaded\n')
 
195
  except:
196
  output_window = 'Unable to upload image'
197
- return [fpath, output_window]
198
 
199
  def upload_image(user, password, output_window):
200
  if not credentials_ok(user, password):
201
  return [gr.Image(visible=False, interactive=True), "Incorrect user name and/or password"]
202
- return [gr.Image(visible=True, interactive=True), output_window]
203
 
204
  def upload_file(user, password, output_window):
205
  if not credentials_ok(user, password):
206
  return [gr.File(visible=False, label='Upload File'), 'Incorrect user and/or password']
207
- return [gr.File(visible=True, label='UploadFile'), output_window]
208
 
209
- def load_file(file_uploader, output_window):
210
  path = file_uploader
211
  fname = os.path.basename(path)
212
- return [path, output_window + f'<BR>{fname} loaded<BR>',
 
213
  gr.File(visible=False, label='Upload File', type='filepath', value=None) ]
214
 
215
  def create_openai_container(name):
@@ -247,12 +249,12 @@ def list_openai_container_files(container_id):
247
 
248
 
249
  async def chat(prompt_window, user_window, password, history, output_window,
250
- uploaded_image_file, uploaded_file_path, prior_inputs):
251
  file_download = gr.DownloadButton(label='Download File', visible=False, value=None)
252
  image_window = gr.Image(visible=False, value=None)
253
 
254
  if not credentials_ok(user_window, password):
255
- return ['Invalid Credentials', prompt_window, uploaded_image_file,
256
  image_window, file_download, history, uploaded_file_path, prior_inputs]
257
  instructions = '''
258
  You are a helpful assistant.
@@ -279,60 +281,62 @@ async def chat(prompt_window, user_window, password, history, output_window,
279
  # inputs = history.copy()
280
  inputs = prior_inputs
281
  file_input = ''
282
- if uploaded_file_path != '':
283
- ext = uploaded_file_path.casefold().split('.')[-1]
284
- if ext == 'pdf':
285
- client = OpenAI(api_key = OPENAI_API_KEY)
286
- file = client.files.create(file=open(f'{uploaded_file_path}','rb'),
287
- purpose='user_data',
288
- expires_after={"seconds": 3600, "anchor": "created_at"})
289
- file_input=(
290
- {"role": "user",
291
- "content": [
292
- {
293
- "type": "input_file",
294
- "file_id": file.id,
 
 
 
295
  }
296
- ]
297
- }
298
- )
299
- inputs.append(file_input)
300
- if ext in ['docx', 'txt', 'py']:
301
- if ext == 'docx':
302
- extracted_text = extract_text_from_docx(uploaded_file_path)
303
- else:
304
- with open(uploaded_file_path, 'rt') as fp:
305
- extracted_text = fp.read()
306
- file_input=(
307
- {"role": "user",
308
- "content": [
309
- {
310
- "type": "input_text",
311
- "text": f"{extracted_text}",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  }
313
  ]
314
  }
315
  )
316
- inputs.append(file_input)
317
- uploaded_file_path == ''
318
- image_input = ''
319
- if uploaded_image_file != '':
320
- with open(uploaded_image_file, 'rt') as fp:
321
- b64data = fp.read()
322
- os.remove(uploaded_image_file)
323
- uploaded_image_file = ''
324
- image_input = (
325
- {
326
- "role": "user",
327
- "content": [
328
- {
329
- "type": "input_image",
330
- "image_url": f'data:image/jpeg;base64, {b64data}',
331
- }
332
- ]
333
- }
334
- )
335
- inputs.append(image_input)
336
  history.append({"role":"user", "content":prompt})
337
  inputs.append({"role":"user", "content":prompt})
338
  exception_msg = ''
@@ -385,12 +389,9 @@ async def chat(prompt_window, user_window, password, history, output_window,
385
  fp.write(fdata)
386
  file_download = gr.DownloadButton(label=f'Download {download_ext.upper()} Doc',
387
  visible=True, value=f'./document.{download_ext}')
388
- return [response, '', uploaded_image_file, image_window, file_download, history,
389
  uploaded_file_path, new_inputs]
390
 
391
- # outputs=[history, output_window, prompt_window, uploaded_image_file,
392
- # image_window, file_download])
393
-
394
  def show_help():
395
  txt = '''
396
  This is an agent using the OpenAI Python Agents SDK.
@@ -398,34 +399,35 @@ def show_help():
398
  * Search the Web
399
  * Compute straight-line distances between locations
400
  * Analyze images you upload.
401
- * Create and display images you describe, which you can download
402
- * Get news from the web
403
- * Make PDF's based on results it generated.
 
404
  Agents perform multiple steps using tools as necessary to satisfy a single request.
405
 
406
  1. Gemeral:
407
  1.1 Login with user name and password (not case-sensitive)
408
  1.2 Type prompts (questions, instructions) into "Prompt or Question" window.
409
  2. Chat:
410
- 2.1 Enter prompt and tap the "Submit Prompt/Question" button. The responses appear in the Dialog window.
411
- 2.2 Enter follow-up questions in the Prompt window. Tap "Submit Prompt/Question".
412
- 2.3 If topic changes or when done chatting, tap the "Start New Session" button.
 
 
 
 
 
413
  3. Make Image:
414
- 3.1 Include description of desired image in prompt window.
 
415
  3.2 Tap the "Submit Prompt/Question" button. This can take a few seconds.
416
  3.3 There is a download button on the image display if your system supports file downloads.
417
  3.4 When done viewing image, tap the "Start New Session" button
418
- 4. Analyze an Image you provide:
419
- 4.1 Tap the "Upload Image to Analyze" button.
420
- 4.2 An empty image box will appear lower left. Drag or upload image into it. It offers web cam or camera
421
- input also.
422
- 4.3 The image should appear. This can take some time with a slow internet connection and large image.
423
- 4.4 Enter what you want done with the image in the "Prompt or Question" box.
424
- 4.5 Tap the "Submit Prompt/Question" button to start the analysis. This initiates a chat dialog and
425
- you can ask follow-up questions. However, the image is not re-analyzed for follow-up dialog.
426
- Hint:
427
  Better results are obtained by including detailed descriptions and instructions
428
  of what you want in the prompt.
 
 
429
  '''
430
  return str(txt).replace('```', ' ').replace(' ', '&nbsp;&nbsp;').replace(' ', '&nbsp;&nbsp;').replace(' ', '&nbsp;&nbsp;').replace('\n','<br>')
431
 
@@ -434,7 +436,7 @@ def new_session(user_window, history):
434
  history = []
435
  return [prompt_window, history, 'Session cleared',
436
  gr.Image(visible=False, value=None),
437
- gr.Image(visible=False, value=None), '',
438
  gr.DownloadButton(label='Download File', visible=False, value=None),
439
  gr.File(visible=False, label='Upload File', type='filepath'), [] ]
440
 
@@ -442,8 +444,8 @@ def new_session(user_window, history):
442
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
443
  password = gr.State("")
444
  user = gr.State("unknown")
445
- uploaded_image_file = gr.State('')
446
- uploaded_file_path = gr.State('')
447
  history = gr.State([])
448
  inputs = gr.State([])
449
 
@@ -467,32 +469,34 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
467
  file_download = gr.DownloadButton(label='Download File', visible=False, value=None)
468
  with gr.Row():
469
  with gr.Column():
470
- image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze', type='filepath')
 
471
  with gr.Column():
472
  image_window = gr.Image(visible=False, label='Generated Image')
473
  with gr.Row():
474
  file_uploader = gr.File(visible=False, label='Upload File', type='filepath')
475
  submit_button.click(chat,
476
  inputs=[prompt_window, user_window, password, history, output_window,
477
- uploaded_image_file, uploaded_file_path, inputs],
478
- outputs=[output_window, prompt_window, uploaded_image_file,
479
- image_window, file_download, history, uploaded_file_path, inputs])
480
  clear_button.click(fn=new_session, inputs=[user_window, history],
481
  outputs=[prompt_window, history, output_window,
482
  image_window, image_window2,
483
- uploaded_image_file, file_download, file_uploader, inputs])
484
  help_button.click(fn=show_help, outputs=output_window)
485
  button_get_image.click(fn=upload_image,inputs = [user, password, output_window],
486
  outputs = [image_window2, output_window])
487
- image_window2.upload(fn=load_image, inputs=[image_window2, user, output_window],
488
- outputs=[uploaded_image_file, output_window])
 
489
  pwd_window.blur(updatePassword,
490
  inputs = [pwd_window, user],
491
  outputs = [password, pwd_window, button_upload_file, button_get_image])
492
  button_upload_file.click(fn=upload_file, inputs=[user, password, output_window],
493
  outputs=[file_uploader, output_window])
494
- file_uploader.upload(fn=load_file, inputs=[file_uploader, output_window],
495
- outputs=[uploaded_file_path, output_window, file_uploader])
496
  # demo.launch(share=True, allowed_paths=[dataDir], ssr_mode=False)
497
  # demo.load(delete_db_files)
498
  demo.unload(clean_up_files)
 
183
  except:
184
  pass
185
 
186
+ def load_image(image, user, output_window, uploaded_image_files):
187
  # status = #'OK, image is ready! Enter prompt and tap submit button'
188
  try:
189
  with open(image, 'rb') as image_file:
190
  base64_image = base64.b64encode(image_file.read()).decode('utf-8')
191
+ fpath = f'{user}_image{len(uploaded_image_files)}.b64'
192
  with open(fpath, 'wt') as fp:
193
  fp.write(base64_image)
194
+ output_window += md(f'\nImage {os.path.basename(image)} loaded\n')
195
+ uploaded_image_files.append(fpath)
196
  except:
197
  output_window = 'Unable to upload image'
198
+ return [uploaded_image_files, output_window]
199
 
200
  def upload_image(user, password, output_window):
201
  if not credentials_ok(user, password):
202
  return [gr.Image(visible=False, interactive=True), "Incorrect user name and/or password"]
203
+ return [gr.Image(visible=True, interactive=True, value=None), output_window]
204
 
205
  def upload_file(user, password, output_window):
206
  if not credentials_ok(user, password):
207
  return [gr.File(visible=False, label='Upload File'), 'Incorrect user and/or password']
208
+ return [gr.File(visible=True, label='UploadFile', value=None), output_window]
209
 
210
+ def load_file(file_uploader, output_window, uploaded_file_paths):
211
  path = file_uploader
212
  fname = os.path.basename(path)
213
+ uploaded_file_paths.append(path)
214
+ return [uploaded_file_paths, output_window + f'<br>{fname} loaded<br>',
215
  gr.File(visible=False, label='Upload File', type='filepath', value=None) ]
216
 
217
  def create_openai_container(name):
 
249
 
250
 
251
  async def chat(prompt_window, user_window, password, history, output_window,
252
+ uploaded_image_files, uploaded_file_paths, prior_inputs):
253
  file_download = gr.DownloadButton(label='Download File', visible=False, value=None)
254
  image_window = gr.Image(visible=False, value=None)
255
 
256
  if not credentials_ok(user_window, password):
257
+ return ['Invalid Credentials', prompt_window, uploaded_image_files,
258
  image_window, file_download, history, uploaded_file_path, prior_inputs]
259
  instructions = '''
260
  You are a helpful assistant.
 
281
  # inputs = history.copy()
282
  inputs = prior_inputs
283
  file_input = ''
284
+ if len(uploaded_file_paths) > 0:
285
+ for uploaded_file_path in uploaded_file_paths:
286
+ ext = uploaded_file_path.casefold().split('.')[-1]
287
+ if ext == 'pdf':
288
+ client = OpenAI(api_key = OPENAI_API_KEY)
289
+ file = client.files.create(file=open(f'{uploaded_file_path}','rb'),
290
+ purpose='user_data',
291
+ expires_after={"seconds": 3600, "anchor": "created_at"})
292
+ file_input=(
293
+ {"role": "user",
294
+ "content": [
295
+ {
296
+ "type": "input_file",
297
+ "file_id": file.id,
298
+ }
299
+ ]
300
  }
301
+ )
302
+ inputs.append(file_input)
303
+ if ext in ['docx', 'txt', 'py']:
304
+ if ext == 'docx':
305
+ extracted_text = extract_text_from_docx(uploaded_file_path)
306
+ else:
307
+ with open(uploaded_file_path, 'rt') as fp:
308
+ extracted_text = fp.read()
309
+ file_input=(
310
+ {"role": "user",
311
+ "content": [
312
+ {
313
+ "type": "input_text",
314
+ "text": f"{extracted_text}",
315
+ }
316
+ ]
317
+ }
318
+ )
319
+ inputs.append(file_input)
320
+ uploaded_file_paths = []
321
+ image_input = ''
322
+ if len(uploaded_image_files) > 0:
323
+ for file in uploaded_image_files:
324
+ with open(file, 'rt') as fp:
325
+ b64data = fp.read()
326
+ os.remove(file)
327
+ image_input = (
328
+ {
329
+ "role": "user",
330
+ "content": [
331
+ {
332
+ "type": "input_image",
333
+ "image_url": f'data:image/jpeg;base64, {b64data}',
334
  }
335
  ]
336
  }
337
  )
338
+ inputs.append(image_input)
339
+ uploaded_image_files = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  history.append({"role":"user", "content":prompt})
341
  inputs.append({"role":"user", "content":prompt})
342
  exception_msg = ''
 
389
  fp.write(fdata)
390
  file_download = gr.DownloadButton(label=f'Download {download_ext.upper()} Doc',
391
  visible=True, value=f'./document.{download_ext}')
392
+ return [response, '', uploaded_image_files, image_window, file_download, history,
393
  uploaded_file_path, new_inputs]
394
 
 
 
 
395
  def show_help():
396
  txt = '''
397
  This is an agent using the OpenAI Python Agents SDK.
 
399
  * Search the Web
400
  * Compute straight-line distances between locations
401
  * Analyze images you upload.
402
+ * Create and display images you describe, which you can download.
403
+ * Use uploaded images and documents as context. (.txt., .pdf, .docx, .py)
404
+ * Get news from the web.
405
+ * Make PDF's, Word Documents and Excel spreadsheets based on results it generated.
406
  Agents perform multiple steps using tools as necessary to satisfy a single request.
407
 
408
  1. Gemeral:
409
  1.1 Login with user name and password (not case-sensitive)
410
  1.2 Type prompts (questions, instructions) into "Prompt or Question" window.
411
  2. Chat:
412
+ 2.1 Upload any image(s) and/or documents (files) you want the agent to consider, using
413
+ the "Upload Image to Analyze" and "Upload Input File" buttons.
414
+ 2.2 Enter prompt/question and tap the "Submit Prompt/Question" button. The responses appear
415
+ in the Dialog window.
416
+ 2.3 Continue your session by optionally uploading more files and/or images and entering a
417
+ new prompt/question. The agent remembers past inputs and responses until you tap
418
+ the "Start New Session" button.
419
+ 2.4 If topic changes or when done chatting, tap the "Start New Session" button.
420
  3. Make Image:
421
+ 3.1 Include description of desired image in prompt window. If desired, uploaded images and
422
+ files can also be used.
423
  3.2 Tap the "Submit Prompt/Question" button. This can take a few seconds.
424
  3.3 There is a download button on the image display if your system supports file downloads.
425
  3.4 When done viewing image, tap the "Start New Session" button
426
+ Hints:
 
 
 
 
 
 
 
 
427
  Better results are obtained by including detailed descriptions and instructions
428
  of what you want in the prompt.
429
+ Start a new session whenever memory of previous inputs and responses is no longer
430
+ needed as context. The agent can only remember so much.
431
  '''
432
  return str(txt).replace('```', ' ').replace(' ', '&nbsp;&nbsp;').replace(' ', '&nbsp;&nbsp;').replace(' ', '&nbsp;&nbsp;').replace('\n','<br>')
433
 
 
436
  history = []
437
  return [prompt_window, history, 'Session cleared',
438
  gr.Image(visible=False, value=None),
439
+ gr.Image(visible=False, value=None), [],
440
  gr.DownloadButton(label='Download File', visible=False, value=None),
441
  gr.File(visible=False, label='Upload File', type='filepath'), [] ]
442
 
 
444
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
445
  password = gr.State("")
446
  user = gr.State("unknown")
447
+ uploaded_image_files = gr.State([])
448
+ uploaded_file_paths = gr.State([])
449
  history = gr.State([])
450
  inputs = gr.State([])
451
 
 
469
  file_download = gr.DownloadButton(label='Download File', visible=False, value=None)
470
  with gr.Row():
471
  with gr.Column():
472
+ image_window2 = gr.Image(visible=False, interactive=True, label='Image to Analyze',
473
+ type='filepath')
474
  with gr.Column():
475
  image_window = gr.Image(visible=False, label='Generated Image')
476
  with gr.Row():
477
  file_uploader = gr.File(visible=False, label='Upload File', type='filepath')
478
  submit_button.click(chat,
479
  inputs=[prompt_window, user_window, password, history, output_window,
480
+ uploaded_image_files, uploaded_file_paths, inputs],
481
+ outputs=[output_window, prompt_window, uploaded_image_files,
482
+ image_window, file_download, history, uploaded_file_paths, inputs])
483
  clear_button.click(fn=new_session, inputs=[user_window, history],
484
  outputs=[prompt_window, history, output_window,
485
  image_window, image_window2,
486
+ uploaded_image_files, file_download, file_uploader, inputs])
487
  help_button.click(fn=show_help, outputs=output_window)
488
  button_get_image.click(fn=upload_image,inputs = [user, password, output_window],
489
  outputs = [image_window2, output_window])
490
+ image_window2.upload(fn=load_image,
491
+ inputs=[image_window2, user, output_window, uploaded_image_files],
492
+ outputs=[uploaded_image_files, output_window])
493
  pwd_window.blur(updatePassword,
494
  inputs = [pwd_window, user],
495
  outputs = [password, pwd_window, button_upload_file, button_get_image])
496
  button_upload_file.click(fn=upload_file, inputs=[user, password, output_window],
497
  outputs=[file_uploader, output_window])
498
+ file_uploader.upload(fn=load_file, inputs=[file_uploader, output_window, uploaded_file_paths],
499
+ outputs=[uploaded_file_paths, output_window, file_uploader])
500
  # demo.launch(share=True, allowed_paths=[dataDir], ssr_mode=False)
501
  # demo.load(delete_db_files)
502
  demo.unload(clean_up_files)