Freddolin commited on
Commit
636c045
·
verified ·
1 Parent(s): ff8f508

Update tools/asr_tool.py

Browse files
Files changed (1) hide show
  1. tools/asr_tool.py +22 -4
tools/asr_tool.py CHANGED
@@ -1,9 +1,27 @@
1
  from faster_whisper import WhisperModel
 
2
 
3
- model = WhisperModel("base", device="cpu")
 
 
 
 
 
 
 
4
 
5
  def transcribe_audio(audio_path: str) -> str:
6
- segments, _ = model.transcribe(audio_path)
7
- transcription = " ".join(segment.text for segment in segments)
8
- return transcription or "No transcription found."
 
 
 
 
 
 
 
 
 
 
9
 
 
1
  from faster_whisper import WhisperModel
2
+ import os
3
 
4
+ # Globalt model för att undvika återladdning
5
+ model = None
6
+
7
+ def get_model():
8
+ global model
9
+ if model is None:
10
+ model = WhisperModel("base", device="cpu", compute_type="int8")
11
+ return model
12
 
13
  def transcribe_audio(audio_path: str) -> str:
14
+ """Transkribera ljudfil till text"""
15
+ try:
16
+ if not os.path.exists(audio_path):
17
+ return f"Audio file not found: {audio_path}"
18
+
19
+ model = get_model()
20
+ segments, info = model.transcribe(audio_path, beam_size=5)
21
+
22
+ transcription = " ".join(segment.text for segment in segments)
23
+ return transcription.strip() or "No transcription found."
24
+
25
+ except Exception as e:
26
+ return f"Error transcribing audio: {str(e)}"
27