Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
| 4 |
+
from transformers import AutoTokenizer
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Set device (GPU if available, else CPU)
|
| 10 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
# Load model and tokenizer from Hugging Face Hub
|
| 13 |
+
# These will be downloaded automatically by the Space when it builds
|
| 14 |
+
# The model will be loaded to the GPU if available in the Space's runtime
|
| 15 |
+
model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler-tts-tiny-v1").to(device)
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler-tts-tiny-v1")
|
| 17 |
+
|
| 18 |
+
def predict_tts(text, voice_description):
|
| 19 |
+
if not text:
|
| 20 |
+
return None, "Please enter some text."
|
| 21 |
+
if not voice_description:
|
| 22 |
+
return None, "Please provide a voice description."
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
input_ids = tokenizer(voice_description, return_tensors="pt").input_ids.to(device)
|
| 26 |
+
prompt_input_ids = tokenizer(text, return_tensors="pt").input_ids.to(device)
|
| 27 |
+
|
| 28 |
+
with torch.no_grad(): # Disable gradient calculation for inference to save memory and speed
|
| 29 |
+
generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
| 30 |
+
|
| 31 |
+
audio_arr = generation.cpu().numpy().squeeze()
|
| 32 |
+
sampling_rate = model.config.sampling_rate
|
| 33 |
+
|
| 34 |
+
# Gradio's Audio output component expects a filepath to an audio file
|
| 35 |
+
output_path = "output_audio.wav"
|
| 36 |
+
sf.write(output_path, audio_arr, sampling_rate)
|
| 37 |
+
|
| 38 |
+
return output_path, "Speech generated successfully!"
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return None, f"An error occurred: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Gradio Interface definition for the Space
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=predict_tts,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Textbox(lines=5, label="Text to Convert", placeholder="Enter your text here..."),
|
| 47 |
+
gr.Textbox(lines=3, label="Voice Description", placeholder="e.g., A female speaker with a calm and clear speech, very high quality audio."),
|
| 48 |
+
],
|
| 49 |
+
outputs=[
|
| 50 |
+
gr.Audio(label="Generated Speech", type="filepath"),
|
| 51 |
+
gr.Textbox(label="Status")
|
| 52 |
+
],
|
| 53 |
+
title="Parler-TTS Tiny: Natural Language Guided Text-to-Speech",
|
| 54 |
+
description="Enter text and describe the voice you want (gender, tone, speed, quality) to generate speech using the tiny Parler-TTS model.",
|
| 55 |
+
examples=[
|
| 56 |
+
["Hello, my name is Parler TTS. How can I help you today?", "A friendly female voice speaking clearly."],
|
| 57 |
+
["The quick brown fox jumps over the lazy dog.", "A deep male voice, speaking slowly and thoughtfully."],
|
| 58 |
+
["We're excited to announce our new product!", "An enthusiastic female voice with high pitch."],
|
| 59 |
+
],
|
| 60 |
+
allow_flagging="never" # This prevents users from flagging your outputs for feedback
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# This standard Gradio line tells the Space to launch the interface
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|