mjbuehler commited on
Commit
c5a11c4
·
verified ·
1 Parent(s): 61371ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -1
app.py CHANGED
@@ -553,13 +553,33 @@ def generate_audio(
553
  combined_text = original_text or ""
554
 
555
  # If there's no original text, extract it from the uploaded files
 
556
  if not combined_text:
557
  for file in files:
558
  with Path(file).open("rb") as f:
559
  reader = PdfReader(f)
560
  text = "\n\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
561
  combined_text += text + "\n\n"
 
562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563
  # Configure the LLM based on selected model and api_base
564
  @retry(retry=retry_if_exception_type(ValidationError))
565
  @conditional_llm(model=text_model, api_base=api_base, api_key=openai_api_key)
@@ -721,7 +741,8 @@ with gr.Blocks(title="PDF to Audio", css="""
721
 
722
  with gr.Row(elem_id="main_container"):
723
  with gr.Column(scale=2):
724
- files = gr.Files(label="PDFs", file_types=["pdf"], )
 
725
 
726
  openai_api_key = gr.Textbox(
727
  label="OpenAI API Key",
 
553
  combined_text = original_text or ""
554
 
555
  # If there's no original text, extract it from the uploaded files
556
+ '''
557
  if not combined_text:
558
  for file in files:
559
  with Path(file).open("rb") as f:
560
  reader = PdfReader(f)
561
  text = "\n\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
562
  combined_text += text + "\n\n"
563
+ '''
564
 
565
+ if not combined_text:
566
+ for file in files:
567
+ file_path = Path(file)
568
+ suffix = file_path.suffix.lower()
569
+
570
+ if suffix == ".pdf":
571
+ with file_path.open("rb") as f:
572
+ reader = PdfReader(f)
573
+ text = "\n\n".join(
574
+ page.extract_text() for page in reader.pages if page.extract_text()
575
+ )
576
+ combined_text += text + "\n\n"
577
+ elif suffix in [".txt", ".md", ".mmd"]:
578
+ with file_path.open("r", encoding="utf-8") as f:
579
+ text = f.read()
580
+ combined_text += text + "\n\n"
581
+
582
+
583
  # Configure the LLM based on selected model and api_base
584
  @retry(retry=retry_if_exception_type(ValidationError))
585
  @conditional_llm(model=text_model, api_base=api_base, api_key=openai_api_key)
 
741
 
742
  with gr.Row(elem_id="main_container"):
743
  with gr.Column(scale=2):
744
+ #files = gr.Files(label="PDFs", file_types=["pdf"], )
745
+ files = gr.Files(label="PDFs (.pdf), markdown (.md, .mmd), or text files (.txt)", file_types=[".pdf", ".PDF", ".md", ".mmd", ".txt"], )
746
 
747
  openai_api_key = gr.Textbox(
748
  label="OpenAI API Key",