CoolFace
Apppublic

Elrmnd/Vocal-PDF-Summarizer

sourceHugging Faceupdated 3y agoView on Hugging Face
2likes
app.py105 linesDownload Raw Back to root
1# https://elrmnd-vocal-pdf-summarizer.hf.space2 3# Import libraries4 5import gradio as gr6import PyPDF27from transformers import AutoTokenizer, AutoModelForSeq2SeqLM8from gtts import gTTS9from io import BytesIO10 11# Function to extract text from PDF12# Defines a function to extract raw text from a PDF file13def extract_text(pdf_file):14    pdfReader = PyPDF2.PdfReader(pdf_file)15    pageObj = pdfReader.pages[0]16    return pageObj.extract_text()17 18# Function to summarize text19# Defines a function to summarize the extracted text using facebook/bart-large-cnn20def summarize_text(text):21    sentences = text.split(". ")22    start = -1  # Default value if "Abstract" is not found23    end = -124 25    for i, sentence in enumerate(sentences):26        if "Abstract" in sentence:27            start = i + 128            end = start + 629            break30 31    if start != -1:32        abstract = ". ".join(sentences[start:end + 1])33 34        # Load BART model & tokenizer35        tokenizer = AutoTokenizer.from_pretrained("pszemraj/led-base-book-summary")36        model = AutoModelForSeq2SeqLM.from_pretrained("pszemraj/led-base-book-summary")37 38        # Tokenize abstract39        inputs = tokenizer(abstract,40                           max_length=1024,41                           return_tensors="pt",42                           truncation=True)43 44        # Generate summary45        summary_ids = model.generate(inputs['input_ids'],46                                     max_length=50,47                                     min_length=30,48                                     no_repeat_ngram_size=3,49                                     encoder_no_repeat_ngram_size=3,50                                     repetition_penalty=3.5,51                                     num_beams=4,52                                     do_sample=True,53                                     early_stopping=False)54 55        summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)56 57        if '.' in summary:58            index = summary.rindex('.')59            if index != -1:60                summary = summary[:index + 1]61    else:62        summary = "Abstract not found in the document."63 64    return summary65 66# Function to convert text to audio67# Defines a function to convert text to an audio file using Google Text-to-Speech68def text_to_audio(text):69    tts = gTTS(text, lang='en')70    buffer = BytesIO()71    tts.write_to_fp(buffer)72    buffer.seek(0)73    return buffer.read()74 75### Main function76### The main function that ties everything together:77### extracts text, summarizes, and converts to audio.78def audio_pdf(pdf_file):79    text = extract_text(pdf_file)80    summary = summarize_text(text)81    audio = text_to_audio(summary)82    return summary, audio83 84# Define Gradio interface85# Gradio web interface with a file input, text output to display the summary86# and audio output to play the audio file. # Launches the interface87inputs = gr.File()88summary_text = gr.Text()89audio_summary = gr.Audio()90 91iface = gr.Interface(92    fn=audio_pdf,93    inputs=inputs,94    outputs=[summary_text, audio_summary],95    title="The Vocal PDF Summarizer",96    description="I will summarize PDFs that have an abstract and transform them into audio. If an abstract is not present in the document, a message will be displayed.",97    examples=["Article 11 Hidden Technical Debt in Machine Learning Systems.pdf",98              "Article 6 BloombergGPT_ A Large Language Model for Finance.pdf",99              "Article 5 A Comprehensive Survey on Applications of Transformers for Deep Learning Tasks.pdf",100              "Article 8 Llama 2_ Open Foundation and Fine-Tuned Chat Models.pdf"101             ]102)103 104iface.launch()  # Launch the interface105