File size: 5,242 Bytes
ca200bf
 
c4b1cb5
ca200bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4b1cb5
ca200bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""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"])