cloningapp / utils.py
meraj12's picture
Update utils.py
f962af2 verified
# 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")