Spaces:
Running
on
Zero
Running
on
Zero
textbox
Browse files
app.py
CHANGED
@@ -162,27 +162,13 @@ def redistribute_codes(code_list, snac_model):
|
|
162 |
audio_hat = snac_model.decode(codes)
|
163 |
return audio_hat.detach().squeeze().cpu().numpy()
|
164 |
|
165 |
-
# Text validation function
|
166 |
-
def
|
167 |
-
"""Validate and
|
168 |
MAX_LENGTH = 150
|
169 |
if len(text) > MAX_LENGTH:
|
170 |
-
return text[:MAX_LENGTH]
|
171 |
-
return text
|
172 |
-
|
173 |
-
# Text change handler
|
174 |
-
def on_text_change(text):
|
175 |
-
"""Handle text changes and show character count"""
|
176 |
-
MAX_LENGTH = 150
|
177 |
-
current_length = len(text)
|
178 |
-
|
179 |
-
if current_length > MAX_LENGTH:
|
180 |
-
text = text[:MAX_LENGTH]
|
181 |
-
current_length = MAX_LENGTH
|
182 |
-
gr.Warning(f"Text truncated to {MAX_LENGTH} characters")
|
183 |
-
|
184 |
-
# Return the (potentially truncated) text and update info
|
185 |
-
return text, f"Characters: {current_length}/{MAX_LENGTH}"
|
186 |
|
187 |
# Main generation function with rate limiting
|
188 |
@rate_limit
|
@@ -192,14 +178,16 @@ def generate_speech(text, temperature=0.6, top_p=0.95, repetition_penalty=1.1, m
|
|
192 |
gr.Warning("Please enter some text to generate speech.")
|
193 |
return None
|
194 |
|
195 |
-
# Validate text length
|
196 |
-
|
|
|
|
|
197 |
|
198 |
try:
|
199 |
progress(0.1, "Processing text...")
|
200 |
-
print(f"Generating speech for text: {
|
201 |
|
202 |
-
input_ids, attention_mask = process_prompt(
|
203 |
|
204 |
progress(0.3, "Generating speech tokens...")
|
205 |
with torch.no_grad():
|
@@ -255,7 +243,7 @@ examples = [
|
|
255 |
|
256 |
EMOTIVE_TAGS = ["`<laugh>`", "`<chuckle>`", "`<sigh>`", "`<cough>`", "`<sniffle>`", "`<groan>`", "`<yawn>`", "`<gasp>`"]
|
257 |
|
258 |
-
# Create custom CSS
|
259 |
css = """
|
260 |
.gradio-container {
|
261 |
max-width: 1200px;
|
@@ -307,11 +295,12 @@ with gr.Blocks(title="Khmer Text-to-Speech", css=css, theme=gr.themes.Soft()) as
|
|
307 |
placeholder="αααα
αΌαα’ααααααααααααααα’ααααα
ααΈααα... (α’αα·ααααΆ α‘α₯α αα½α’αααα)",
|
308 |
lines=4,
|
309 |
max_lines=6,
|
310 |
-
interactive=True
|
|
|
311 |
)
|
312 |
|
313 |
-
#
|
314 |
-
char_info = gr.
|
315 |
|
316 |
# Advanced Settings
|
317 |
with gr.Accordion("π§ Advanced Settings", open=False):
|
@@ -358,14 +347,32 @@ with gr.Blocks(title="Khmer Text-to-Speech", css=css, theme=gr.themes.Soft()) as
|
|
358 |
label="π Example Texts (α’αααααααααΌ) - Click example then press Generate"
|
359 |
)
|
360 |
|
361 |
-
#
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
|
368 |
-
# Set up event handlers
|
369 |
submit_btn.click(
|
370 |
fn=generate_speech,
|
371 |
inputs=[text_input, temperature, top_p, repetition_penalty, max_new_tokens],
|
@@ -374,9 +381,10 @@ with gr.Blocks(title="Khmer Text-to-Speech", css=css, theme=gr.themes.Soft()) as
|
|
374 |
)
|
375 |
|
376 |
clear_btn.click(
|
377 |
-
fn=lambda: ("",
|
378 |
inputs=[],
|
379 |
-
outputs=[text_input,
|
|
|
380 |
)
|
381 |
|
382 |
# Add keyboard shortcut
|
|
|
162 |
audio_hat = snac_model.decode(codes)
|
163 |
return audio_hat.detach().squeeze().cpu().numpy()
|
164 |
|
165 |
+
# Text validation function - now only validates, doesn't truncate
|
166 |
+
def validate_and_truncate_text(text):
|
167 |
+
"""Validate and truncate text length"""
|
168 |
MAX_LENGTH = 150
|
169 |
if len(text) > MAX_LENGTH:
|
170 |
+
return text[:MAX_LENGTH], True # Return truncated text and truncation flag
|
171 |
+
return text, False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
# Main generation function with rate limiting
|
174 |
@rate_limit
|
|
|
178 |
gr.Warning("Please enter some text to generate speech.")
|
179 |
return None
|
180 |
|
181 |
+
# Validate and truncate text length
|
182 |
+
validated_text, was_truncated = validate_and_truncate_text(text)
|
183 |
+
if was_truncated:
|
184 |
+
gr.Warning(f"Text was truncated to 150 characters for processing.")
|
185 |
|
186 |
try:
|
187 |
progress(0.1, "Processing text...")
|
188 |
+
print(f"Generating speech for text: {validated_text[:50]}...")
|
189 |
|
190 |
+
input_ids, attention_mask = process_prompt(validated_text, voice, tokenizer, device)
|
191 |
|
192 |
progress(0.3, "Generating speech tokens...")
|
193 |
with torch.no_grad():
|
|
|
243 |
|
244 |
EMOTIVE_TAGS = ["`<laugh>`", "`<chuckle>`", "`<sigh>`", "`<cough>`", "`<sniffle>`", "`<groan>`", "`<yawn>`", "`<gasp>`"]
|
245 |
|
246 |
+
# Create custom CSS with character counter using JavaScript
|
247 |
css = """
|
248 |
.gradio-container {
|
249 |
max-width: 1200px;
|
|
|
295 |
placeholder="αααα
αΌαα’ααααααααααααααα’ααααα
ααΈααα... (α’αα·ααααΆ α‘α₯α αα½α’αααα)",
|
296 |
lines=4,
|
297 |
max_lines=6,
|
298 |
+
interactive=True,
|
299 |
+
elem_id="text_input"
|
300 |
)
|
301 |
|
302 |
+
# Static character counter - will be updated by JavaScript
|
303 |
+
char_info = gr.HTML('<div class="char-counter" id="char-counter">Characters: 0/150</div>')
|
304 |
|
305 |
# Advanced Settings
|
306 |
with gr.Accordion("π§ Advanced Settings", open=False):
|
|
|
347 |
label="π Example Texts (α’αααααααααΌ) - Click example then press Generate"
|
348 |
)
|
349 |
|
350 |
+
# Add JavaScript for real-time character counting without server calls
|
351 |
+
demo.load(js="""
|
352 |
+
function() {
|
353 |
+
const textInput = document.querySelector('#text_input textarea');
|
354 |
+
const charCounter = document.querySelector('#char-counter');
|
355 |
+
|
356 |
+
if (textInput && charCounter) {
|
357 |
+
function updateCounter() {
|
358 |
+
const length = textInput.value.length;
|
359 |
+
const maxLength = 150;
|
360 |
+
charCounter.textContent = `Characters: ${length}/${maxLength}`;
|
361 |
+
|
362 |
+
if (length > maxLength) {
|
363 |
+
charCounter.style.color = '#ff6b6b';
|
364 |
+
} else {
|
365 |
+
charCounter.style.color = '#666';
|
366 |
+
}
|
367 |
+
}
|
368 |
+
|
369 |
+
textInput.addEventListener('input', updateCounter);
|
370 |
+
updateCounter(); // Initial count
|
371 |
+
}
|
372 |
+
}
|
373 |
+
""")
|
374 |
|
375 |
+
# Set up event handlers - NO text change event
|
376 |
submit_btn.click(
|
377 |
fn=generate_speech,
|
378 |
inputs=[text_input, temperature, top_p, repetition_penalty, max_new_tokens],
|
|
|
381 |
)
|
382 |
|
383 |
clear_btn.click(
|
384 |
+
fn=lambda: ("", None),
|
385 |
inputs=[],
|
386 |
+
outputs=[text_input, audio_output],
|
387 |
+
js="() => { document.querySelector('#char-counter').textContent = 'Characters: 0/150'; }"
|
388 |
)
|
389 |
|
390 |
# Add keyboard shortcut
|