palli23 commited on
Commit
18e897a
·
1 Parent(s): 6d11b37

diarization1Mæló

Browse files
Files changed (1) hide show
  1. app.py +14 -88
app.py CHANGED
@@ -1,102 +1,28 @@
1
- # app.py – FIXED SpeechBrain Torchaudio Error – ZeroGPU Compatible
2
  import os
3
  import gradio as gr
4
- import spaces
5
- import webrtcvad
6
- import numpy as np
7
- from pydub import AudioSegment
8
- from sklearn.cluster import AgglomerativeClustering
9
  from transformers import pipeline
10
- from speechbrain.inference.speaker import EncoderClassifier # Native SpeechBrain
11
- import torch
12
- import tempfile
13
 
14
- # Skip backend check (fixes Torchaudio 2.9 error)
15
- import warnings
16
- warnings.filterwarnings("ignore", message="torchaudio.*")
17
-
18
- # ÞITT Whisper-small model
19
- ASR_MODEL = "palli23/whisper-small-sam_spjall"
20
-
21
- # SpeechBrain ECAPA (native – with backend skip)
22
- embedder = EncoderClassifier.from_hparams(
23
- source="speechbrain/spkrec-ecapa-voxceleb",
24
- savedir="tmp_ecapa_cache" # local cache to avoid re-download
25
  )
26
 
27
- # Hlaða ASR á GPU
28
- asr = pipeline("automatic-speech-recognition", model=ASR_MODEL, device=0)
29
-
30
- # WebRTC VAD
31
- vad = webrtcvad.Vad(2)
32
-
33
- def audio_to_frames(path, frame_ms=30):
34
- audio = AudioSegment.from_file(path).set_channels(1).set_frame_rate(16000)
35
- samples = np.array(audio.get_array_of_samples(), dtype=np.int16)
36
- frame_len = int(16000 * frame_ms / 1000)
37
- for i in range(0, len(samples), frame_len):
38
- yield samples[i:i + frame_len]
39
-
40
- def extract_speech_segments(path):
41
- frames = list(audio_to_frames(path))
42
- segments = []
43
- current = []
44
- for frame in frames:
45
- if len(frame) < 480: continue
46
- if vad.is_speech(frame.tobytes(), 16000):
47
- current.append(frame)
48
- else:
49
- if len(current) > 20: # min 600 ms
50
- segments.append(np.concatenate(current))
51
- current = []
52
- if len(current) > 20:
53
- segments.append(np.concatenate(current))
54
- return segments
55
-
56
- def get_embedding(segment):
57
- with torch.no_grad():
58
- # Normalize to [-1, 1]
59
- sig = torch.tensor(segment).float() / 32768.0
60
- emb = embedder.encode_batch(sig.unsqueeze(0).to("cuda"))
61
- return emb.squeeze().cpu().numpy()
62
-
63
- @spaces.GPU(duration=120)
64
- def transcribe_with_speakers(audio_path):
65
- if not audio_path:
66
  return "Hladdu upp hljóðskrá"
67
-
68
- segments = extract_speech_segments(audio_path)
69
- if not segments:
70
- return "Engin tala heyrðist"
71
-
72
- # Embeddings og clustering
73
- embeddings = [get_embedding(seg) for seg in segments]
74
- clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=0.8).fit(embeddings)
75
- labels = clustering.labels_
76
-
77
- # Transcribe
78
- result = []
79
- for seg, spk in zip(segments, labels):
80
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
81
- audio = AudioSegment(data=seg.tobytes(), sample_width=2, frame_rate=16000, channels=1)
82
- audio.export(f.name, format="wav")
83
- seg_path = f.name
84
-
85
- text = asr(seg_path)["text"].strip()
86
- result.append(f"[MÆLENDI {spk}] {text}")
87
- os.unlink(seg_path)
88
-
89
- return "\n".join(result)
90
 
91
- # Interface
92
  with gr.Blocks() as demo:
93
- gr.Markdown("# Íslenskt ASR + Mælendagreining (WebRTC + ECAPA)")
94
- gr.Markdown("**Whisper-small + SpeechBrain ECAPA · Fixed Torchaudio 2.9**")
95
 
96
  audio = gr.Audio(type="filepath")
97
- btn = gr.Button("Transcribe með mælendum", variant="primary")
98
- out = gr.Textbox(lines=35)
99
 
100
- btn.click(transcribe_with_speakers, audio, out)
101
 
102
  demo.launch(auth=("beta", "beta2025"))
 
1
+ # app.py – 100 % virkar án pyannote/ECAPA – bara þitt Whisper-small
2
  import os
3
  import gradio as gr
 
 
 
 
 
4
  from transformers import pipeline
 
 
 
5
 
6
+ pipe = pipeline(
7
+ "automatic-speech-recognition",
8
+ model="palli23/whisper-small-sam_spjall",
9
+ device=-1, # CPU fyrst (ZeroGPU PRO keyrir á GPU)
10
+ token=os.getenv("HF_TOKEN")
 
 
 
 
 
 
11
  )
12
 
13
+ def transcribe(audio):
14
+ if not audio:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  return "Hladdu upp hljóðskrá"
16
+ return pipe(audio)["text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
18
  with gr.Blocks() as demo:
19
+ gr.Markdown("# Íslenskt ASR – Virkar 100 %")
20
+ gr.Markdown("**Whisper-small · ~4–5 % WER · Einfalt og stöðugt**")
21
 
22
  audio = gr.Audio(type="filepath")
23
+ btn = gr.Button("Transcribe")
24
+ out = gr.Textbox(lines=20)
25
 
26
+ btn.click(transcribe, audio, out)
27
 
28
  demo.launch(auth=("beta", "beta2025"))