mjbuehler commited on
Commit
0f6029d
1 Parent(s): 430da9d

Update app.py

Browse files

Added edit functionality

Files changed (1) hide show
  1. app.py +117 -29
app.py CHANGED
@@ -323,30 +323,35 @@ def generate_audio(
323
  scratch_pad_instructions: str = '',
324
  prelude_dialog: str = '',
325
  podcast_dialog_instructions: str = '',
326
- ) -> bytes:
 
 
 
 
327
  # Validate API Key
328
  if not os.getenv("OPENAI_API_KEY") and not openai_api_key:
329
  raise gr.Error("OpenAI API key is required")
330
 
331
- combined_text = ""
332
 
333
- # Loop through each uploaded file and extract text
334
- for file in files:
335
- with Path(file).open("rb") as f:
336
- reader = PdfReader(f)
337
- text = "\n\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
338
- combined_text += text + "\n\n" # Add separation between different files' texts
 
339
 
340
  # Configure the LLM based on selected model and api_base
341
  @retry(retry=retry_if_exception_type(ValidationError))
342
  @conditional_llm(model=text_model, api_base=api_base, api_key=openai_api_key)
343
  def generate_dialogue(text: str, intro_instructions: str, text_instructions: str, scratch_pad_instructions: str,
344
  prelude_dialog: str, podcast_dialog_instructions: str,
345
- ) -> Dialogue:
346
  """
347
  {intro_instructions}
348
 
349
- Here is the input text you will be working with:
350
 
351
  <input_text>
352
  {text}
@@ -363,8 +368,20 @@ def generate_audio(
363
  <podcast_dialogue>
364
  {podcast_dialog_instructions}
365
  </podcast_dialogue>
 
366
  """
367
 
 
 
 
 
 
 
 
 
 
 
 
368
  # Generate the dialogue using the LLM
