File size: 996 Bytes
a8f55c9
 
 
 
3441734
a8f55c9
 
9cc018b
a8f55c9
 
 
 
 
 
9cc018b
a8f55c9
 
 
 
9cc018b
a8f55c9
 
 
 
 
 
3441734
a8f55c9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# utils.py
import sounddevice as sd
from scipy.io.wavfile import write
import tempfile
import os
import whisper
import json

def record_audio(duration=5, fs=44100):
    stt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
    recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
    sd.wait()
    write(stt_file.name, fs, recording)
    return stt_file.name

def transcribe_audio(audio_path, language="English"):
    model = whisper.load_model("base")
    result = model.transcribe(audio_path, language=language.lower())
    return result['text']

def save_audio_file(uploaded_file, filename):
    path = os.path.join("audio", filename)
    os.makedirs("audio", exist_ok=True)
    with open(path, "wb") as f:
        f.write(uploaded_file.read())
    return path

def save_chat_history(history, filename="chat_history.json"):
    os.makedirs("history", exist_ok=True)
    with open(os.path.join("history", filename), "w") as f:
        json.dump(history, f, indent=4)