MusIre commited on
Commit
707243e
1 Parent(s): 566683b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -18
app.py CHANGED
@@ -1,25 +1,43 @@
1
  import subprocess
2
 
3
- subprocess.run(["pip", "install", "PyPDF2", "transformers", "bark", "gradio"])
4
 
5
  import PyPDF2
6
  from transformers import pipeline
7
  from bark import SAMPLE_RATE, generate_audio, preload_models
8
  import gradio as gr
 
 
 
 
 
 
 
 
 
9
 
10
- def summarize_and_convert_to_audio(pdf_file, abstract_page):
11
- # Save the uploaded file
12
- pdf_path = "/tmp/uploaded_file.pdf"
13
- pdf_file.save(pdf_path)
 
 
 
14
 
15
- # Convert abstract_page to integer
16
- abstract_page = int(abstract_page)
17
 
18
- with open(pdf_path, 'rb') as file:
19
- pdf_reader = PyPDF2.PdfReader(file)
20
-
 
 
 
 
 
 
 
 
21
  # Get the abstract page text
22
- abstract_page_text = pdf_reader.pages[abstract_page - 1].extract_text()
23
 
24
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
25
  summary = summarizer(abstract_page_text, max_length=20, min_length=20)
@@ -29,16 +47,41 @@ def summarize_and_convert_to_audio(pdf_file, abstract_page):
29
  text = summary[0]['summary_text']
30
  audio_array = generate_audio(text)
31
 
32
- return Audio(audio_array, rate=SAMPLE_RATE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  iface = gr.Interface(
35
- fn=summarize_and_convert_to_audio,
36
  inputs=[
37
- gr.File("application/pdf", label="Upload PDF file"),
38
- "text"
 
 
 
 
 
 
39
  ],
40
- outputs="audio",
41
- live=True
42
- )
43
 
44
  iface.launch()
 
1
  import subprocess
2
 
3
+ subprocess.run(["pip", "install", "PyPDF2", "transformers", "bark", "gradio","soundfile","PyMuPDF","numpy"])
4
 
5
  import PyPDF2
6
  from transformers import pipeline
7
  from bark import SAMPLE_RATE, generate_audio, preload_models
8
  import gradio as gr
9
+ from IPython.display import Audio
10
+ import os
11
+ import io
12
+ import fitz
13
+ import tempfile
14
+ from PyPDF2 import PdfReader
15
+ import numpy as np
16
+ from tempfile import NamedTemporaryFile
17
+ import soundfile as sf
18
 
19
+ def readPDF(pdf_file_path):
20
+ if not pdf_file_path.endswith(".pdf"):
21
+ raise ValueError("Please upload a PDF file.")
22
+
23
+ with open(pdf_file_path, 'rb') as file:
24
+ pdf_reader = file.read()
25
+ return pdf_reader
26
 
 
 
27
 
28
+ def summarize_and_convert_to_audio(pdf_reader, page):
29
+
30
+ temp_file = tempfile.NamedTemporaryFile(delete=False)
31
+ temp_file.write(pdf_reader)
32
+ temp_file_path = temp_file.name
33
+
34
+ # Use PyMuPDF to read the PDF content
35
+ pdf_document = fitz.open(temp_file_path)
36
+
37
+ print(page)
38
+
39
  # Get the abstract page text
40
+ abstract_page_text = pdf_document[int(page) - 1].get_text()
41
 
42
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
43
  summary = summarizer(abstract_page_text, max_length=20, min_length=20)
 
47
  text = summary[0]['summary_text']
48
  audio_array = generate_audio(text)
49
 
50
+ #save temporary file audio to use it in the second step
51
+
52
+ with NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav_file:
53
+ wav_file_path = temp_wav_file.name
54
+ sf.write(wav_file_path, audio_array, SAMPLE_RATE)
55
+ return wav_file_path
56
+
57
+
58
+ def read_and_speech(pdf_file,abstract_page):
59
+ print(pdf_file)
60
+ pdf_file_path= pdf_file.name
61
+ print(pdf_file_path)
62
+ page=abstract_page
63
+ reader=readPDF(pdf_file_path)
64
+ audio=summarize_and_convert_to_audio(reader,page)
65
+ return audio;
66
+
67
+ # Define app name, app description, and examples
68
+ app_name = "From PDF to Speech"
69
+ app_description = "Convert text from a PDF file to audio. Upload a PDF file. We accept only PDF files with abstracts."
70
+
71
 
72
  iface = gr.Interface(
73
+ fn=read_and_speech,
74
  inputs=[
75
+ gr.File(file_types=["pdf"], label="Upload PDF file"),
76
+ gr.Textbox(label="Insert the page where the abstract is located")],
77
+ outputs=gr.Audio(type="filepath"),
78
+ title=app_name,
79
+ description=app_description,
80
+ examples=[
81
+ ["/content/drive/MyDrive/AAI/2312.04027.pdf",1],
82
+ ["/content/drive/MyDrive/AAI/2312.04542.pdf",1],
83
  ],
84
+ allow_flagging="never"
85
+ )
 
86
 
87
  iface.launch()