fix transcribe bug
Browse files
app.py
CHANGED
|
@@ -1,50 +1,80 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
MODEL_NAME = "palli23/whisper-small-sam_spjall"
|
| 7 |
|
| 8 |
-
print("
|
| 9 |
|
|
|
|
| 10 |
pipe = pipeline(
|
| 11 |
"automatic-speech-recognition",
|
| 12 |
model=MODEL_NAME,
|
| 13 |
-
torch_dtype=
|
| 14 |
device="cuda",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
token=os.getenv("HF_TOKEN")
|
| 16 |
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
def transcribe(audio_path):
|
| 23 |
if not audio_path:
|
| 24 |
-
return "
|
| 25 |
-
result = pipe(audio_path, chunk_length_s=30, batch_size=16)
|
| 26 |
-
return result["text"].strip()
|
| 27 |
-
|
| 28 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 29 |
-
gr.Markdown("# Íslenskt Whisper – T4 small (mjög hratt & nákvæmt)")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
btn.click(transcribe, audio, out).then(
|
| 47 |
-
lambda: gr.update(active=False), outputs=timer
|
| 48 |
-
)
|
| 49 |
|
| 50 |
-
demo.launch(auth=("beta", "beta2025"))
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
+
import torch
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
MODEL_NAME = "palli23/whisper-small-sam_spjall"
|
| 8 |
|
| 9 |
+
print("Loading optimized Whisper Small for T4...")
|
| 10 |
|
| 11 |
+
# Load once + T4-specific optimizations
|
| 12 |
pipe = pipeline(
|
| 13 |
"automatic-speech-recognition",
|
| 14 |
model=MODEL_NAME,
|
| 15 |
+
torch_dtype=torch.float16, # FP16 = 2x faster, <4GB VRAM on T4
|
| 16 |
device="cuda",
|
| 17 |
+
model_kwargs={
|
| 18 |
+
"attn_implementation": "flash_attention_2", # 20–30% faster attention
|
| 19 |
+
"use_cache": True,
|
| 20 |
+
},
|
| 21 |
token=os.getenv("HF_TOKEN")
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Pre-set Icelandic for no detection overhead
|
| 25 |
+
pipe.model.generation_config.language = "is"
|
| 26 |
+
pipe.model.generation_config.task = "transcribe"
|
| 27 |
|
| 28 |
+
print(f"Model ready! VRAM used: {torch.cuda.memory_allocated() / 1e9:.1f}GB")
|
| 29 |
+
|
| 30 |
+
@spaces.GPU # No duration—let T4 run free
|
| 31 |
def transcribe(audio_path):
|
| 32 |
if not audio_path:
|
| 33 |
+
return "Upload audio first"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
try:
|
| 36 |
+
# Clear cache to prevent OOM aborts
|
| 37 |
+
torch.cuda.empty_cache()
|
| 38 |
+
|
| 39 |
+
result = pipe(
|
| 40 |
+
audio_path,
|
| 41 |
+
chunk_length_s=15, # Shorter = faster on T4 (less recompute)
|
| 42 |
+
batch_size=32, # Max for T4's 16GB VRAM
|
| 43 |
+
stride_length_s=(3, 1), # Minimal overlap = speed win
|
| 44 |
+
return_timestamps=False,
|
| 45 |
+
generate_kwargs={
|
| 46 |
+
"do_sample": False, # Deterministic, faster
|
| 47 |
+
"num_beams": 1, # No beam search = 2x faster
|
| 48 |
+
}
|
| 49 |
)
|
| 50 |
+
text = result["text"].strip()
|
| 51 |
+
|
| 52 |
+
# Post-clear to free VRAM
|
| 53 |
+
torch.cuda.empty_cache()
|
| 54 |
+
|
| 55 |
+
return f"✅ Done in {torch.cuda.max_memory_allocated() / 1e9:.1f}GB VRAM\n\n{text}"
|
| 56 |
+
|
| 57 |
+
except RuntimeError as e:
|
| 58 |
+
if "out of memory" in str(e):
|
| 59 |
+
return "❌ OOM error—try shorter audio (<3min). VRAM spiked too high."
|
| 60 |
+
raise gr.Error(f"GPU task failed: {str(e)}") # Catch & re-raise as Gradio error
|
| 61 |
+
|
| 62 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 63 |
+
gr.Markdown("# Icelandic Whisper Small – T4 Optimized (No Aborts)")
|
| 64 |
+
gr.Markdown("Upload <5min audio → Expect **10–20s** (monitors VRAM to prevent kills)")
|
| 65 |
+
|
| 66 |
+
audio = gr.Audio(type="filepath", label="Audio (mp3/wav, <5min for best speed)")
|
| 67 |
+
btn = gr.Button("Transcribe", variant="primary")
|
| 68 |
|
| 69 |
+
# Add VRAM status for debugging
|
| 70 |
+
status = gr.Markdown("VRAM: Ready")
|
| 71 |
+
|
| 72 |
+
out = gr.Textbox(label="Transcription", lines=25, show_copy_button=True)
|
| 73 |
+
|
| 74 |
+
def update_status():
|
| 75 |
+
vram = torch.cuda.memory_allocated() / 1e9
|
| 76 |
+
return f"VRAM: {vram:.1f}GB used"
|
| 77 |
|
| 78 |
+
btn.click(transcribe, audio, out).then(update_status, outputs=status)
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
demo.launch(auth=("beta", "beta2025"), max_threads=4) # Queue for concurrency
|