Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
import torch
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 12 |
+
speech = pipeline("text-to-speech", model="microsoft/speecht5_tts")
|
| 13 |
+
|
| 14 |
+
# code from the Model card
|
| 15 |
+
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 16 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 17 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 18 |
+
speaker_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 19 |
+
speaker_embeddings = torch.tensor(speaker_dataset[0]["xvector"]).unsqueeze(0)
|
| 20 |
+
|
| 21 |
+
def summarize_text_and_speak(prompt):
|
| 22 |
+
summary = summarizer(prompt, max_length=150, min_length=30, do_sample=False)
|
| 23 |
+
summary_text = summary[0]['summary_text']
|
| 24 |
+
|
| 25 |
+
#inputs = processor(text="Hello, my dog is cute.", return_tensors="pt")
|
| 26 |
+
inputs = processor(text=summary_text, return_tensors="pt")
|
| 27 |
+
#speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
| 28 |
+
speech_audio = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
|
| 29 |
+
|
| 30 |
+
#sf.write("speech.wav", speech.numpy(), samplerate=16000)
|
| 31 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 32 |
+
sf.write(tmp_file.name, speech_audio.numpy(), samplerate=16000)
|
| 33 |
+
audio_path = tmp_file.name
|
| 34 |
+
|
| 35 |
+
return summary_text, audio_path
|
| 36 |
+
|
| 37 |
+
interface = gr.Interface(
|
| 38 |
+
fn=summarize_text_and_speak,
|
| 39 |
+
inputs=gr.Textbox(lines=10, label="Input text"),
|
| 40 |
+
outputs=[gr.Textbox(label="Summary"), gr.Audio(label="Audio")]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
interface.launch(share=True)
|