MHayden commited on
Commit
f81b130
·
1 Parent(s): ec24267

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PyPDF2 import PdfReader
2
+ from transformers import pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
3
+ import torch
4
+ import soundfile as sf
5
+ from IPython.display import Audio
6
+ from datasets import load_dataset
7
+ import gradio as gr
8
+ import os, re
9
+ import shutil
10
+
11
+ first_model = pipeline(task='summarization',model='pszemraj/long-t5-tglobal-base-16384-book-summary')
12
+ processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
13
+ model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
14
+
15
+ def readAbstract(pdf):
16
+ # Extract text from PDF
17
+ reader = PdfReader(pdf)
18
+ # Extract each page to variable.
19
+ abstract = reader.pages[0]
20
+ abstract = abstract.extract_text()
21
+ # Removing all before 'abstract' for cleaning
22
+ abstract = abstract[abstract.find('Abstract'):]
23
+ abstract = abstract.split('Introduction', 1)[0]
24
+ return abstract
25
+
26
+ title = 'PDF Abstracter'
27
+ 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'
28
+ def abstract_summary(file):
29
+ # Set file path for uploaded file
30
+ file_path = "/content/" + os.path.basename(file)
31
+ shutil.copyfile(file.name, file_path)
32
+ # Extract Abstract from PDF
33
+ pdf = readAbstract(file_path)
34
+ # Run Summarisation Model
35
+ abstract = first_model(pdf)
36
+
37
+ # Text cleaning
38
+ abstract = str(abstract)
39
+ abstract = abstract.replace("[","").replace("]","").replace("{","").replace("}","").replace("'","").replace("summary_text: ","")
40
+
41
+ inputs = processor(text=str(abstract), return_tensors="pt")
42
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
43
+ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
44
+ spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
45
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
46
+
47
+ with torch.no_grad():
48
+ speech = vocoder(spectrogram)
49
+ speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
50
+ audio = Audio(speech, rate=16000)
51
+ with open('/content/abstract.wav', 'wb') as f:
52
+ f.write(audio.data)
53
+ audio = os.path.join('/content/abstract.wav')
54
+ return abstract, audio
55
+
56
+ gui = gr.Interface(fn=abstract_summary,inputs=["file",],outputs=["text","audio"],title=title,description=description)
57
+ gui.launch(debug=True)
58
+
59
+ gui.close()