Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,24 +1,31 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
import os
|
|
|
|
|
3 |
|
4 |
-
def
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
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)
|