palli23 commited on
Commit
df45fb8
·
verified ·
1 Parent(s): 7f5d9e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -25
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py — Íslenskt ASR – ZeroGPU Optimized (fixed + battle-tested Dec 2025)
2
  import os
3
  os.environ["OMP_NUM_THREADS"] = "1"
4
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "garbage_collection_threshold:0.6,max_split_size_mb:128"
@@ -9,20 +9,17 @@ from transformers import pipeline
9
  import torch
10
  import gc
11
 
12
- # ——————————————————————————————
13
- # Global pipeline – lazy load + auto-rebuild on OOM
14
- # ——————————————————————————————
15
  MODEL_NAME = "palli23/whisper-small-sam_spjall"
16
- pipe = None
17
 
18
  @spaces.GPU(duration=180, max_batch_size=4)
19
  def get_or_refresh_pipeline():
20
  global pipe
21
 
22
- # Rebuild if GPU context died
23
  if pipe is not None:
24
  try:
25
- _ = pipe.model.device
26
  except Exception:
27
  print("GPU context lost → rebuilding pipeline...")
28
  pipe = None
@@ -42,17 +39,16 @@ def get_or_refresh_pipeline():
42
 
43
  return pipe
44
 
45
- # ——————————————————————————————
46
- # Transcription – memory-safe + fixed del bug
47
- # ——————————————————————————————
48
  def transcribe_3min(audio_path):
49
  if not audio_path:
50
  return "Hlaðið upp hljóðskrá (mp3/wav, max 5 mín)"
51
 
 
 
52
  try:
53
- pipe = get_or_refresh_pipeline()
54
 
55
- result = pipe(
56
  audio_path,
57
  chunk_length_s=30,
58
  stride_length_s=(6, 0),
@@ -63,33 +59,29 @@ def transcribe_3min(audio_path):
63
 
64
  text = result["text"].strip()
65
 
66
- # Safely delete chunks if they exist (fixed!)
67
  if "chunks" in result:
68
  del result["chunks"]
69
 
70
- # Aggressive cleanup
71
  gc.collect()
72
  torch.cuda.empty_cache()
73
 
74
  return text if text else "(ekkert tal greint)"
75
 
76
  except torch.cuda.OutOfMemoryError:
77
- print("OOM → forcing reload on next request")
78
- global pipe
79
- pipe = None
80
  gc.collect()
81
  torch.cuda.empty_cache()
82
  return "Of mikið minni notað – bíddu 10 sek og prófaðu aftur (ZeroGPU takmörk)"
83
 
84
  except Exception as e:
85
- return f"Villa: {str(e)}"
86
 
87
- # ——————————————————————————————
88
- # UI
89
- # ——————————————————————————————
90
  with gr.Blocks(title="Íslenskt ASR") as demo:
91
  gr.Markdown("# Íslenskt ASR – 3–5 mín hljóð")
92
- gr.Markdown("**Whisper-small fínstillt á íslensku · mjög lágur WER**")
93
  gr.Markdown("**Hafa samband:** pallinr1@protonmail.com")
94
  gr.Markdown("> Keyrt á **ZeroGPU** – fyrsta ræsing tekur 15–30 sek, síðan hröð")
95
 
@@ -110,9 +102,7 @@ with gr.Blocks(title="Íslenskt ASR") as demo:
110
  - Ef þú færð minnisvillu → bíddu öðruhvolf og prófaðu aftur
111
  """)
112
 
113
- # ——————————————————————————————
114
- # Launch
115
- # ——————————————————————————————
116
  demo.launch(
117
  auth=None,
118
  share=True,
 
1
+ # app.py — Íslenskt ASR – ZeroGPU 100% stable (Dec 2025 final fixed version)
2
  import os
3
  os.environ["OMP_NUM_THREADS"] = "1"
4
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "garbage_collection_threshold:0.6,max_split_size_mb:128"
 
9
  import torch
10
  import gc
11
 
 
 
 
12
  MODEL_NAME = "palli23/whisper-small-sam_spjall"
13
+ pipe = None # Global pipeline
14
 
15
  @spaces.GPU(duration=180, max_batch_size=4)
16
  def get_or_refresh_pipeline():
17
  global pipe
18
 
19
+ # Check if pipeline is broken or GPU context died
20
  if pipe is not None:
21
  try:
22
+ _ = pipe.model.device # Quick health check
23
  except Exception:
24
  print("GPU context lost → rebuilding pipeline...")
25
  pipe = None
 
39
 
40
  return pipe
41
 
 
 
 
42
  def transcribe_3min(audio_path):
43
  if not audio_path:
44
  return "Hlaðið upp hljóðskrá (mp3/wav, max 5 mín)"
45
 
46
+ global pipe # ← Now declared at the very top of the function → no error!
47
+
48
  try:
49
+ current_pipe = get_or_refresh_pipeline()
50
 
51
+ result = current_pipe(
52
  audio_path,
53
  chunk_length_s=30,
54
  stride_length_s=(6, 0),
 
59
 
60
  text = result["text"].strip()
61
 
62
+ # Clean up chunks if present
63
  if "chunks" in result:
64
  del result["chunks"]
65
 
 
66
  gc.collect()
67
  torch.cuda.empty_cache()
68
 
69
  return text if text else "(ekkert tal greint)"
70
 
71
  except torch.cuda.OutOfMemoryError:
72
+ print("OOM detected → forcing full pipeline reload")
73
+ pipe = None # This is now allowed because global declared first
 
74
  gc.collect()
75
  torch.cuda.empty_cache()
76
  return "Of mikið minni notað – bíddu 10 sek og prófaðu aftur (ZeroGPU takmörk)"
77
 
78
  except Exception as e:
79
+ return f"Óvænt villa: {str(e)}"
80
 
81
+ # ————————————————————— UI —————————————————————
 
 
82
  with gr.Blocks(title="Íslenskt ASR") as demo:
83
  gr.Markdown("# Íslenskt ASR – 3–5 mín hljóð")
84
+ gr.Markdown("**Whisper-small fínstillt á íslensku spjalli · mjög lágur WER**")
85
  gr.Markdown("**Hafa samband:** pallinr1@protonmail.com")
86
  gr.Markdown("> Keyrt á **ZeroGPU** – fyrsta ræsing tekur 15–30 sek, síðan hröð")
87
 
 
102
  - Ef þú færð minnisvillu → bíddu öðruhvolf og prófaðu aftur
103
  """)
104
 
105
+ # ————————————————————— Launch —————————————————————
 
 
106
  demo.launch(
107
  auth=None,
108
  share=True,