palli23 commited on
Commit
2102ae8
·
verified ·
1 Parent(s): 9489fe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -1,4 +1,5 @@
1
- # app.py — Íslenskt ASR – 3 mínútur (ZeroGPU, works forever, your original code!)
 
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 loaded ONLY inside GPU worker (ZeroGPU safe)
14
  # ——————————————————————————————
15
  MODEL_NAME = "palli23/whisper-small-sam_spjall"
16
- #MODEL_NAME = "language-and-voice-lab/whisper-large-icelandic-62640-steps-967h"
17
- @spaces.GPU(duration=180) # Auto-refreshes every 3 min idle → Space never dies
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, # GPU 0 (safe inside @spaces.GPU)
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
- # Aggressive memory cleanup so ZeroGPU stays happy
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 your original, unchanged
50
  # ——————————————————————————————
51
- with gr.Blocks() as demo: # removed 'theme=' (was causing error)
52
  gr.Markdown("# Íslenskt ASR – 3 mínútur")
53
- gr.Markdown("**Whisper small· mjög lágur WER á prófunarupptökum · allt að 5 mín hljóð**")
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=30, label="Útskrift")
 
61
  btn.click(fn=transcribe_3min, inputs=audio_in, outputs=output)
62
 
63
  # ——————————————————————————————
64
- # PUBLIC NO LOGIN, NO PASSWORD
65
  # ——————————————————————————————
66
  demo.launch(
67
- auth=None, # ← No login
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
  )