PlotweaverModel commited on
Commit
4a5e9f1
·
verified ·
1 Parent(s): aa5b642

app update

Browse files
Files changed (1) hide show
  1. app.py +38 -1
app.py CHANGED
@@ -124,9 +124,10 @@ def translate_sentence(text, max_length=256, fast=False):
124
  with torch.no_grad():
125
  if fast:
126
  # Greedy decoding - much faster, slightly lower quality
 
127
  output_ids = mt_model.generate(
128
  **inputs,
129
- max_length=max_length,
130
  forced_bos_token_id=tgt_lang_id,
131
  repetition_penalty=1.5,
132
  no_repeat_ngram_size=3,
@@ -433,12 +434,48 @@ with gr.Blocks(
433
  label="Yoruba Output",
434
  type="numpy",
435
  autoplay=True,
 
436
  )
437
  stream_log = gr.Markdown(
438
  label="Live Transcript",
439
  value="Waiting for audio input..."
440
  )
441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  stream_input.stream(
443
  fn=streaming_process,
444
  inputs=[stream_input, stream_state],
 
124
  with torch.no_grad():
125
  if fast:
126
  # Greedy decoding - much faster, slightly lower quality
127
+ # Lower max_length since streaming chunks are short
128
  output_ids = mt_model.generate(
129
  **inputs,
130
+ max_length=128,
131
  forced_bos_token_id=tgt_lang_id,
132
  repetition_penalty=1.5,
133
  no_repeat_ngram_size=3,
 
434
  label="Yoruba Output",
435
  type="numpy",
436
  autoplay=True,
437
+ elem_id="yoruba-stream-output",
438
  )
439
  stream_log = gr.Markdown(
440
  label="Live Transcript",
441
  value="Waiting for audio input..."
442
  )
443
 
444
+ # JS hook: force autoplay whenever the audio src changes.
445
+ # Browsers sometimes block autoplay until user interaction;
446
+ # this MutationObserver forces play() on every src update.
447
+ gr.HTML("""
448
+ <script>
449
+ (function() {
450
+ function attachAutoplayHook() {
451
+ const container = document.getElementById('yoruba-stream-output');
452
+ if (!container) { setTimeout(attachAutoplayHook, 500); return; }
453
+ const audio = container.querySelector('audio');
454
+ if (!audio) { setTimeout(attachAutoplayHook, 500); return; }
455
+
456
+ // Force play whenever the src attribute changes
457
+ const observer = new MutationObserver(() => {
458
+ audio.muted = false;
459
+ audio.play().catch(e => console.log('Autoplay blocked:', e));
460
+ });
461
+ observer.observe(audio, { attributes: true, attributeFilter: ['src'] });
462
+
463
+ // Also play on loadeddata event
464
+ audio.addEventListener('loadeddata', () => {
465
+ audio.play().catch(e => console.log('Play failed:', e));
466
+ });
467
+
468
+ console.log('Yoruba autoplay hook attached');
469
+ }
470
+ if (document.readyState === 'loading') {
471
+ document.addEventListener('DOMContentLoaded', attachAutoplayHook);
472
+ } else {
473
+ attachAutoplayHook();
474
+ }
475
+ })();
476
+ </script>
477
+ """)
478
+
479
  stream_input.stream(
480
  fn=streaming_process,
481
  inputs=[stream_input, stream_state],