gaurannggg7 commited on
Commit
4f2f9ba
·
verified ·
1 Parent(s): 2ca0c8e

Update whisper_transcribe.py

Browse files
Files changed (1) hide show
  1. whisper_transcribe.py +1 -77
whisper_transcribe.py CHANGED
@@ -1,6 +1,4 @@
1
  import os
2
- import sounddevice as sd
3
- import soundfile as sf
4
  import tempfile
5
  import whisper
6
  from utils import get_best_device
@@ -39,78 +37,4 @@ def transcribe_file(path: str, model_name: str = WHISPER_MODEL_NAME) -> str:
39
  result = model.transcribe(path, fp16=False)
40
  return result.get("text", "").strip()
41
  except Exception as e:
42
- raise RuntimeError(f"Whisper transcription failed: {e}")
43
-
44
-
45
- def list_input_devices():
46
- """
47
- Return a list of (index, name) for all input-capable audio devices.
48
- """
49
- devices = sd.query_devices()
50
- return [(i, d.get('name', '')) for i, d in enumerate(devices) if d.get('max_input_channels', 0) > 0]
51
-
52
-
53
- def record_audio(duration: int, sr: int = 16000, device_index: int = None) -> tuple[int, list[float]]:
54
- """
55
- Record `duration` seconds of mono audio at sample rate `sr`.
56
- Automatically selects a valid input device if none is provided.
57
- Raises RuntimeError on failure.
58
- """
59
- devices = list_input_devices()
60
- if not devices:
61
- raise RuntimeError("No input audio devices found.")
62
-
63
- # Choose device: user-specified or default, else first available
64
- if device_index is None:
65
- default = sd.default.device
66
- if isinstance(default, (tuple, list)) and default[0] is not None:
67
- device_index = default[0]
68
- else:
69
- device_index = devices[0][0]
70
-
71
- # Validate device
72
- if device_index not in [i for i, _ in devices]:
73
- device_index = devices[0][0]
74
-
75
- # Configure recorder
76
- sd.default.device = (device_index, None)
77
- sd.default.samplerate = sr
78
- sd.default.channels = 1
79
-
80
- print(f"Recording {duration}s at {sr}Hz from device #{device_index}…")
81
- try:
82
- data = sd.rec(int(duration * sr), dtype='float32', channels=1)
83
- sd.wait()
84
- except Exception as e:
85
- raise RuntimeError(f"Audio recording failed: {e}")
86
-
87
- # Convert to 1D float32 list
88
- wav = data.flatten().tolist()
89
- return sr, wav
90
-
91
-
92
- def transcribe_from_mic(duration: int) -> str:
93
- """
94
- Record `duration` seconds from the microphone, save to a temp WAV, transcribe, then clean up.
95
- Raises RuntimeError on recording or transcription errors.
96
- """
97
- try:
98
- sr, wav = record_audio(duration)
99
- except Exception as e:
100
- raise RuntimeError(f"Microphone recording error: {e}")
101
-
102
- # Write to temporary WAV file
103
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
104
- tmp_path = tmp.name
105
- sf.write(tmp_path, wav, sr)
106
-
107
- try:
108
- text = transcribe_file(tmp_path)
109
- finally:
110
- # Ensure cleanup
111
- try:
112
- os.remove(tmp_path)
113
- except OSError:
114
- pass
115
-
116
- return text
 
1
  import os
 
 
2
  import tempfile
3
  import whisper
4
  from utils import get_best_device
 
37
  result = model.transcribe(path, fp16=False)
38
  return result.get("text", "").strip()
39
  except Exception as e:
40
+ raise RuntimeError(f"Whisper transcription failed: {e}")