Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from PyPDF2 import PdfReader
|
| 2 |
from transformers import pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 3 |
import torch
|
|
@@ -8,10 +9,12 @@ import gradio as gr
|
|
| 8 |
import os, re
|
| 9 |
import shutil
|
| 10 |
|
|
|
|
| 11 |
import os
|
| 12 |
path = '/tmp/gradio/tmp1biredw9'
|
| 13 |
os.makedirs(path, exist_ok=True)
|
| 14 |
|
|
|
|
| 15 |
first_model = pipeline(task='summarization',model='pszemraj/long-t5-tglobal-base-16384-book-summary')
|
| 16 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 17 |
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
|
@@ -19,10 +22,10 @@ model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
|
| 19 |
def readAbstract(pdf):
|
| 20 |
# Extract text from PDF
|
| 21 |
reader = PdfReader(pdf)
|
| 22 |
-
# Extract
|
| 23 |
abstract = reader.pages[0]
|
| 24 |
abstract = abstract.extract_text()
|
| 25 |
-
# Removing all before '
|
| 26 |
abstract = abstract[abstract.find('Abstract'):]
|
| 27 |
abstract = abstract.split('Introduction', 1)[0]
|
| 28 |
return abstract
|
|
@@ -42,12 +45,14 @@ def abstract_summary(file):
|
|
| 42 |
abstract = str(abstract)
|
| 43 |
abstract = abstract.replace("[","").replace("]","").replace("{","").replace("}","").replace("'","").replace("summary_text: ","")
|
| 44 |
|
|
|
|
| 45 |
inputs = processor(text=str(abstract), return_tensors="pt")
|
| 46 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 47 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
| 48 |
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
| 49 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 50 |
|
|
|
|
| 51 |
with torch.no_grad():
|
| 52 |
speech = vocoder(spectrogram)
|
| 53 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
|
|
|
| 1 |
+
# Import Dependencies
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
from transformers import pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 4 |
import torch
|
|
|
|
| 9 |
import os, re
|
| 10 |
import shutil
|
| 11 |
|
| 12 |
+
# Gradio needs a tmp directory for file store, creating manually
|
| 13 |
import os
|
| 14 |
path = '/tmp/gradio/tmp1biredw9'
|
| 15 |
os.makedirs(path, exist_ok=True)
|
| 16 |
|
| 17 |
+
# Loading HuggingFace models
|
| 18 |
first_model = pipeline(task='summarization',model='pszemraj/long-t5-tglobal-base-16384-book-summary')
|
| 19 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 20 |
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
|
|
|
| 22 |
def readAbstract(pdf):
|
| 23 |
# Extract text from PDF
|
| 24 |
reader = PdfReader(pdf)
|
| 25 |
+
# Extract needed page to variable.
|
| 26 |
abstract = reader.pages[0]
|
| 27 |
abstract = abstract.extract_text()
|
| 28 |
+
# Removing all before 'Abstract' for cleaning
|
| 29 |
abstract = abstract[abstract.find('Abstract'):]
|
| 30 |
abstract = abstract.split('Introduction', 1)[0]
|
| 31 |
return abstract
|
|
|
|
| 45 |
abstract = str(abstract)
|
| 46 |
abstract = abstract.replace("[","").replace("]","").replace("{","").replace("}","").replace("'","").replace("summary_text: ","")
|
| 47 |
|
| 48 |
+
# Text to Speech model
|
| 49 |
inputs = processor(text=str(abstract), return_tensors="pt")
|
| 50 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 51 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
| 52 |
spectrogram = model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
| 53 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 54 |
|
| 55 |
+
# Create .wav audio file from above
|
| 56 |
with torch.no_grad():
|
| 57 |
speech = vocoder(spectrogram)
|
| 58 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|