fix transcribe bug
Browse files
app.py
CHANGED
|
@@ -1,50 +1,53 @@
|
|
| 1 |
-
# app.py –
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
from transformers import pipeline
|
|
|
|
|
|
|
| 6 |
|
| 7 |
MODEL_NAME = "palli23/whisper-small-sam_spjall"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
pipe = pipeline(
|
| 12 |
-
"automatic-speech-recognition",
|
| 13 |
-
model=MODEL_NAME,
|
| 14 |
-
torch_dtype="auto",
|
| 15 |
-
device="cuda" if os.getenv("SYSTEM") == "spaces" else "cpu",
|
| 16 |
-
model_kwargs={"attn_implementation": "sdpa"},
|
| 17 |
-
token=os.getenv("HF_TOKEN")
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
# *** THIS IS THE FIX: Uppfæra generation_config til að styðja language/task ***
|
| 21 |
-
pipe.model.generation_config.language = "is" # Default language
|
| 22 |
-
pipe.model.generation_config.task = "transcribe" # Default task
|
| 23 |
-
pipe.model.generation_config.forced_decoder_ids = None # Disable old forced IDs (deprecated)
|
| 24 |
-
pipe.model.generation_config.suppress_tokens = [] # Clear suppress if needed
|
| 25 |
-
print("Generation config uppfærð – allt klárt!")
|
| 26 |
-
|
| 27 |
-
@spaces.GPU(duration=120)
|
| 28 |
-
def transcribe_audio(audio_path):
|
| 29 |
if not audio_path:
|
| 30 |
-
return "Hladdu upp hljóðskrá
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py – ZeroGPU SAFE – 3 mín hljóð án "GPU task aborted"
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
from transformers import pipeline
|
| 6 |
+
import numpy as np
|
| 7 |
+
import librosa
|
| 8 |
|
| 9 |
MODEL_NAME = "palli23/whisper-small-sam_spjall"
|
| 10 |
|
| 11 |
+
@spaces.GPU(duration=60) # ← MEST 60 sek – ZeroGPU leyfir
|
| 12 |
+
def transcribe_safe(audio_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if not audio_path:
|
| 14 |
+
return "Hladdu upp hljóðskrá"
|
| 15 |
|
| 16 |
+
# Hlaða hljóð og klippa í 20 sek chunkar (mjög öruggt)
|
| 17 |
+
audio, sr = librosa.load(audio_path, sr=16000)
|
| 18 |
+
chunk_len = 16000 * 20 # 20 sek
|
| 19 |
+
stride = 16000 * 2 # 2 sek overlap
|
| 20 |
+
chunks = []
|
| 21 |
+
for i in range(0, len(audio), chunk_len - stride):
|
| 22 |
+
chunk = audio[i:i + chunk_len]
|
| 23 |
+
if len(chunk) < 16000: # undir 1 sek → hætta
|
| 24 |
+
break
|
| 25 |
+
chunks.append(chunk)
|
| 26 |
+
|
| 27 |
+
# Hlaða ASR á GPU (cached)
|
| 28 |
+
pipe = pipeline(
|
| 29 |
+
"automatic-speech-recognition",
|
| 30 |
+
model=MODEL_NAME,
|
| 31 |
+
device=0,
|
| 32 |
+
token=os.getenv("HF_TOKEN")
|
| 33 |
)
|
| 34 |
+
|
| 35 |
+
full_text = ""
|
| 36 |
+
for idx, chunk in enumerate(chunks):
|
| 37 |
+
result = pipe(chunk, batch_size=8)
|
| 38 |
+
full_text += result["text"] + " "
|
| 39 |
+
|
| 40 |
+
return full_text.strip() or "Ekkert heyrt"
|
| 41 |
|
| 42 |
+
# Gradio – fallegt og tilbúið fyrir 3 mín
|
| 43 |
+
with gr.Blocks(title="Íslenskt ASR – 3 mín ZeroGPU") as demo:
|
| 44 |
+
gr.Markdown("# Íslenskt ASR – 3 mín hljóð")
|
| 45 |
+
gr.Markdown("**~4 % WER · 25–45 sek · ZeroGPU (PRO)**")
|
| 46 |
+
|
| 47 |
+
audio = gr.Audio(type="filepath", label="Hladdu upp .mp3 / .wav (allt að 3 mín)")
|
| 48 |
+
btn = gr.Button("Transcribe (25–45 sek)", variant="primary", size="lg")
|
| 49 |
+
out = gr.Textbox(lines=30, label="Útskrift")
|
| 50 |
+
|
| 51 |
+
btn.click(transcribe_safe, inputs=audio, outputs=out)
|
| 52 |
|
| 53 |
+
demo.launch(auth=("beta", "beta2025"))
|