369
  llm_output = generate_dialogue(
370
  combined_text,
@@ -372,12 +389,14 @@ def generate_audio(
372
  text_instructions=text_instructions,
373
  scratch_pad_instructions=scratch_pad_instructions,
374
  prelude_dialog=prelude_dialog,
375
- podcast_dialog_instructions=podcast_dialog_instructions
 
 
376
  )
377
 
 
378
  audio = b""
379
  transcript = ""
380
-
381
  characters = 0
382
 
383
  with cf.ThreadPoolExecutor() as executor:
@@ -413,14 +432,32 @@ def generate_audio(
413
  if os.path.isfile(file) and time.time() - os.path.getmtime(file) > 24 * 60 * 60:
414
  os.remove(file)
415
 
416
- return temporary_file.name, transcript
417
 
418
  def validate_and_generate_audio(*args):
419
  files = args[0]
420
  if not files:
421
- return None, None, "Please upload at least one PDF file before generating audio."
422
- audio_file, transcript = generate_audio(*args)
423
- return audio_file, transcript, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
  with gr.Blocks(title="PDF to Audio", css="""
426
  #header {
@@ -542,36 +579,87 @@ with gr.Blocks(title="PDF to Audio", css="""
542
  value=INSTRUCTION_TEMPLATES["podcast"]["dialog"],
543
  info="Provide the instructions for generating the presentation or podcast dialogue.",
544
  )
545
-
546
  audio_output = gr.Audio(label="Audio", format="mp3")
547
  transcript_output = gr.Textbox(label="Transcript", lines=20, show_copy_button=True)
 
548
  error_output = gr.Textbox(visible=False) # Hidden textbox to store error message
549
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550
  # Update instruction fields when template is changed
551
  template_dropdown.change(
552
  fn=update_instructions,
553
  inputs=[template_dropdown],
554
  outputs=[intro_instructions, text_instructions, scratch_pad_instructions, prelude_dialog, podcast_dialog_instructions]
555
  )
556
-
557
  submit_btn.click(
558
  fn=validate_and_generate_audio,
559
  inputs=[
560
  files, openai_api_key, text_model, audio_model,
561
  speaker_1_voice, speaker_2_voice, api_base,
562
  intro_instructions, text_instructions, scratch_pad_instructions,
563
- prelude_dialog, podcast_dialog_instructions
 
 
564
  ],
565
- outputs=[
566
- audio_output,
567
- transcript_output,
568
- error_output
569
- ]
570
- ).then(
571
- fn=lambda error: gr.Warning(error) if error else None,
572
- inputs=[error_output],
573
- outputs=[]
574
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
 
576
  # Add README content at the bottom
577
  gr.Markdown("---") # Horizontal line to separate the interface from README
 
323
  scratch_pad_instructions: str = '',
324
  prelude_dialog: str = '',
325
  podcast_dialog_instructions: str = '',
326
+ edited_transcript: str = None,
327
+ user_feedback: str = None,
328
+ original_text: str = None,
329
+ debug = False,
330
+ ) -> tuple:
331
  # Validate API Key
332
  if not os.getenv("OPENAI_API_KEY") and not openai_api_key:
333
  raise gr.Error("OpenAI API key is required")
334
 
335
+ combined_text = original_text or ""
336
 
337
+ # If there's no original text, extract it from the uploaded files
338
+ if not combined_text:
339
+ for file in files:
340
+ with Path(file).open("rb") as f:
341
+ reader = PdfReader(f)
342
+ text = "\n\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
343
+ combined_text += text + "\n\n"
344
 
345
  # Configure the LLM based on selected model and api_base
346
  @retry(retry=retry_if_exception_type(ValidationError))
347
  @conditional_llm(model=text_model, api_base=api_base, api_key=openai_api_key)
348
  def generate_dialogue(text: str, intro_instructions: str, text_instructions: str, scratch_pad_instructions: str,
349
  prelude_dialog: str, podcast_dialog_instructions: str,
350
+ edited_transcript: str = None, user_feedback: str = None, ) -> Dialogue:
351
  """
352
  {intro_instructions}
353
 
354
+ Here is the original input text:
355
 
356
  <input_text>
357
  {text}
 
368
  <podcast_dialogue>
369
  {podcast_dialog_instructions}
370
  </podcast_dialogue>
371
+ {edited_transcript}{user_feedback}
372
  """
373
 
374
+ instruction_improve='Based on the original text, please generate an improved version of the dialogue by incorporating the edits, comments and feedback.'
375
+ edited_transcript_processed="\nPreviously generated edited transcript, with specific edits and comments that I want you to carefully address:\n"+"<edited_transcript>\n"+edited_transcript+"</edited_transcript>" if edited_transcript !="" else ""
376
+ user_feedback_processed="\nOverall user feedback:\n\n"+user_feedback if user_feedback !="" else ""
377
+
378
+ if edited_transcript_processed.strip()!='' or user_feedback_processed.strip()!='':
379
+ user_feedback_processed="<requested_improvements>"+user_feedback_processed+"\n\n"+instruction_improve+"</requested_improvements>"
380
+
381
+ if debug:
382
+ logger.info (edited_transcript_processed)
383
+ logger.info (user_feedback_processed)
384
+
385
  # Generate the dialogue using the LLM
386
  llm_output = generate_dialogue(
387
  combined_text,
 
389
  text_instructions=text_instructions,
390
  scratch_pad_instructions=scratch_pad_instructions,
391
  prelude_dialog=prelude_dialog,
392
+ podcast_dialog_instructions=podcast_dialog_instructions,
393
+ edited_transcript=edited_transcript_processed,
394
+ user_feedback=user_feedback_processed
395
  )
396
 
397
+ # Generate audio from the transcript
398
  audio = b""
399
  transcript = ""
 
400
  characters = 0
401
 
402
  with cf.ThreadPoolExecutor() as executor:
 
432
  if os.path.isfile(file) and time.time() - os.path.getmtime(file) > 24 * 60 * 60:
433
  os.remove(file)
434
 
435
+ return temporary_file.name, transcript, combined_text
436
 
437
  def validate_and_generate_audio(*args):
438
  files = args[0]
439
  if not files:
440
+ return None, None, None, "Please upload at least one PDF file before generating audio."
441
+ try:
442
+ audio_file, transcript, original_text = generate_audio(*args)
443
+ return audio_file, transcript, original_text, None # Return None as the error when successful
444
+ except Exception as e:
445
+ # If an error occurs during generation, return None for the outputs and the error message
446
+ return None, None, None, str(e)
447
+
448
+ def edit_and_regenerate(edited_transcript, user_feedback, *args):
449
+ # Replace the original transcript and feedback in the args with the new ones
450
+ #new_args = list(args)
451
+ #new_args[-2] = edited_transcript # Update edited transcript
452
+ #new_args[-1] = user_feedback # Update user feedback
453
+ return validate_and_generate_audio(*new_args)
454
+
455
+ # New function to handle user feedback and regeneration
456
+ def process_feedback_and_regenerate(feedback, *args):
457
+ # Add user feedback to the args
458
+ new_args = list(args)
459
+ new_args.append(feedback) # Add user feedback as a new argument
460
+ return validate_and_generate_audio(*new_args)
461
 
462
  with gr.Blocks(title="PDF to Audio", css="""
463
  #header {
 
579
  value=INSTRUCTION_TEMPLATES["podcast"]["dialog"],
580
  info="Provide the instructions for generating the presentation or podcast dialogue.",
581
  )
582
+
583
  audio_output = gr.Audio(label="Audio", format="mp3")
584
  transcript_output = gr.Textbox(label="Transcript", lines=20, show_copy_button=True)
585
+ original_text_output = gr.Textbox(label="Original Text", lines=10, visible=False)
586
  error_output = gr.Textbox(visible=False) # Hidden textbox to store error message
587
 
588
+ use_edited_transcript = gr.Checkbox(label="Use Edited Transcript (check if you want to make edits to the initially generated transcript)", value=False)
589
+ edited_transcript = gr.Textbox(label="Edit Transcript Here. E.g., mark edits in the text with clear instructions. E.g., '[ADD DEFINITION OF MATERIOMICS]'.", lines=20, visible=False,
590
+ show_copy_button=True, interactive=False)
591
+
592
+ user_feedback = gr.Textbox(label="Provide Feedback or Notes", lines=10, #placeholder="Enter your feedback or notes here..."
593
+ )
594
+ regenerate_btn = gr.Button("Regenerate Audio with Edits and Feedback")
595
+ # Function to update the interactive state of edited_transcript
596
+ def update_edit_box(checkbox_value):
597
+ return gr.update(interactive=checkbox_value, lines=20 if checkbox_value else 20, visible=True if checkbox_value else False)
598
+
599
+ # Update the interactive state of edited_transcript when the checkbox is toggled
600
+ use_edited_transcript.change(
601
+ fn=update_edit_box,
602
+ inputs=[use_edited_transcript],
603
+ outputs=[edited_transcript]
604
+ )
605
  # Update instruction fields when template is changed
606
  template_dropdown.change(
607
  fn=update_instructions,
608
  inputs=[template_dropdown],
609
  outputs=[intro_instructions, text_instructions, scratch_pad_instructions, prelude_dialog, podcast_dialog_instructions]
610
  )
611
+
612
  submit_btn.click(
613
  fn=validate_and_generate_audio,
614
  inputs=[
615
  files, openai_api_key, text_model, audio_model,
616
  speaker_1_voice, speaker_2_voice, api_base,
617
  intro_instructions, text_instructions, scratch_pad_instructions,
618
+ prelude_dialog, podcast_dialog_instructions,
619
+ edited_transcript, # placeholder for edited_transcript
620
+ user_feedback, # placeholder for user_feedback
621
  ],
622
+ outputs=[audio_output, transcript_output, original_text_output, error_output]
623
+ ).then(
624
+ fn=lambda audio, transcript, original_text, error: (
625
+ transcript if transcript else "",
626
+ error if error else None
627
+ ),
628
+ inputs=[audio_output, transcript_output, original_text_output, error_output],
629
+ outputs=[edited_transcript, error_output]
630
+ ).then(
631
+ fn=lambda error: gr.Warning(error) if error else None,
632
+ inputs=[error_output],
633
+ outputs=[]
634
+ )
635
+
636
+ regenerate_btn.click(
637
+ fn=lambda use_edit, edit, *args: validate_and_generate_audio(
638
+ *args[:12], # All inputs up to podcast_dialog_instructions
639
+ edit if use_edit else "", # Use edited transcript if checkbox is checked, otherwise empty string
640
+ *args[12:] # user_feedback and original_text_output
641
+ ),
642
+ inputs=[
643
+ use_edited_transcript, edited_transcript,
644
+ files, openai_api_key, text_model, audio_model,
645
+ speaker_1_voice, speaker_2_voice, api_base,
646
+ intro_instructions, text_instructions, scratch_pad_instructions,
647
+ prelude_dialog, podcast_dialog_instructions,
648
+ user_feedback, original_text_output
649
+ ],
650
+ outputs=[audio_output, transcript_output, original_text_output, error_output]
651
+ ).then(
652
+ fn=lambda audio, transcript, original_text, error: (
653
+ transcript if transcript else "",
654
+ error if error else None
655
+ ),
656
+ inputs=[audio_output, transcript_output, original_text_output, error_output],
657
+ outputs=[edited_transcript, error_output]
658
+ ).then(
659
+ fn=lambda error: gr.Warning(error) if error else None,
660
+ inputs=[error_output],
661
+ outputs=[]
662
+ )
663
 
664
  # Add README content at the bottom
665
  gr.Markdown("---") # Horizontal line to separate the interface from README