meraj12 commited on
Commit
f0ba5cd
·
verified ·
1 Parent(s): 566375b

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +10 -15
utils.py CHANGED
@@ -44,25 +44,20 @@ def save_chat_history(history, filename=None):
44
  timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
45
  filename = f"chat_history_{timestamp}.json"
46
  with open(os.path.join("history", filename), "w") as f:
47
- json.dump(history, f, indent=4)
48
  return filename
49
 
50
- def load_chat_history(filename):
51
  """
52
- Load conversation history from a JSON file.
53
  """
54
- filepath = os.path.join("history", filename)
55
- if os.path.exists(filepath):
56
- with open(filepath, "r") as f:
57
- return json.load(f)
58
- else:
59
- st.warning("No chat history found with that filename.")
60
- return []
61
 
62
- def list_chat_histories():
63
  """
64
- List all saved chat history filenames.
65
  """
66
- history_dir = "history"
67
- os.makedirs(history_dir, exist_ok=True)
68
- return sorted([f for f in os.listdir(history_dir) if f.endswith(".json")])
 
44
  timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
45
  filename = f"chat_history_{timestamp}.json"
46
  with open(os.path.join("history", filename), "w") as f:
47
+ json.dump(history, f, indent=4, ensure_ascii=False)
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.
61
  """
62
+ with open(os.path.join("history", filename), "r") as f:
63
+ return json.load(f)