Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import VitsModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import scipy.io.wavfile
|
| 5 |
+
import numpy as np
|
| 6 |
+
import librosa
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
# Load the model and tokenizer
|
| 11 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-eng")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def pitch_shift_np(audio_np, sampling_rate, pitch_shift):
|
| 16 |
+
# Correcting the function call
|
| 17 |
+
return librosa.effects.pitch_shift(audio_np, sr=sampling_rate, n_steps=pitch_shift)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def synthesize_speech(text, pitch_shift):
|
| 21 |
+
# Tokenize the input text
|
| 22 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 23 |
+
|
| 24 |
+
# Generate waveform
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
output = model(**inputs).waveform.squeeze().numpy()
|
| 27 |
+
|
| 28 |
+
# Pitch shift
|
| 29 |
+
shifted_audio = pitch_shift_np(output, model.config.sampling_rate, pitch_shift)
|
| 30 |
+
|
| 31 |
+
# Save to a temporary file
|
| 32 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
| 33 |
+
sf.write(fp.name, shifted_audio, model.config.sampling_rate)
|
| 34 |
+
temp_file_path = fp.name
|
| 35 |
+
|
| 36 |
+
return temp_file_path
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Create the Gradio interface
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=synthesize_speech,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.components.Textbox(lines=2, placeholder="Type your text here..."),
|
| 44 |
+
gr.components.Slider(minimum=-2, maximum=2, step=0.1, label="Pitch Shift (Semitones)")
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.components.Audio(type="filepath", label="Generated Speech"),
|
| 47 |
+
title="Text to Speech Synthesis",
|
| 48 |
+
description="Type text and convert it to speech using a TTS model. Use the slider to adjust the pitch."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the application
|
| 52 |
+
interface.launch()
|