Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758from PyPDF2 import PdfReaderfrom transformers import pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGanimport torchimport soundfile as sffrom IPython.display import Audiofrom datasets import load_datasetimport gradio as grimport os, reimport shutilfirst_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 PDFreader = PdfReader(pdf)# Extract each page to variable.abstract = reader.pages[0]abstract = abstract.extract_text()# Removing all before 'abstract' for cleaningabstract = abstract[abstract.find('Abstract'):]abstract = abstract.split('Introduction', 1)[0]return abstracttitle = '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 filefile_path = "/content/" + os.path.basename(file)shutil.copyfile(file.name, file_path)# Extract Abstract from PDFpdf = readAbstract(file_path)# Run Summarisation Modelabstract = first_model(pdf)# Text cleaningabstract = 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, audiogui = gr.Interface(fn=abstract_summary,inputs=["file",],outputs=["text","audio"],title=title,description=description)gui.launch(debug=True)1print('Hello World')
app.py+1-58