File size: 2,490 Bytes
f81b130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
from PyPDF2 import PdfReader
from transformers import pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
import torch
import soundfile as sf
from IPython.display import Audio
from datasets import load_dataset
import gradio as gr
import os, re
import shutil

first_model = pipeline(task='summarization',model='pszemraj/long-t5-tglobal-base-16384-book-summary')
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")

def readAbstract(pdf):
  # Extract text from PDF
  reader = PdfReader(pdf)
  # Extract each page to variable.
  abstract = reader.pages[0]
  abstract = abstract.extract_text()
  # Removing all before 'abstract' for cleaning
  abstract = abstract[abstract.find('Abstract'):]
  abstract = abstract.split('Introduction', 1)[0]
  return abstract

title = 'PDF Abstracter'
description = 'The model takes a PDF with an abstract as input and summarises it in one sentence that can be read and listened to. Please note that only PDFs with an abstract will work, otherwise there will be an error'
def abstract_summary(file):
    # Set file path for uploaded file
    file_path = "/content/" + os.path.basename(file)
    shutil.copyfile(file.name, file_path)
    # Extract Abstract from PDF
    pdf = readAbstract(file_path)
    # Run Summarisation Model
    abstract = first_model(pdf)

    # Text cleaning
    abstract = str(abstract)
    abstract = abstract.replace("[","").replace("]","").replace("{","").replace("}","").replace("'","").replace("summary_text: ","")

    inputs = processor(text=str(abstract), return_tensors="pt")
    embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
    speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
    spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
    vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")

    with torch.no_grad():
      speech = vocoder(spectrogram)
    speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
    audio = Audio(speech, rate=16000)
    with open('/content/abstract.wav', 'wb') as f:
        f.write(audio.data)
    audio = os.path.join('/content/abstract.wav')
    return abstract, audio

gui = gr.Interface(fn=abstract_summary,inputs=["file",],outputs=["text","audio"],title=title,description=description)
gui.launch(debug=True)

gui.close()