feeday commited on
Commit
b98b3aa
·
verified ·
1 Parent(s): 89fe5e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -22
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import spaces
3
  # only debug for hf now
4
  REPO_TYPE = "hf"
@@ -376,18 +377,62 @@ def trim_audio(audio_path, start_time, end_time):
376
  raise gr.Error(f"Error trimming audio: {str(e)}")
377
 
378
  def save_transcription(transcription):
379
- """
380
- Saves the transcription text to a temporary file.
381
-
382
- Args:
383
- transcription (str): The transcription text.
384
-
385
- Returns:
386
- str: The path to the transcription file.
387
- """
388
- with tempfile.NamedTemporaryFile(delete=False, suffix='.txt', mode='w', encoding='utf-8') as temp_file:
389
  temp_file.write(transcription)
390
- logging.info(f"Transcription saved to: {temp_file.name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  return temp_file.name
392
 
393
  def get_model_options(pipeline_type):
@@ -445,7 +490,7 @@ def transcribe_audio(audio_input, audio_url, proxy_url, proxy_username, proxy_pa
445
  verbose_messages = f"Starting transcription with parameters:\nPipeline Type: {pipeline_type}\nModel ID: {model_id}\nDownload Method: {download_method}\n"
446
 
447
  if verbose:
448
- yield verbose_messages, "", None
449
 
450
  # Determine the audio source
451
  audio_path = None
@@ -461,16 +506,16 @@ def transcribe_audio(audio_input, audio_url, proxy_url, proxy_username, proxy_pa
461
  if not audio_path:
462
  error_msg = f"Error downloading audio from {audio_url} using method {download_method}. Check logs for details."
463
  logging.error(error_msg)
464
- yield verbose_messages + error_msg, "", None
465
  return
466
  else:
467
  verbose_messages += f"Successfully downloaded audio from {audio_url}\n"
468
  if verbose:
469
- yield verbose_messages, "", None
470
  else:
471
  error_msg = "No audio source provided. Please upload an audio file, record audio, or enter a URL."
472
  logging.error(error_msg)
473
- yield verbose_messages + error_msg, "", None
474
  return
475
 
476
  # Convert start_time and end_time to float or None
@@ -482,7 +527,7 @@ def transcribe_audio(audio_input, audio_url, proxy_url, proxy_username, proxy_pa
482
  is_temp_file = True # The trimmed audio is a temporary file
483
  verbose_messages += f"Audio trimmed from {start_time} to {end_time}\n"
484
  if verbose:
485
- yield verbose_messages, "", None
486
 
487
  # Model caching
488
  model_key = (pipeline_type, model_id)
@@ -514,7 +559,7 @@ def transcribe_audio(audio_input, audio_url, proxy_url, proxy_username, proxy_pa
514
  else:
515
  error_msg = "Invalid pipeline type. Only 'sensevoice' is supported."
516
  logging.error(error_msg)
517
- yield verbose_messages + error_msg, "", None
518
  return
519
  loaded_models[model_key] = model
520
 
@@ -552,22 +597,31 @@ def transcribe_audio(audio_input, audio_url, proxy_url, proxy_username, proxy_pa
552
  transcription_time = end_time_perf - start_time_perf
553
  audio_file_size = os.path.getsize(audio_path) / (1024 * 1024)
554
 
 
 
 
 
 
555
  metrics_output = (
556
  f"Transcription time: {transcription_time:.2f} seconds\n"
557
  f"Audio file size: {audio_file_size:.2f} MB\n"
 
 
558
  )
559
 
560
- # Save the transcription to a file
561
  transcription_file = save_transcription(transcription)
 
562
 
563
  # Always yield the final result, regardless of verbose setting
564
  final_metrics = verbose_messages + metrics_output
565
- yield final_metrics, transcription, transcription_file
566
 
567
  except Exception as e:
568
  error_msg = f"An error occurred during transcription: {str(e)}"
569
  logging.error(error_msg)
570
- yield verbose_messages + error_msg, "", None
 
571
 
572
  finally:
573
  # Clean up temporary audio files
@@ -618,7 +672,8 @@ with gr.Blocks() as iface:
618
  with gr.Row():
619
  metrics_output = gr.Textbox(label="Transcription Metrics and Verbose Messages", lines=10)
620
  transcription_output = gr.Textbox(label="Transcription", lines=10)
621
- transcription_file = gr.File(label="Download Transcription")
 
622
 
623
  def update_model_dropdown(pipeline_type):
624
  """
@@ -652,7 +707,7 @@ with gr.Blocks() as iface:
652
  transcribe_button.click(
653
  transcribe_with_progress,
654
  inputs=[audio_input, audio_url, proxy_url, proxy_username, proxy_password, pipeline_type, model_id, download_method, start_time, end_time, verbose],
655
- outputs=[metrics_output, transcription_output, transcription_file]
656
  )
657
 
658
  # Note: For examples, users should use local audio files or upload their own files
 
1
  import os
2
+ import re
3
  import spaces
4
  # only debug for hf now
5
  REPO_TYPE = "hf"
 
377
  raise gr.Error(f"Error trimming audio: {str(e)}")
378
 
379
  def save_transcription(transcription):
380
+ """Save transcription text to a temporary TXT file."""
381
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as temp_file:
 
 
 
 
 
 
 
 
382
  temp_file.write(transcription)
383
+ logging.info("Transcription saved to: %s", temp_file.name)
384
+ return temp_file.name
385
+
386
+
387
+ def _format_srt_time(seconds):
388
+ """Format seconds as SRT timestamp: HH:MM:SS,mmm."""
389
+ total_ms = max(0, int(round(float(seconds) * 1000)))
390
+ hours, rem = divmod(total_ms, 3_600_000)
391
+ minutes, rem = divmod(rem, 60_000)
392
+ secs, millis = divmod(rem, 1000)
393
+ return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
394
+
395
+
396
+ def _split_subtitle_text(text, max_chars=42):
397
+ """Split transcript into subtitle-sized chunks."""
398
+ text = (text or "").strip()
399
+ if not text:
400
+ return []
401
+ sentences = re.split(r"(?<=[。!?!?;;])\s*|\n+", text)
402
+ chunks = []
403
+ for sentence in sentences:
404
+ sentence = sentence.strip()
405
+ while len(sentence) > max_chars:
406
+ cut = max_chars
407
+ for pos in range(max_chars, max(8, max_chars - 12), -1):
408
+ if sentence[pos - 1:pos] in ",,、 ":
409
+ cut = pos
410
+ break
411
+ chunks.append(sentence[:cut].strip())
412
+ sentence = sentence[cut:].strip()
413
+ if sentence:
414
+ chunks.append(sentence)
415
+ return chunks
416
+
417
+
418
+ def save_srt(transcription, duration_seconds):
419
+ """Save an SRT file. Timings are estimated when the ASR model provides no timestamps."""
420
+ chunks = _split_subtitle_text(transcription) or [""]
421
+ duration = max(float(duration_seconds or 0), 1.0)
422
+ weights = [max(len(chunk), 1) for chunk in chunks]
423
+ total_weight = sum(weights)
424
+ cursor = 0.0
425
+ blocks = []
426
+ for index, (chunk, weight) in enumerate(zip(chunks, weights), start=1):
427
+ start_time = cursor
428
+ end_time = duration if index == len(chunks) else duration * sum(weights[:index]) / total_weight
429
+ if end_time <= start_time:
430
+ end_time = start_time + 0.5
431
+ blocks.append(f"{index}\n{_format_srt_time(start_time)} --> {_format_srt_time(end_time)}\n{chunk}")
432
+ cursor = end_time
433
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".srt", mode="w", encoding="utf-8-sig", newline="\n") as temp_file:
434
+ temp_file.write("\n\n".join(blocks) + "\n")
435
+ logging.info("SRT subtitles saved to: %s", temp_file.name)
436
  return temp_file.name
437
 
438
  def get_model_options(pipeline_type):
 
490
  verbose_messages = f"Starting transcription with parameters:\nPipeline Type: {pipeline_type}\nModel ID: {model_id}\nDownload Method: {download_method}\n"
491
 
492
  if verbose:
493
+ yield verbose_messages, "", None, None
494
 
495
  # Determine the audio source
496
  audio_path = None
 
506
  if not audio_path:
507
  error_msg = f"Error downloading audio from {audio_url} using method {download_method}. Check logs for details."
508
  logging.error(error_msg)
509
+ yield verbose_messages + error_msg, "", None, None
510
  return
511
  else:
512
  verbose_messages += f"Successfully downloaded audio from {audio_url}\n"
513
  if verbose:
514
+ yield verbose_messages, "", None, None
515
  else:
516
  error_msg = "No audio source provided. Please upload an audio file, record audio, or enter a URL."
517
  logging.error(error_msg)
518
+ yield verbose_messages + error_msg, "", None, None
519
  return
520
 
521
  # Convert start_time and end_time to float or None
 
527
  is_temp_file = True # The trimmed audio is a temporary file
528
  verbose_messages += f"Audio trimmed from {start_time} to {end_time}\n"
529
  if verbose:
530
+ yield verbose_messages, "", None, None
531
 
532
  # Model caching
533
  model_key = (pipeline_type, model_id)
 
559
  else:
560
  error_msg = "Invalid pipeline type. Only 'sensevoice' is supported."
561
  logging.error(error_msg)
562
+ yield verbose_messages + error_msg, "", None, None
563
  return
564
  loaded_models[model_key] = model
565
 
 
597
  transcription_time = end_time_perf - start_time_perf
598
  audio_file_size = os.path.getsize(audio_path) / (1024 * 1024)
599
 
600
+ try:
601
+ audio_duration = len(AudioSegment.from_file(audio_path)) / 1000.0
602
+ except Exception:
603
+ audio_duration = max(float(end_time or 0), 1.0)
604
+
605
  metrics_output = (
606
  f"Transcription time: {transcription_time:.2f} seconds\n"
607
  f"Audio file size: {audio_file_size:.2f} MB\n"
608
+ f"Audio duration: {audio_duration:.2f} seconds\n"
609
+ "SRT timing is estimated from text/audio duration when the model provides no timestamps.\n"
610
  )
611
 
612
+ # Save both plain-text transcript and SRT subtitles.
613
  transcription_file = save_transcription(transcription)
614
+ srt_file = save_srt(transcription, audio_duration)
615
 
616
  # Always yield the final result, regardless of verbose setting
617
  final_metrics = verbose_messages + metrics_output
618
+ yield final_metrics, transcription, transcription_file, srt_file
619
 
620
  except Exception as e:
621
  error_msg = f"An error occurred during transcription: {str(e)}"
622
  logging.error(error_msg)
623
+ prefix = locals().get("verbose_messages", "")
624
+ yield prefix + error_msg, "", None, None
625
 
626
  finally:
627
  # Clean up temporary audio files
 
672
  with gr.Row():
673
  metrics_output = gr.Textbox(label="Transcription Metrics and Verbose Messages", lines=10)
674
  transcription_output = gr.Textbox(label="Transcription", lines=10)
675
+ transcription_file = gr.File(label="Download TXT")
676
+ srt_file_output = gr.File(label="Download SRT Subtitles")
677
 
678
  def update_model_dropdown(pipeline_type):
679
  """
 
707
  transcribe_button.click(
708
  transcribe_with_progress,
709
  inputs=[audio_input, audio_url, proxy_url, proxy_username, proxy_password, pipeline_type, model_id, download_method, start_time, end_time, verbose],
710
+ outputs=[metrics_output, transcription_output, transcription_file, srt_file_output]
711
  )
712
 
713
  # Note: For examples, users should use local audio files or upload their own files