meraj12 commited on
Commit
a8f55c9
·
verified ·
1 Parent(s): 0171c4b

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +26 -19
utils.py CHANGED
@@ -1,24 +1,31 @@
1
- import torchaudio
 
 
 
2
  import os
 
 
3
 
4
- def save_uploaded_audio(uploaded_file, save_path="converted_audio.wav"):
5
- try:
6
- # Save to a temp file
7
- temp_path = f"temp_{uploaded_file.name}"
8
- with open(temp_path, "wb") as f:
9
- f.write(uploaded_file.read())
10
 
11
- # Load with torchaudio
12
- waveform, sample_rate = torchaudio.load(temp_path)
 
 
13
 
14
- # Convert to 16kHz mono .wav (Whisper preferred)
15
- waveform = waveform.mean(dim=0).unsqueeze(0)
16
- torchaudio.save(save_path, waveform, 16000)
17
-
18
- # Clean up temp
19
- os.remove(temp_path)
20
 
21
- return save_path
22
- except Exception as e:
23
- print(f"[Error] Audio conversion failed: {e}")
24
- return None
 
1
+ # utils.py
2
+ import sounddevice as sd
3
+ from scipy.io.wavfile import write
4
+ import tempfile
5
  import os
6
+ import whisper
7
+ import json
8
 
9
+ def record_audio(duration=5, fs=44100):
10
+ stt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
11
+ recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
12
+ sd.wait()
13
+ write(stt_file.name, fs, recording)
14
+ return stt_file.name
15
 
16
+ def transcribe_audio(audio_path, language="English"):
17
+ model = whisper.load_model("base")
18
+ result = model.transcribe(audio_path, language=language.lower())
19
+ return result['text']
20
 
21
+ def save_audio_file(uploaded_file, filename):
22
+ path = os.path.join("audio", filename)
23
+ os.makedirs("audio", exist_ok=True)
24
+ with open(path, "wb") as f:
25
+ f.write(uploaded_file.read())
26
+ return path
27
 
28
+ def save_chat_history(history, filename="chat_history.json"):
29
+ os.makedirs("history", exist_ok=True)
30
+ with open(os.path.join("history", filename), "w") as f:
31
+ json.dump(history, f, indent=4)