mixklim commited on
Commit
9da6199
1 Parent(s): 9d252a2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pathlib import Path
3
+ from pypdf import PdfReader
4
+
5
+ from transformers.utils import logging
6
+
7
+ logging.set_verbosity_error()
8
+
9
+ from transformers import pipeline
10
+ import torch
11
+
12
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
13
+
14
+ summarizer = pipeline(
15
+ task="summarization",
16
+ model="facebook/bart-large-cnn",
17
+ torch_dtype=torch.bfloat16,
18
+ device=device
19
+ )
20
+
21
+ narrator = pipeline("text-to-speech", model="suno/bark-small", device=device)
22
+
23
+
24
+ def upload_file(filepath):
25
+ file_path = Path(filepath).name
26
+ return file_path
27
+
28
+
29
+ def pdf_to_audio(file_output):
30
+ pdf_summary = ""
31
+ pdf_reader = PdfReader(file_output)
32
+ for page in pdf_reader.pages[:1]:
33
+ page_summary = summarizer(page.extract_text(), min_length=10, max_length=10)
34
+ pdf_summary = pdf_summary + page_summary[0]["summary_text"] + " "
35
+ narrated_text = narrator(pdf_summary)
36
+ return gr.Audio((narrated_text["sampling_rate"], narrated_text["audio"].T))
37
+
38
+
39
+ with gr.Blocks() as demo:
40
+ file_output = gr.File()
41
+ upload_button = gr.UploadButton(
42
+ "Click to upload your PDF file", file_types=["file"], file_count="single"
43
+ )
44
+ upload_button.upload(upload_file, upload_button, file_output)
45
+
46
+ audio = gr.Interface(
47
+ fn=pdf_to_audio,
48
+ inputs=file_output,
49
+ outputs="audio",
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ demo.launch(debug=True)