File size: 1,379 Bytes
9da6199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c71c63
d19cb4f
9da6199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
from pathlib import Path
from pypdf import PdfReader

from transformers.utils import logging

logging.set_verbosity_error()

from transformers import pipeline
import torch

device = "cuda:0" if torch.cuda.is_available() else "cpu"

summarizer = pipeline(
    task="summarization",
    model="facebook/bart-large-cnn",
    torch_dtype=torch.bfloat16,
    device=device
)

narrator = pipeline("text-to-speech", model="suno/bark-small", device=device)


def upload_file(filepath):
    file_path = Path(filepath).name
    return file_path


def pdf_to_audio(file_output):
    pdf_summary = ""
    pdf_reader = PdfReader(file_output)
    for page in pdf_reader.pages:
        page_summary = summarizer(page.extract_text(), min_length=10, max_length=100)
        pdf_summary = pdf_summary + page_summary[0]["summary_text"] + " "
    narrated_text = narrator(pdf_summary)
    return gr.Audio((narrated_text["sampling_rate"], narrated_text["audio"].T))


with gr.Blocks() as demo:
    file_output = gr.File()
    upload_button = gr.UploadButton(
        "Click to upload your PDF file", file_types=["file"], file_count="single"
    )
    upload_button.upload(upload_file, upload_button, file_output)

    audio = gr.Interface(
        fn=pdf_to_audio,
        inputs=file_output,
        outputs="audio",
    )

if __name__ == "__main__":
    demo.launch(debug=True)