tortoise / app.py
cdminix's picture
Update tortoise space
c4b1cb5 verified
"""TorToise - HuggingFace Space Demo
Generated at 2026-01-29T20:46:31Z from templates/space/app.py.j2.
A Gradio interface for TorToise text-to-speech synthesis.
"""
import os
from pathlib import Path
import gradio as gr
import numpy as np
from ttsdb_tortoise import TorToise
# Initialize model (downloads weights on first run)
MODEL_ID = os.environ.get("MODEL_ID", "ttsds/tortoise")
model = TorToise(model_id=MODEL_ID)
def synthesize(
text: str,
reference_audio: str,
reference_text: str,
language: str,
) -> tuple[int, np.ndarray]:
"""Synthesize speech from text.
Expects `reference_audio` to be a filepath (Gradio `type="filepath"`).
Returns (sample_rate, audio_array) as expected by Gradio.
"""
if not text or not text.strip():
raise gr.Error("Please enter some text to synthesize.")
if not reference_audio or not os.path.exists(reference_audio):
raise gr.Error("Please upload a reference audio file.")
if not reference_text or not reference_text.strip():
raise gr.Error("Please enter the transcript of the reference audio.")
audio, sr = model.synthesize(
text=text,
reference_audio=reference_audio,
text_reference=reference_text,
language=language,
)
return (sr, audio)
gr.set_static_paths(paths=["examples"])
# Build the Gradio interface
with gr.Blocks(title="TorToise TTS") as demo:
# Header
gr.Markdown(
"""
# TorToise Text-to-Speech
Tortoise TTS voice cloning model.
> **Note:** This demo is not affiliated with or endorsed by the original authors.
> It is provided for research and educational purposes only.
**Links:** [Code](https://github.com/neonbjb/tortoise-tts.git) | [Paper](https://arxiv.org/abs/2305.07243) | [Weights](https://huggingface.co/jbetker/tortoise-tts-v2)
"""
)
with gr.Row():
with gr.Column():
reference_audio = gr.Audio(
label="Reference Audio",
type="filepath",
)
reference_text = gr.Textbox(
label="Reference Transcript",
placeholder="Enter what is being said in the reference audio...",
lines=2,
)
text_input = gr.Textbox(
label="Text to Synthesize",
placeholder="Enter the text you want to convert to speech...",
lines=3,
)
language = gr.Dropdown(
label="Language",
choices=[
("English", "eng"),
],
value="eng",
)
submit_btn = gr.Button("Synthesize", variant="primary")
with gr.Column():
output_audio = gr.Audio(
label="Synthesized Audio",
type="numpy",
)
# Example inputs from test_data.yaml
# Assume packaged example audios live under the repository `examples/` folder.
_runtime_examples = []
_rel = Path("examples/ref_eng.mp3")
_src = Path(__file__).parent / _rel
if _src.exists():
_runtime_examples.append([_src, "Were the leaders in this luckless change, though our own Baskerville, who was at work some years before them, went much on the same lines.", "With tenure, Suzie'd have all the more leisure for yachting, but her publications are no good.", "eng"])
_rel = Path("examples/ref_zho.wav")
_src = Path(__file__).parent / _rel
if _src.exists():
_runtime_examples.append([_src, "對,這就是我,萬人敬仰的太乙真人。雖然有點嬰兒肥,但也掩不住我,逼人的帥氣。", "視野無限廣,窗外有藍天", "zho"])
gr.Examples(
examples=_runtime_examples,
inputs=[reference_audio, reference_text, text_input, language],
)
submit_btn.click(
fn=synthesize,
inputs=[text_input, reference_audio, reference_text, language],
outputs=[output_audio],
)
# Footer with model information and citations
gr.Markdown(
"""
## Model Information
| Property | Value |
|----------|-------|
| **Architecture** | Autoregressive, Diffusion, Language Modeling |
| **Sample Rate** | 24000 Hz |
| **Parameters** | 960M |
| **Languages** | English |
| **Release Date** | 2022-05-17 |
## Citations
If you use this model, please cite the original work:
```bibtex
@misc{betker2023betterspeechsynthesisscaling,
title={Better speech synthesis through scaling},
author={James Betker},
year={2023},
eprint={2305.07243},
archivePrefix={arXiv},
primaryClass={cs.SD},
url={https://arxiv.org/abs/2305.07243},
}
```
"""
)
if __name__ == "__main__":
# HuggingFace Spaces exposes the service via $PORT and requires binding to 0.0.0.0
port = int(os.environ.get("PORT", "7860"))
demo.launch(server_name="0.0.0.0", server_port=port, share=False, allowed_paths=["examples"])