Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,70 +1,46 @@
|
|
1 |
# utils.py
|
2 |
-
import tempfile
|
3 |
import os
|
4 |
-
import
|
5 |
import json
|
6 |
-
import
|
7 |
-
from datetime import datetime
|
8 |
-
|
9 |
-
def record_audio(duration=5, fs=44100):
|
10 |
-
try:
|
11 |
-
import sounddevice as sd
|
12 |
-
from scipy.io.wavfile import write
|
13 |
-
|
14 |
-
stt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
15 |
-
recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
|
16 |
-
sd.wait()
|
17 |
-
write(stt_file.name, fs, recording)
|
18 |
-
return stt_file.name
|
19 |
-
except ImportError:
|
20 |
-
st.error("Recording not supported in this environment. Please upload an audio file instead.")
|
21 |
-
return None
|
22 |
-
|
23 |
-
def transcribe_audio(audio_path, language="English"):
|
24 |
-
model = whisper.load_model("base")
|
25 |
-
result = model.transcribe(audio_path, language=language.lower())
|
26 |
-
return result['text']
|
27 |
|
28 |
def save_uploaded_audio(uploaded_file, filename):
|
29 |
-
""
|
30 |
-
|
31 |
-
""
|
32 |
-
path = os.path.join("audio", filename)
|
33 |
-
os.makedirs("audio", exist_ok=True)
|
34 |
-
with open(path, "wb") as f:
|
35 |
f.write(uploaded_file.read())
|
36 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
def save_chat_history(
|
39 |
-
"""
|
40 |
-
Save conversation history to a JSON file.
|
41 |
-
"""
|
42 |
os.makedirs("history", exist_ok=True)
|
43 |
-
if filename
|
44 |
-
|
45 |
-
|
46 |
-
with open(
|
47 |
-
json.dump(
|
48 |
return filename
|
49 |
|
50 |
-
def list_chat_histories():
|
51 |
-
"""
|
52 |
-
List all chat history filenames.
|
53 |
-
"""
|
54 |
-
folder = "history"
|
55 |
-
os.makedirs(folder, exist_ok=True)
|
56 |
-
return [f for f in os.listdir(folder) if f.endswith(".json")]
|
57 |
-
|
58 |
def load_chat_history(filename):
|
59 |
-
"""
|
60 |
-
Load a specific chat history file with None check.
|
61 |
-
"""
|
62 |
if not filename:
|
63 |
-
st.warning("No file selected to load history.")
|
64 |
return []
|
65 |
try:
|
66 |
with open(os.path.join("history", filename), "r") as f:
|
67 |
return json.load(f)
|
68 |
-
except
|
69 |
-
st.warning("Selected history file not found.")
|
70 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# utils.py
|
|
|
2 |
import os
|
3 |
+
import uuid
|
4 |
import json
|
5 |
+
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def save_uploaded_audio(uploaded_file, filename):
|
8 |
+
output_path = os.path.join("uploads", filename)
|
9 |
+
os.makedirs("uploads", exist_ok=True)
|
10 |
+
with open(output_path, "wb") as f:
|
|
|
|
|
|
|
11 |
f.write(uploaded_file.read())
|
12 |
+
return output_path
|
13 |
+
|
14 |
+
def get_voice_preset(gender):
|
15 |
+
presets = {
|
16 |
+
"Male": "male-preset-1",
|
17 |
+
"Female": "female-preset-1"
|
18 |
+
}
|
19 |
+
return presets.get(gender, "male-preset-1")
|
20 |
|
21 |
+
def save_chat_history(chat_data, filename=None):
|
|
|
|
|
|
|
22 |
os.makedirs("history", exist_ok=True)
|
23 |
+
if not filename:
|
24 |
+
filename = f"chat_{uuid.uuid4().hex}.json"
|
25 |
+
filepath = os.path.join("history", filename)
|
26 |
+
with open(filepath, "w") as f:
|
27 |
+
json.dump(chat_data, f, indent=2)
|
28 |
return filename
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def load_chat_history(filename):
|
|
|
|
|
|
|
31 |
if not filename:
|
|
|
32 |
return []
|
33 |
try:
|
34 |
with open(os.path.join("history", filename), "r") as f:
|
35 |
return json.load(f)
|
36 |
+
except Exception:
|
|
|
37 |
return []
|
38 |
+
|
39 |
+
def list_chat_histories():
|
40 |
+
os.makedirs("history", exist_ok=True)
|
41 |
+
return [f for f in os.listdir("history") if f.endswith(".json")]
|
42 |
+
|
43 |
+
def clear_all_histories():
|
44 |
+
if os.path.exists("history"):
|
45 |
+
shutil.rmtree("history")
|
46 |
+
os.makedirs("history")
|