mhenrhcsen commited on
Commit
1e95696
·
1 Parent(s): 43bf12c
Files changed (3) hide show
  1. README.md +6 -5
  2. app.py +239 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: Tts
3
- emoji: 🔥
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.29.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Orpheus TTS
3
+ emoji: 🚀
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.22.0
8
  app_file: app.py
9
  pinned: false
10
+ short_description: Try Orpheus TTS here
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ from snac import SNAC
3
+ import torch
4
+ import gradio as gr
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+ from huggingface_hub import snapshot_download
7
+ from dotenv import load_dotenv
8
+ load_dotenv()
9
+
10
+ # Check if CUDA is available
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+
13
+ print("Loading SNAC model...")
14
+ snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_24khz")
15
+ snac_model = snac_model.to(device)
16
+
17
+ model_name = "syvai/tts-v1-finetuned"
18
+
19
+ # Download only model config and safetensors
20
+ snapshot_download(
21
+ repo_id=model_name,
22
+ allow_patterns=[
23
+ "config.json",
24
+ "*.safetensors",
25
+ "model.safetensors.index.json",
26
+ ],
27
+ ignore_patterns=[
28
+ "optimizer.pt",
29
+ "pytorch_model.bin",
30
+ "training_args.bin",
31
+ "scheduler.pt",
32
+ "tokenizer.json",
33
+ "tokenizer_config.json",
34
+ "special_tokens_map.json",
35
+ "vocab.json",
36
+ "merges.txt",
37
+ "tokenizer.*"
38
+ ]
39
+ )
40
+
41
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
42
+ model.to(device)
43
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
44
+ print(f"Orpheus model loaded to {device}")
45
+
46
+ # Process text prompt
47
+ def process_prompt(prompt, voice, tokenizer, device):
48
+ prompt = f"{voice}: {prompt}"
49
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
50
+
51
+ start_token = torch.tensor([[128259]], dtype=torch.int64) # Start of human
52
+ end_tokens = torch.tensor([[128009, 128260]], dtype=torch.int64) # End of text, End of human
53
+
54
+ modified_input_ids = torch.cat([start_token, input_ids, end_tokens], dim=1) # SOH SOT Text EOT EOH
55
+
56
+ # No padding needed for single input
57
+ attention_mask = torch.ones_like(modified_input_ids)
58
+
59
+ return modified_input_ids.to(device), attention_mask.to(device)
60
+
61
+ # Parse output tokens to audio
62
+ def parse_output(generated_ids):
63
+ token_to_find = 128257
64
+ token_to_remove = 128258
65
+
66
+ token_indices = (generated_ids == token_to_find).nonzero(as_tuple=True)
67
+
68
+ if len(token_indices[1]) > 0:
69
+ last_occurrence_idx = token_indices[1][-1].item()
70
+ cropped_tensor = generated_ids[:, last_occurrence_idx+1:]
71
+ else:
72
+ cropped_tensor = generated_ids
73
+
74
+ processed_rows = []
75
+ for row in cropped_tensor:
76
+ masked_row = row[row != token_to_remove]
77
+ processed_rows.append(masked_row)
78
+
79
+ code_lists = []
80
+ for row in processed_rows:
81
+ row_length = row.size(0)
82
+ new_length = (row_length // 7) * 7
83
+ trimmed_row = row[:new_length]
84
+ trimmed_row = [t - 128266 for t in trimmed_row]
85
+ code_lists.append(trimmed_row)
86
+
87
+ return code_lists[0] # Return just the first one for single sample
88
+
89
+ # Redistribute codes for audio generation
90
+ def redistribute_codes(code_list, snac_model):
91
+ device = next(snac_model.parameters()).device # Get the device of SNAC model
92
+
93
+ layer_1 = []
94
+ layer_2 = []
95
+ layer_3 = []
96
+ for i in range((len(code_list)+1)//7):
97
+ layer_1.append(code_list[7*i])
98
+ layer_2.append(code_list[7*i+1]-4096)
99
+ layer_3.append(code_list[7*i+2]-(2*4096))
100
+ layer_3.append(code_list[7*i+3]-(3*4096))
101
+ layer_2.append(code_list[7*i+4]-(4*4096))
102
+ layer_3.append(code_list[7*i+5]-(5*4096))
103
+ layer_3.append(code_list[7*i+6]-(6*4096))
104
+
105
+ # Move tensors to the same device as the SNAC model
106
+ codes = [
107
+ torch.tensor(layer_1, device=device).unsqueeze(0),
108
+ torch.tensor(layer_2, device=device).unsqueeze(0),
109
+ torch.tensor(layer_3, device=device).unsqueeze(0)
110
+ ]
111
+
112
+ audio_hat = snac_model.decode(codes)
113
+ return audio_hat.detach().squeeze().cpu().numpy() # Always return CPU numpy array
114
+
115
+ # Main generation function
116
+ @spaces.GPU()
117
+ def generate_speech(text, voice, temperature, top_p, repetition_penalty, max_new_tokens, progress=gr.Progress()):
118
+ if not text.strip():
119
+ return None
120
+
121
+ try:
122
+ progress(0.1, "Processing text...")
123
+ input_ids, attention_mask = process_prompt(text, voice, tokenizer, device)
124
+
125
+ progress(0.3, "Generating speech tokens...")
126
+ with torch.no_grad():
127
+ generated_ids = model.generate(
128
+ input_ids=input_ids,
129
+ attention_mask=attention_mask,
130
+ max_new_tokens=max_new_tokens,
131
+ do_sample=True,
132
+ temperature=temperature,
133
+ top_p=top_p,
134
+ repetition_penalty=repetition_penalty,
135
+ num_return_sequences=1,
136
+ eos_token_id=128258,
137
+ )
138
+
139
+ progress(0.6, "Processing speech tokens...")
140
+ code_list = parse_output(generated_ids)
141
+
142
+ progress(0.8, "Converting to audio...")
143
+ audio_samples = redistribute_codes(code_list, snac_model)
144
+
145
+ return (24000, audio_samples) # Return sample rate and audio
146
+ except Exception as e:
147
+ print(f"Error generating speech: {e}")
148
+ return None
149
+
150
+ # Examples for the UI
151
+ examples = [
152
+ ["Hej mit navn er Mads. Jeg håber du har en god dag.", "mic", 0.6, 0.95, 1.1, 1200],
153
+ ["Hej mit navn er Sofie. Jeg håber du har en god dag.", "nic", 0.6, 0.95, 1.1, 1200],
154
+ ]
155
+
156
+ # Available voices
157
+ VOICES = ["nic", "mic"]
158
+
159
+ # Available Emotive Tags
160
+ EMOTIVE_TAGS = []
161
+
162
+ # Create Gradio interface
163
+ with gr.Blocks(title="Orpheus Text-to-Speech") as demo:
164
+ gr.Markdown(f"""
165
+ # 🎵 [Orpheus Text-to-Speech](https://github.com/canopyai/Orpheus-TTS)
166
+ Enter your text below and hear it converted to natural-sounding speech with the Orpheus TTS model.
167
+
168
+ ## Tips for better prompts:
169
+ - Add paralinguistic elements like {", ".join(EMOTIVE_TAGS)} or `uhm` for more human-like speech.
170
+ - Longer text prompts generally work better than very short phrases
171
+ - Increasing `repetition_penalty` and `temperature` makes the model speak faster.
172
+ """)
173
+ with gr.Row():
174
+ with gr.Column(scale=3):
175
+ text_input = gr.Textbox(
176
+ label="Text to speak",
177
+ placeholder="Enter your text here...",
178
+ lines=5
179
+ )
180
+ voice = gr.Dropdown(
181
+ choices=VOICES,
182
+ value="tara",
183
+ label="Voice"
184
+ )
185
+
186
+ with gr.Accordion("Advanced Settings", open=False):
187
+ temperature = gr.Slider(
188
+ minimum=0.1, maximum=1.5, value=0.6, step=0.05,
189
+ label="Temperature",
190
+ info="Higher values (0.7-1.0) create more expressive but less stable speech"
191
+ )
192
+ top_p = gr.Slider(
193
+ minimum=0.1, maximum=1.0, value=0.95, step=0.05,
194
+ label="Top P",
195
+ info="Nucleus sampling threshold"
196
+ )
197
+ repetition_penalty = gr.Slider(
198
+ minimum=1.0, maximum=2.0, value=1.1, step=0.05,
199
+ label="Repetition Penalty",
200
+ info="Higher values discourage repetitive patterns"
201
+ )
202
+ max_new_tokens = gr.Slider(
203
+ minimum=100, maximum=2000, value=1200, step=100,
204
+ label="Max Length",
205
+ info="Maximum length of generated audio (in tokens)"
206
+ )
207
+
208
+ with gr.Row():
209
+ submit_btn = gr.Button("Generate Speech", variant="primary")
210
+ clear_btn = gr.Button("Clear")
211
+
212
+ with gr.Column(scale=2):
213
+ audio_output = gr.Audio(label="Generated Speech", type="numpy")
214
+
215
+ # Set up examples
216
+ gr.Examples(
217
+ examples=examples,
218
+ inputs=[text_input, voice, temperature, top_p, repetition_penalty, max_new_tokens],
219
+ outputs=audio_output,
220
+ fn=generate_speech,
221
+ cache_examples=True,
222
+ )
223
+
224
+ # Set up event handlers
225
+ submit_btn.click(
226
+ fn=generate_speech,
227
+ inputs=[text_input, voice, temperature, top_p, repetition_penalty, max_new_tokens],
228
+ outputs=audio_output
229
+ )
230
+
231
+ clear_btn.click(
232
+ fn=lambda: (None, None),
233
+ inputs=[],
234
+ outputs=[text_input, audio_output]
235
+ )
236
+
237
+ # Launch the app
238
+ if __name__ == "__main__":
239
+ demo.queue().launch(share=False, ssr_mode=False)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ snac
2
+ python-dotenv
3
+ transformers
4
+ torch
5
+ spaces