Spaces:
Sleeping
Sleeping
File size: 1,414 Bytes
a8f55c9 3441734 e1681aa a8f55c9 e1681aa 9cc018b 20c5eb3 e1681aa a8f55c9 e1681aa f962af2 e1681aa f962af2 e1681aa f962af2 3441734 e1681aa a8f55c9 e1681aa 8105291 f0ba5cd 615751f e1681aa 615751f e1681aa |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# utils.py
import os
import uuid
import json
import shutil
def save_uploaded_audio(uploaded_file, filename):
output_path = os.path.join("uploads", filename)
os.makedirs("uploads", exist_ok=True)
with open(output_path, "wb") as f:
f.write(uploaded_file.read())
return output_path
def get_voice_preset(gender, emotion):
presets = {
("Male", "Happy"): "male-happy-preset",
("Male", "Sad"): "male-sad-preset",
("Female", "Happy"): "female-happy-preset",
("Female", "Sad"): "female-sad-preset",
}
return presets.get((gender, emotion), "male-happy-preset")
def save_chat_history(chat_data, filename=None):
os.makedirs("history", exist_ok=True)
if not filename:
filename = f"chat_{uuid.uuid4().hex}.json"
filepath = os.path.join("history", filename)
with open(filepath, "w") as f:
json.dump(chat_data, f, indent=2)
return filename
def load_chat_history(filename):
if not filename:
return []
try:
with open(os.path.join("history", filename), "r") as f:
return json.load(f)
except Exception:
return []
def list_chat_histories():
os.makedirs("history", exist_ok=True)
return [f for f in os.listdir("history") if f.endswith(".json")]
def clear_all_histories():
if os.path.exists("history"):
shutil.rmtree("history")
os.makedirs("history") |