Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
# app.py — Íslenskt ASR –
|
|
|
|
| 2 |
import os
|
| 3 |
os.environ["OMP_NUM_THREADS"] = "1"
|
| 4 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
|
@@ -10,64 +11,68 @@ import torch
|
|
| 10 |
import gc
|
| 11 |
|
| 12 |
# ——————————————————————————————
|
| 13 |
-
# Model
|
| 14 |
# ——————————————————————————————
|
| 15 |
MODEL_NAME = "palli23/whisper-small-sam_spjall"
|
| 16 |
-
|
| 17 |
-
@spaces.GPU(duration=180)
|
| 18 |
def transcribe_3min(audio_path):
|
| 19 |
if not audio_path:
|
| 20 |
return "Hlaðið upp hljóðskrá"
|
| 21 |
-
|
| 22 |
-
# Load pipeline directly on GPU inside the worker (this is the simplest & works 100%)
|
| 23 |
pipe = pipeline(
|
| 24 |
"automatic-speech-recognition",
|
| 25 |
model=MODEL_NAME,
|
| 26 |
torch_dtype=torch.float16,
|
| 27 |
-
device=0,
|
| 28 |
-
token=os.getenv("HF_TOKEN"),
|
| 29 |
)
|
| 30 |
-
|
| 31 |
result = pipe(
|
| 32 |
audio_path,
|
| 33 |
chunk_length_s=30,
|
| 34 |
stride_length_s=(6, 0),
|
| 35 |
batch_size=8,
|
| 36 |
return_timestamps=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
if "chunks" in result:
|
| 41 |
-
del result["chunks"]
|
| 42 |
del pipe
|
| 43 |
gc.collect()
|
| 44 |
torch.cuda.empty_cache()
|
| 45 |
-
|
| 46 |
return result["text"]
|
| 47 |
|
| 48 |
# ——————————————————————————————
|
| 49 |
-
# UI
|
| 50 |
# ——————————————————————————————
|
| 51 |
-
with gr.Blocks() as demo:
|
| 52 |
gr.Markdown("# Íslenskt ASR – 3 mínútur")
|
| 53 |
-
gr.Markdown("**
|
| 54 |
gr.Markdown("**Hafa samband:** pallinr1@protonmail.com")
|
|
|
|
| 55 |
audio_in = gr.Audio(
|
| 56 |
type="filepath",
|
| 57 |
-
label="Hlaðið upp .mp3 / .wav (max 5 mín)"
|
| 58 |
)
|
| 59 |
btn = gr.Button("Transcribe", variant="primary", size="lg")
|
| 60 |
-
output = gr.Textbox(lines=
|
|
|
|
| 61 |
btn.click(fn=transcribe_3min, inputs=audio_in, outputs=output)
|
| 62 |
|
| 63 |
# ——————————————————————————————
|
| 64 |
-
#
|
| 65 |
# ——————————————————————————————
|
| 66 |
demo.launch(
|
| 67 |
-
|
| 68 |
-
share=True, # ← Public
|
| 69 |
server_name="0.0.0.0",
|
| 70 |
server_port=7860,
|
| 71 |
show_error=True,
|
| 72 |
-
quiet=False
|
|
|
|
| 73 |
)
|
|
|
|
| 1 |
+
# app.py — Íslenskt ASR – ZeroGPU + repetition_penalty=1.2 (perfect for your model)
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
os.environ["OMP_NUM_THREADS"] = "1"
|
| 5 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
|
|
|
|
| 11 |
import gc
|
| 12 |
|
| 13 |
# ——————————————————————————————
|
| 14 |
+
# Model + generation settings (repetition_penalty = 1.2)
|
| 15 |
# ——————————————————————————————
|
| 16 |
MODEL_NAME = "palli23/whisper-small-sam_spjall"
|
| 17 |
+
|
| 18 |
+
@spaces.GPU(duration=180)
|
| 19 |
def transcribe_3min(audio_path):
|
| 20 |
if not audio_path:
|
| 21 |
return "Hlaðið upp hljóðskrá"
|
| 22 |
+
|
|
|
|
| 23 |
pipe = pipeline(
|
| 24 |
"automatic-speech-recognition",
|
| 25 |
model=MODEL_NAME,
|
| 26 |
torch_dtype=torch.float16,
|
| 27 |
+
device=0, # GPU inside @spaces.GPU
|
| 28 |
+
token=os.getenv("HF_TOKEN"), # if you have private model
|
| 29 |
)
|
| 30 |
+
|
| 31 |
result = pipe(
|
| 32 |
audio_path,
|
| 33 |
chunk_length_s=30,
|
| 34 |
stride_length_s=(6, 0),
|
| 35 |
batch_size=8,
|
| 36 |
return_timestamps=False,
|
| 37 |
+
generate_kwargs={
|
| 38 |
+
"repetition_penalty": 1.2, # ← exactly what you want
|
| 39 |
+
"no_repeat_ngram_size": 3, # extra safety against loops
|
| 40 |
+
"temperature": 0.0,
|
| 41 |
+
}
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
# Clean up memory so ZeroGPU never dies
|
|
|
|
|
|
|
| 45 |
del pipe
|
| 46 |
gc.collect()
|
| 47 |
torch.cuda.empty_cache()
|
| 48 |
+
|
| 49 |
return result["text"]
|
| 50 |
|
| 51 |
# ——————————————————————————————
|
| 52 |
+
# UI – clean and fast
|
| 53 |
# ——————————————————————————————
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("# Íslenskt ASR – 3 mínútur")
|
| 56 |
+
gr.Markdown("**palli23/whisper-small-sam_spjall** · mjög lágur WER · allt að 5 mín hljóð")
|
| 57 |
gr.Markdown("**Hafa samband:** pallinr1@protonmail.com")
|
| 58 |
+
|
| 59 |
audio_in = gr.Audio(
|
| 60 |
type="filepath",
|
| 61 |
+
label="Hlaðið upp .mp3 / .wav (max ~5 mín)"
|
| 62 |
)
|
| 63 |
btn = gr.Button("Transcribe", variant="primary", size="lg")
|
| 64 |
+
output = gr.Textbox(lines=25, label="Útskrift", show_word_timestamps=False)
|
| 65 |
+
|
| 66 |
btn.click(fn=transcribe_3min, inputs=audio_in, outputs=output)
|
| 67 |
|
| 68 |
# ——————————————————————————————
|
| 69 |
+
# Public Space – no login
|
| 70 |
# ——————————————————————————————
|
| 71 |
demo.launch(
|
| 72 |
+
share=True,
|
|
|
|
| 73 |
server_name="0.0.0.0",
|
| 74 |
server_port=7860,
|
| 75 |
show_error=True,
|
| 76 |
+
quiet=False,
|
| 77 |
+
auth=None
|
| 78 |
)
|