prashantmatlani commited on
Commit
2c0a58f
·
1 Parent(s): 2503583

history display

Browse files
Files changed (1) hide show
  1. storage.py +75 -2
storage.py CHANGED
@@ -10,9 +10,15 @@ import os
10
  from datetime import datetime
11
  from huggingface_hub import HfApi, hf_hub_download
12
 
 
 
13
  REPO_ID = "prashantmatlani/chathistorycoderg"
14
  HISTORY_DIR = "./chathistory"
15
 
 
 
 
 
16
  api = HfApi(token=os.getenv("HF_TOKEN"))
17
 
18
  def get_secret_password():
@@ -23,13 +29,30 @@ def get_secret_password():
23
 
24
  return str(val).strip() if val is not None else ""
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def load_history(user_password=""):
27
  """Retrieves list of chat IDs from the Hub ONLY if the password matches the secret."""
28
  target_password = get_secret_password()
29
  clean_input = str(user_password).strip()
30
 
31
  # ====================================================================
32
- # 🔐 YOUR NEW IF / ELSE SECURITY CONFIGURATION
33
  # ====================================================================
34
 
35
  # Condition 1: If the password field is left blank OR if it doesn't match the secret...
@@ -65,4 +88,54 @@ def load_history(user_password=""):
65
  print(f"[!!] Critical Storage Interface Breakdown: {fallback_error}")
66
  return []
67
 
68
- # Keep your existing save_chat and get_chat_content below unchanged...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from datetime import datetime
11
  from huggingface_hub import HfApi, hf_hub_download
12
 
13
+
14
+ # --- CONFIGURATION ---
15
  REPO_ID = "prashantmatlani/chathistorycoderg"
16
  HISTORY_DIR = "./chathistory"
17
 
18
+ # Internal Session Authentication Toggle
19
+ _SESSION_UNLOCKED = False
20
+
21
+ # Initialize the API with your token
22
  api = HfApi(token=os.getenv("HF_TOKEN"))
23
 
24
  def get_secret_password():
 
29
 
30
  return str(val).strip() if val is not None else ""
31
 
32
+ def verify_and_unlock(user_password_input):
33
+ """Validates input credentials and flips the internal tracking state variable."""
34
+ global _SESSION_UNLOCKED
35
+ target_password = get_secret_password()
36
+ clean_input = str(user_password_input).strip()
37
+
38
+ # If no master environment password exists, unlock access automatically
39
+ if not target_password:
40
+ _SESSION_UNLOCKED = True
41
+ return True
42
+
43
+ if clean_input == target_password:
44
+ _SESSION_UNLOCKED = True
45
+ return True
46
+
47
+ return False
48
+
49
  def load_history(user_password=""):
50
  """Retrieves list of chat IDs from the Hub ONLY if the password matches the secret."""
51
  target_password = get_secret_password()
52
  clean_input = str(user_password).strip()
53
 
54
  # ====================================================================
55
+ # 🔐 IF / ELSE SECURITY CONFIGURATION
56
  # ====================================================================
57
 
58
  # Condition 1: If the password field is left blank OR if it doesn't match the secret...
 
88
  print(f"[!!] Critical Storage Interface Breakdown: {fallback_error}")
89
  return []
90
 
91
+ def save_chat(chat_id, history):
92
+ """Saves chat to local subdirectory and syncs to Hugging Face Dataset."""
93
+ if not os.path.exists(HISTORY_DIR):
94
+ os.makedirs(HISTORY_DIR)
95
+
96
+ if not chat_id:
97
+ chat_id = datetime.now().strftime("%m%d%Y_%H%M%S")
98
+
99
+ filename = f"{chat_id}.json"
100
+ local_path = os.path.join(HISTORY_DIR, filename)
101
+
102
+ with open(local_path, "w", encoding="utf-8") as f:
103
+ json.dump(history, f, indent=4)
104
+
105
+ try:
106
+ api.upload_file(
107
+ path_or_fileobj=local_path,
108
+ path_in_repo=f"chats/{filename}",
109
+ repo_id=REPO_ID,
110
+ repo_type="dataset"
111
+ )
112
+ except Exception as e:
113
+ print(f"Cloud Sync Warning: {e}")
114
+
115
+ return chat_id
116
+
117
+ def get_chat_content(chat_id):
118
+ """Loads a specific chat's content from the Hub or local cache."""
119
+ filename = f"chats/{chat_id}.json"
120
+ local_path = os.path.join(HISTORY_DIR, f"{chat_id}.json")
121
+
122
+ try:
123
+ if not os.path.exists(HISTORY_DIR):
124
+ os.makedirs(HISTORY_DIR)
125
+
126
+ downloaded_path = hf_hub_download(
127
+ repo_id=REPO_ID,
128
+ repo_type="dataset",
129
+ filename=filename,
130
+ token=os.getenv("HF_TOKEN")
131
+ )
132
+ with open(downloaded_path, "r", encoding="utf-8") as f:
133
+ return json.load(f)
134
+ except Exception:
135
+ if os.path.exists(local_path):
136
+ with open(local_path, "r", encoding="utf-8") as f:
137
+ return json.load(f)
138
+ return []
139
+
140
+
141
+