meraj12 commited on
Commit
8105291
·
verified ·
1 Parent(s): d93accf

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +27 -2
utils.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import whisper
5
  import json
6
  import streamlit as st
 
7
 
8
  def record_audio(duration=5, fs=44100):
9
  try:
@@ -34,10 +35,34 @@ def save_uploaded_audio(uploaded_file, filename):
34
  f.write(uploaded_file.read())
35
  return path
36
 
37
- def save_chat_history(history, filename="chat_history.json"):
38
  """
39
  Save conversation history to a JSON file.
40
  """
41
  os.makedirs("history", exist_ok=True)
 
 
 
42
  with open(os.path.join("history", filename), "w") as f:
43
- json.dump(history, f, indent=4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import whisper
5
  import json
6
  import streamlit as st
7
+ from datetime import datetime
8
 
9
  def record_audio(duration=5, fs=44100):
10
  try:
 
35
  f.write(uploaded_file.read())
36
  return path
37
 
38
+ def save_chat_history(history, filename=None):
39
  """
40
  Save conversation history to a JSON file.
41
  """
42
  os.makedirs("history", exist_ok=True)
43
+ if filename is 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")])