Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,994 Bytes
96dc011 ef55fce 96dc011 d794e1d 96dc011 d794e1d 96dc011 d794e1d 96dc011 |
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
import os
import gradio as gr
import numpy as np
import spaces
import torch
import torchaudio
from generator import Segment, load_csm_1b
from huggingface_hub import hf_hub_download, login
from watermarking import watermark
api_key = os.getenv("HF_TOKEN")
gpu_timeout = int(os.getenv("GPU_TIMEOUT", 60))
CSM_1B_HF_WATERMARK = list(map(int, os.getenv("WATERMARK_KEY").split(" ")))
login(token=api_key)
SPACE_INTRO_TEXT = """\
# Sesame CSM 1B
Generate from CSM 1B (Conversational Speech Model).
Code is available on GitHub: [SesameAILabs/csm](https://github.com/SesameAILabs/csm).
Checkpoint is [hosted on HuggingFace](https://huggingface.co/sesame/csm-1b).
Try out our interactive demo [sesame.com/voicedemo](https://www.sesame.com/voicedemo),
this uses a fine-tuned variant of CSM.
The model has some capacity for non-English languages due to data contamination in the training
data, but it is likely not to perform well.
---
"""
CONVO_INTRO_TEXT = """\
## Conversation content
Each line is an utterance in the conversation to generate. Speakers alternate between A and B, starting with speaker A.
"""
DEFAULT_CONVERSATION = """\
Hey how are you doing.
Pretty good, pretty good.
I'm great, so happy to be speaking to you.
Me too, this is some cool stuff huh?
Yeah, I've been reading more about speech generation, and it really seems like context is important.
Definitely.
"""
SPEAKER_PROMPTS = {
"conversational_a": {
"text": (
"like revising for an exam I'd have to try and like keep up the momentum because I'd "
"start really early I'd be like okay I'm gonna start revising now and then like "
"you're revising for ages and then I just like start losing steam I didn't do that "
"for the exam we had recently to be fair that was a more of a last minute scenario "
"but like yeah I'm trying to like yeah I noticed this yesterday that like Mondays I "
"sort of start the day with this not like a panic but like a"
),
"audio": "prompts/conversational_a.wav",
},
"conversational_b": {
"text": (
"like a super Mario level. Like it's very like high detail. And like, once you get "
"into the park, it just like, everything looks like a computer game and they have all "
"these, like, you know, if, if there's like a, you know, like in a Mario game, they "
"will have like a question block. And if you like, you know, punch it, a coin will "
"come out. So like everyone, when they come into the park, they get like this little "
"bracelet and then you can go punching question blocks around."
),
"audio": "prompts/conversational_b.wav",
},
"read_speech_a": {
"text": (
"And Lake turned round upon me, a little abruptly, his odd yellowish eyes, a little "
"like those of the sea eagle, and the ghost of his smile that flickered on his "
"singularly pale face, with a stern and insidious look, confronted me."
),
"audio": "prompts/read_speech_a.wav",
},
"read_speech_b": {
"text": (
"He was such a big boy that he wore high boots and carried a jack knife. He gazed and "
"gazed at the cap, and could not keep from fingering the blue tassel."
),
"audio": "prompts/read_speech_b.wav",
},
"read_speech_c": {
"text": (
"All passed so quickly, there was so much going on around him, the Tree quite forgot "
"to look to himself."
),
"audio": "prompts/read_speech_c.wav",
},
"read_speech_d": {
"text": (
"Suddenly I was back in the old days Before you felt we ought to drift apart. It was "
"some trick-the way your eyebrows raise."
),
"audio": "prompts/read_speech_d.wav",
},
}
device = "cuda" if torch.cuda.is_available() else "cpu"
model_path = hf_hub_download(repo_id="sesame/csm-1b", filename="ckpt.pt")
generator = load_csm_1b(model_path, device)
@spaces.GPU(duration=gpu_timeout)
def infer(
text_prompt_speaker_a,
text_prompt_speaker_b,
audio_prompt_speaker_a,
audio_prompt_speaker_b,
gen_conversation_input,
) -> tuple[np.ndarray, int]:
# Estimate token limit, otherwise failure might happen after many utterances have been generated.
if len(gen_conversation_input.strip() + text_prompt_speaker_a.strip() + text_prompt_speaker_b.strip()) >= 2000:
raise gr.Error("Prompts and conversation too long.", duration=30)
try:
return _infer(
text_prompt_speaker_a,
text_prompt_speaker_b,
audio_prompt_speaker_a,
audio_prompt_speaker_b,
gen_conversation_input,
)
except ValueError as e:
raise gr.Error(f"Error generating audio: {e}", duration=120)
def _infer(
text_prompt_speaker_a,
text_prompt_speaker_b,
audio_prompt_speaker_a,
audio_prompt_speaker_b,
gen_conversation_input,
) -> tuple[np.ndarray, int]:
audio_prompt_a = prepare_prompt(text_prompt_speaker_a, 0, audio_prompt_speaker_a)
audio_prompt_b = prepare_prompt(text_prompt_speaker_b, 1, audio_prompt_speaker_b)
prompt_segments: list[Segment] = [audio_prompt_a, audio_prompt_b]
generated_segments: list[Segment] = []
conversation_lines = [line.strip() for line in gen_conversation_input.strip().split("\n") if line.strip()]
for i, line in enumerate(conversation_lines):
# Alternating speakers A and B, starting with A
speaker_id = i % 2
audio_tensor = generator.generate(
text=line,
speaker=speaker_id,
context=prompt_segments + generated_segments,
max_audio_length_ms=30_000,
)
generated_segments.append(Segment(text=line, speaker=speaker_id, audio=audio_tensor))
# Concatenate all generations and convert to 16-bit int format
audio_tensors = [segment.audio for segment in generated_segments]
audio_tensor = torch.cat(audio_tensors, dim=0)
# This applies an imperceptible watermark to identify audio as AI-generated.
# Watermarking ensures transparency, dissuades misuse, and enables traceability.
# Please be a responsible AI citizen and keep the watermarking in place.
# If using CSM 1B in another application, use your own private key and keep it secret.
audio_tensor, wm_sample_rate = watermark(
generator._watermarker, audio_tensor, generator.sample_rate, CSM_1B_HF_WATERMARK
)
audio_tensor = torchaudio.functional.resample(
audio_tensor, orig_freq=wm_sample_rate, new_freq=generator.sample_rate
)
audio_array = (audio_tensor * 32768).to(torch.int16).cpu().numpy()
return generator.sample_rate, audio_array
def prepare_prompt(text: str, speaker: int, audio_path: str) -> Segment:
audio_tensor, _ = load_prompt_audio(audio_path)
return Segment(text=text, speaker=speaker, audio=audio_tensor)
def load_prompt_audio(audio_path: str) -> torch.Tensor:
audio_tensor, sample_rate = torchaudio.load(audio_path)
audio_tensor = audio_tensor.squeeze(0)
if sample_rate != generator.sample_rate:
audio_tensor = torchaudio.functional.resample(
audio_tensor, orig_freq=sample_rate, new_freq=generator.sample_rate
)
return audio_tensor, generator.sample_rate
def create_speaker_prompt_ui(speaker_name: str):
speaker_dropdown = gr.Dropdown(
choices=list(SPEAKER_PROMPTS.keys()), label="Select a predefined speaker", value=speaker_name
)
with gr.Accordion("Or add your own voice prompt", open=False):
text_prompt_speaker = gr.Textbox(label="Speaker prompt", lines=4, value=SPEAKER_PROMPTS[speaker_name]["text"])
audio_prompt_speaker = gr.Audio(
label="Speaker prompt", type="filepath", value=SPEAKER_PROMPTS[speaker_name]["audio"]
)
return speaker_dropdown, text_prompt_speaker, audio_prompt_speaker
with gr.Blocks() as app:
gr.Markdown(SPACE_INTRO_TEXT)
gr.Markdown("## Voices")
with gr.Row():
with gr.Column():
gr.Markdown("### Speaker A")
speaker_a_dropdown, text_prompt_speaker_a, audio_prompt_speaker_a = create_speaker_prompt_ui(
"conversational_a"
)
with gr.Column():
gr.Markdown("### Speaker B")
speaker_b_dropdown, text_prompt_speaker_b, audio_prompt_speaker_b = create_speaker_prompt_ui(
"conversational_b"
)
def update_audio(speaker):
if speaker in SPEAKER_PROMPTS:
return SPEAKER_PROMPTS[speaker]["audio"]
return None
def update_text(speaker):
if speaker in SPEAKER_PROMPTS:
return SPEAKER_PROMPTS[speaker]["text"]
return None
speaker_a_dropdown.change(fn=update_audio, inputs=[speaker_a_dropdown], outputs=[audio_prompt_speaker_a])
speaker_b_dropdown.change(fn=update_audio, inputs=[speaker_b_dropdown], outputs=[audio_prompt_speaker_b])
speaker_a_dropdown.change(fn=update_text, inputs=[speaker_a_dropdown], outputs=[text_prompt_speaker_a])
speaker_b_dropdown.change(fn=update_text, inputs=[speaker_b_dropdown], outputs=[text_prompt_speaker_b])
gr.Markdown(CONVO_INTRO_TEXT)
gen_conversation_input = gr.TextArea(label="conversation", lines=20, value=DEFAULT_CONVERSATION)
generate_btn = gr.Button("Generate conversation", variant="primary")
gr.Markdown("GPU time limited to 3 minutes, for longer usage duplicate the space.")
audio_output = gr.Audio(label="Synthesized audio")
generate_btn.click(
infer,
inputs=[
text_prompt_speaker_a,
text_prompt_speaker_b,
audio_prompt_speaker_a,
audio_prompt_speaker_b,
gen_conversation_input,
],
outputs=[audio_output],
)
app.launch(ssr_mode=True)
|