Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,16 +76,14 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 76 |
video.close()
|
| 77 |
print(f"Audio extracted: {audio_path} ({os.path.getsize(audio_path)} bytes)")
|
| 78 |
|
| 79 |
-
# Transcribe
|
| 80 |
progress(0.4, desc="Transcribing with Whisper Turbo...")
|
| 81 |
print(f"Starting transcription (language: {language})...")
|
| 82 |
|
| 83 |
transcription_result = pipe(
|
| 84 |
audio_path,
|
| 85 |
-
return_timestamps=
|
| 86 |
-
chunk_length_s=30,
|
| 87 |
-
stride_length_s=(4, 2),
|
| 88 |
-
batch_size=8,
|
| 89 |
generate_kwargs={
|
| 90 |
"language": language if language != "auto" else None,
|
| 91 |
"task": "transcribe"
|
|
@@ -94,53 +92,7 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 94 |
|
| 95 |
print(f"Transcription result keys: {transcription_result.keys()}")
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
if 'chunks' in transcription_result and transcription_result['chunks']:
|
| 99 |
-
words_data = transcription_result['chunks']
|
| 100 |
-
|
| 101 |
-
# Group words into readable subtitle chunks (max 10-12 words, max 4 seconds)
|
| 102 |
-
optimal_chunks = []
|
| 103 |
-
current_chunk_words = []
|
| 104 |
-
current_chunk_start = None
|
| 105 |
-
|
| 106 |
-
for word_info in words_data:
|
| 107 |
-
word_text = word_info['text']
|
| 108 |
-
word_start, word_end = word_info['timestamp']
|
| 109 |
-
|
| 110 |
-
if current_chunk_start is None:
|
| 111 |
-
current_chunk_start = word_start
|
| 112 |
-
|
| 113 |
-
current_chunk_words.append(word_text)
|
| 114 |
-
|
| 115 |
-
# Check if we should end this chunk
|
| 116 |
-
chunk_duration = word_end - current_chunk_start
|
| 117 |
-
should_break = (
|
| 118 |
-
len(current_chunk_words) >= 10 or # Max 10 words
|
| 119 |
-
chunk_duration >= 4.0 or # Max 4 seconds
|
| 120 |
-
word_text.strip().endswith(('.', '?', '!', '।')) # Sentence end
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
if should_break:
|
| 124 |
-
optimal_chunks.append({
|
| 125 |
-
'timestamp': (current_chunk_start, word_end),
|
| 126 |
-
'text': ''.join(current_chunk_words)
|
| 127 |
-
})
|
| 128 |
-
current_chunk_words = []
|
| 129 |
-
current_chunk_start = None
|
| 130 |
-
|
| 131 |
-
# Add remaining words
|
| 132 |
-
if current_chunk_words:
|
| 133 |
-
last_word = words_data[-1]
|
| 134 |
-
optimal_chunks.append({
|
| 135 |
-
'timestamp': (current_chunk_start, last_word['timestamp'][1]),
|
| 136 |
-
'text': ''.join(current_chunk_words)
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
-
transcription_result['chunks'] = optimal_chunks
|
| 140 |
-
print(f"Created {len(optimal_chunks)} optimal subtitle chunks from word timestamps")
|
| 141 |
-
else:
|
| 142 |
-
print("Warning: No word-level timestamps returned")
|
| 143 |
-
|
| 144 |
if not transcription_result.get('chunks') or len(transcription_result['chunks']) == 0:
|
| 145 |
error_msg = f"Transcription returned no segments"
|
| 146 |
print(error_msg)
|
|
@@ -151,17 +103,12 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
|
|
| 151 |
print(f"Transcription complete: {len(transcription_result['chunks'])} segments")
|
| 152 |
|
| 153 |
# Debug: Print first and last chunks
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
print(f"\nLast 3 chunks structure:")
|
| 161 |
-
for i, chunk in enumerate(transcription_result['chunks'][-3:], len(transcription_result['chunks'])-2):
|
| 162 |
-
duration = chunk['timestamp'][1] - chunk['timestamp'][0]
|
| 163 |
-
word_count = len(chunk['text'].split())
|
| 164 |
-
print(f" Chunk {i}: {duration:.2f}s, {word_count} words - {chunk['text'][:50]}...")
|
| 165 |
|
| 166 |
# Post-process: Split overly long chunks for better readability
|
| 167 |
processed_chunks = []
|
|
|
|
| 76 |
video.close()
|
| 77 |
print(f"Audio extracted: {audio_path} ({os.path.getsize(audio_path)} bytes)")
|
| 78 |
|
| 79 |
+
# Transcribe - keep chunk_length_s for full audio, but simplify everything else
|
| 80 |
progress(0.4, desc="Transcribing with Whisper Turbo...")
|
| 81 |
print(f"Starting transcription (language: {language})...")
|
| 82 |
|
| 83 |
transcription_result = pipe(
|
| 84 |
audio_path,
|
| 85 |
+
return_timestamps=True,
|
| 86 |
+
chunk_length_s=30, # Keep this to process full audio
|
|
|
|
|
|
|
| 87 |
generate_kwargs={
|
| 88 |
"language": language if language != "auto" else None,
|
| 89 |
"task": "transcribe"
|
|
|
|
| 92 |
|
| 93 |
print(f"Transcription result keys: {transcription_result.keys()}")
|
| 94 |
|
| 95 |
+
# Verify we have chunks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if not transcription_result.get('chunks') or len(transcription_result['chunks']) == 0:
|
| 97 |
error_msg = f"Transcription returned no segments"
|
| 98 |
print(error_msg)
|
|
|
|
| 103 |
print(f"Transcription complete: {len(transcription_result['chunks'])} segments")
|
| 104 |
|
| 105 |
# Debug: Print first and last chunks
|
| 106 |
+
print(f"\nFirst 3 chunks:")
|
| 107 |
+
for i, chunk in enumerate(transcription_result['chunks'][:3], 1):
|
| 108 |
+
print(f" {i}: {chunk}")
|
| 109 |
+
print(f"\nLast 3 chunks:")
|
| 110 |
+
for i, chunk in enumerate(transcription_result['chunks'][-3:], len(transcription_result['chunks'])-2):
|
| 111 |
+
print(f" {i}: {chunk}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Post-process: Split overly long chunks for better readability
|
| 114 |
processed_chunks = []
|