palli23 commited on
Commit
52d795c
·
verified ·
1 Parent(s): a64de37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -13,7 +13,6 @@ import torch
13
  os.environ["OMP_NUM_THREADS"] = "1"
14
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
15
 
16
-
17
  # ——————————————————————————————
18
  # ZeroGPU worker – model loaded once
19
  # ——————————————————————————————
@@ -21,58 +20,56 @@ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
21
  def transcribe_files(audio_files):
22
  if not audio_files:
23
  return None, "Hlaðið upp hljóðskrám"
24
-
25
  audio_files = audio_files[:10]
26
-
27
  workdir = tempfile.mkdtemp()
28
  outdir = os.path.join(workdir, "transcripts")
29
  os.makedirs(outdir, exist_ok=True)
30
-
31
  pipe = pipeline(
32
  "automatic-speech-recognition",
33
  model="palli23/whisper-small-sam_spjall",
34
  torch_dtype=torch.float16,
35
  device=0,
36
  )
37
-
38
  for file in audio_files:
39
  audio_path = file.name
40
  base = os.path.splitext(os.path.basename(audio_path))[0]
41
  txt_path = os.path.join(outdir, f"{base}.txt")
42
-
43
  result = pipe(
44
  audio_path,
45
  chunk_length_s=30,
46
  batch_size=8,
47
  return_timestamps=False,
48
  generate_kwargs={
49
- "language": "is", # ← Keep it here for now
50
- "task": "transcribe", # ← Add this too
51
  "num_beams": 5,
52
  "repetition_penalty": 1.2,
53
  "no_repeat_ngram_size": 3,
54
  "temperature": 0.0,
55
- },
56
- )
57
  )
58
-
59
  with open(txt_path, "w", encoding="utf-8") as f:
60
  f.write(result["text"].strip())
61
-
62
  # Zip outputs
63
  zip_path = os.path.join(workdir, "transcripts.zip")
64
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as z:
65
  for fname in os.listdir(outdir):
66
  z.write(os.path.join(outdir, fname), arcname=fname)
67
-
68
  # Cleanup
69
  del pipe
70
  gc.collect()
71
  torch.cuda.empty_cache()
72
-
73
  return zip_path, "Lokið ✅"
74
 
75
-
76
  # ——————————————————————————————
77
  # UI
78
  # ——————————————————————————————
@@ -81,25 +78,24 @@ with gr.Blocks() as demo:
81
  gr.Markdown(
82
  "**palli23/whisper-small-sam_spjall** · sama stillingar · .wav / .mp3"
83
  )
84
-
85
  audio_in = gr.File(
86
  label="Hlaðið upp allt að 10 .wav / .mp3 skrám",
87
  file_types=[".wav", ".mp3"],
88
  file_count="multiple",
89
  )
90
-
91
  btn = gr.Button("Transcribe", variant="primary", size="lg")
92
-
93
  zip_out = gr.File(label="Niðurhal – transcripts.zip")
94
  status = gr.Textbox(label="Staða", interactive=False)
95
-
96
  btn.click(
97
  fn=transcribe_files,
98
  inputs=audio_in,
99
  outputs=[zip_out, status],
100
  )
101
 
102
-
103
  # ——————————————————————————————
104
  # Launch
105
  # ——————————————————————————————
@@ -107,4 +103,4 @@ demo.launch(
107
  share=True,
108
  server_name="0.0.0.0",
109
  server_port=7860,
110
- )
 
13
  os.environ["OMP_NUM_THREADS"] = "1"
14
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
15
 
 
16
  # ——————————————————————————————
17
  # ZeroGPU worker – model loaded once
18
  # ——————————————————————————————
 
20
  def transcribe_files(audio_files):
21
  if not audio_files:
22
  return None, "Hlaðið upp hljóðskrám"
23
+
24
  audio_files = audio_files[:10]
25
+
26
  workdir = tempfile.mkdtemp()
27
  outdir = os.path.join(workdir, "transcripts")
28
  os.makedirs(outdir, exist_ok=True)
29
+
30
  pipe = pipeline(
31
  "automatic-speech-recognition",
32
  model="palli23/whisper-small-sam_spjall",
33
  torch_dtype=torch.float16,
34
  device=0,
35
  )
36
+
37
  for file in audio_files:
38
  audio_path = file.name
39
  base = os.path.splitext(os.path.basename(audio_path))[0]
40
  txt_path = os.path.join(outdir, f"{base}.txt")
41
+
42
  result = pipe(
43
  audio_path,
44
  chunk_length_s=30,
45
  batch_size=8,
46
  return_timestamps=False,
47
  generate_kwargs={
48
+ "language": "is",
49
+ "task": "transcribe",
50
  "num_beams": 5,
51
  "repetition_penalty": 1.2,
52
  "no_repeat_ngram_size": 3,
53
  "temperature": 0.0,
54
+ },
 
55
  )
56
+
57
  with open(txt_path, "w", encoding="utf-8") as f:
58
  f.write(result["text"].strip())
59
+
60
  # Zip outputs
61
  zip_path = os.path.join(workdir, "transcripts.zip")
62
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as z:
63
  for fname in os.listdir(outdir):
64
  z.write(os.path.join(outdir, fname), arcname=fname)
65
+
66
  # Cleanup
67
  del pipe
68
  gc.collect()
69
  torch.cuda.empty_cache()
70
+
71
  return zip_path, "Lokið ✅"
72
 
 
73
  # ——————————————————————————————
74
  # UI
75
  # ——————————————————————————————
 
78
  gr.Markdown(
79
  "**palli23/whisper-small-sam_spjall** · sama stillingar · .wav / .mp3"
80
  )
81
+
82
  audio_in = gr.File(
83
  label="Hlaðið upp allt að 10 .wav / .mp3 skrám",
84
  file_types=[".wav", ".mp3"],
85
  file_count="multiple",
86
  )
87
+
88
  btn = gr.Button("Transcribe", variant="primary", size="lg")
89
+
90
  zip_out = gr.File(label="Niðurhal – transcripts.zip")
91
  status = gr.Textbox(label="Staða", interactive=False)
92
+
93
  btn.click(
94
  fn=transcribe_files,
95
  inputs=audio_in,
96
  outputs=[zip_out, status],
97
  )
98
 
 
99
  # ——————————————————————————————
100
  # Launch
101
  # ——————————————————————————————
 
103
  share=True,
104
  server_name="0.0.0.0",
105
  server_port=7860,
106
+ )