Spaces:
PhilSpiel
/
Sleeping

PhilSpiel commited on
Commit
bc27619
·
verified ·
1 Parent(s): 979a704

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -7
app.py CHANGED
@@ -13,7 +13,6 @@ coach_name_upper = os.getenv("COACH_NAME_UPPER")
13
  coach_name_long = os.getenv("COACH_NAME_LONG")
14
  sys_prompt_new = os.getenv("PROMPT_NEW")
15
  sys_prompt_hist = os.getenv("PROMPT_HIST")
16
- pics_prompt = os.getenv("PROMPT_PICS")
17
  theme=os.getenv("THEME")
18
  ################# End PERSONA-SPECIFIC VALUES ######################
19
 
@@ -27,9 +26,12 @@ openai_model = os.getenv("OPENAI_MODEL")
27
  # define file name prefix
28
  prefix = os.getenv("PREFIX") # "/data/" if in HF or "data/" if local
29
  tx = os.getenv("TX")
 
30
 
31
  # Assistants API prompts
32
  bullet_instructions = os.getenv("PROMPT_BULLET")
 
 
33
 
34
  # Get dateTime string to build a filename reflecting the UserID + Timestamp
35
  dt = datetime.now()
@@ -73,9 +75,7 @@ def get_system_prompt(user_id):
73
 
74
  # System prompt for returning users includes past summary
75
  return [
76
- {"role": "system", "content": sys_prompt_hist + past_summary +
77
- "If the user asks how to share or upload pictures, refer them to the 'DatingFinesse.com' website for instructions. The user may have shared pictures with you. Each picture will be separated by 'IMAGE - Date/Time:'. If the user has shared images, most recently shared will be listed last and they would be summarized here: " +
78
- user_pics_content + pics_prompt}
79
  ]
80
  else:
81
  # System prompt for new users
@@ -97,7 +97,6 @@ def on_timeout(user_id):
97
  global last_file_mod_time
98
  current_mod_time = get_user_hist_file_mod_time(user_id)
99
  if current_mod_time is not None and (last_file_mod_time is None or current_mod_time > last_file_mod_time):
100
- #print(f"User with ID {user_id} has been inactive, but the history file was updated. Running timeout script.")
101
  last_file_mod_time = current_mod_time
102
  # Construct the filename for the user's bullet file
103
  user_summ_file_bullet = f"{prefix}{coach_code}-{user_id.upper().replace(' ', '')}-bullet.txt"
@@ -206,12 +205,32 @@ inactivity_thread = threading.Thread(target=check_for_inactivity)
206
  inactivity_thread.daemon = True
207
  inactivity_thread.start()
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  ############### CHAT ###################
210
  def predict(user_input, history):
211
  max_length = 2000
212
  if len(user_input) > max_length:
213
  user_input = ""
214
-
215
  global last_interaction_time, user_id, last_file_mod_time
216
  # ... [other global variables that are used]
217
 
@@ -219,8 +238,81 @@ def predict(user_input, history):
219
  last_interaction_time = time.time()
220
 
221
  user_hist_file = f"{prefix}{coach_code}-{user_id}.txt"
 
222
  pics_file_path = f"{prefix}{coach_code}-{user_id}-pics.txt"
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  if user_id == "NOTLOGGEDIN":
225
  raise gr.Error(f"You must be LOGGED IN to Chat. Refresh the page and try again.")
226
  if user_id == "":
@@ -306,7 +398,7 @@ def get_user_summary(name):
306
  def admin():
307
  if user_id == "POIPOIPOI":
308
  # Set the directory path
309
- directory_path = "" # different on huggingface
310
 
311
  # Get a list of all files in the directory
312
  file_list = os.listdir(directory_path)
@@ -330,7 +422,9 @@ with gr.Blocks(theme) as demo:
330
 
331
  def submit_name(name, password):
332
  name = name + "7777"
 
333
  pwd = password
 
334
  if name != pwd:
335
  name = "NOTLOGGEDIN"
336
  raise gr.Error(f"You must be LOGGED IN to Chat. Refresh the page, log in, and try again.")
 
13
  coach_name_long = os.getenv("COACH_NAME_LONG")
14
  sys_prompt_new = os.getenv("PROMPT_NEW")
15
  sys_prompt_hist = os.getenv("PROMPT_HIST")
 
16
  theme=os.getenv("THEME")
17
  ################# End PERSONA-SPECIFIC VALUES ######################
18
 
 
26
  # define file name prefix
27
  prefix = os.getenv("PREFIX") # "/data/" if in HF or "data/" if local
28
  tx = os.getenv("TX")
29
+ overwrite = os.getenv("OVERWRITE")
30
 
31
  # Assistants API prompts
32
  bullet_instructions = os.getenv("PROMPT_BULLET")
33
+ ##################################################################
34
+ ########################### END OF HF SECRETS ####################
35
 
36
  # Get dateTime string to build a filename reflecting the UserID + Timestamp
37
  dt = datetime.now()
 
75
 
76
  # System prompt for returning users includes past summary
77
  return [
78
+ {"role": "system", "content": sys_prompt_hist + past_summary + "The user may have shared pictures with you. If so, each picture will have a title and detailed description and would be summarized here: " + user_pics_content}
 
 
79
  ]
80
  else:
81
  # System prompt for new users
 
97
  global last_file_mod_time
98
  current_mod_time = get_user_hist_file_mod_time(user_id)
99
  if current_mod_time is not None and (last_file_mod_time is None or current_mod_time > last_file_mod_time):
 
100
  last_file_mod_time = current_mod_time
101
  # Construct the filename for the user's bullet file
102
  user_summ_file_bullet = f"{prefix}{coach_code}-{user_id.upper().replace(' ', '')}-bullet.txt"
 
205
  inactivity_thread.daemon = True
206
  inactivity_thread.start()
207
 
208
+ #### UPDATE FILE
209
+ def update_file(file_path, content):
210
+ try:
211
+ # Create a backup of the existing file
212
+ backup_file_path = file_path + "-OLD"
213
+ if os.path.exists(file_path):
214
+ # If the backup file already exists, delete it
215
+ if os.path.exists(backup_file_path):
216
+ os.remove(backup_file_path)
217
+ # Rename the original file to the backup file
218
+ os.rename(file_path, backup_file_path)
219
+
220
+ # Write the new content to the file
221
+ with open(file_path, "w", encoding="UTF-8") as file:
222
+ file.write(content)
223
+
224
+ return "File updated successfully. Backup created as " + backup_file_path
225
+ except Exception as e:
226
+ return f"Error updating file: {str(e)}"
227
+
228
  ############### CHAT ###################
229
  def predict(user_input, history):
230
  max_length = 2000
231
  if len(user_input) > max_length:
232
  user_input = ""
233
+
234
  global last_interaction_time, user_id, last_file_mod_time
235
  # ... [other global variables that are used]
236
 
 
238
  last_interaction_time = time.time()
239
 
240
  user_hist_file = f"{prefix}{coach_code}-{user_id}.txt"
241
+ user_summ_file_bullet = f"{prefix}{coach_code}-{user_id.upper().replace(' ', '')}-bullet.txt"
242
  pics_file_path = f"{prefix}{coach_code}-{user_id}-pics.txt"
243
 
244
+ ####### READ #######
245
+ if user_input == tx + coach_code:
246
+ try:
247
+ # Prepare the transcript for the Textbox output
248
+ if os.path.exists(user_summ_file_bullet):
249
+ with open(user_summ_file_bullet, "r", encoding="UTF-8") as file:
250
+ output = file.read()
251
+ yield output
252
+ return
253
+ except FileNotFoundError:
254
+ yield "File '" + user_summ_file_bullet + "' not found."
255
+ return
256
+
257
+ if user_input == tx + coach_code + "hist":
258
+ try:
259
+ # Prepare the transcript for the Textbox output
260
+ if os.path.exists(user_hist_file):
261
+ with open(user_hist_file, "r", encoding="UTF-8") as file:
262
+ output = file.read()
263
+ yield output
264
+ return
265
+ except FileNotFoundError:
266
+ yield "File '" + user_hist_file + "' not found."
267
+ return
268
+
269
+ if user_input == tx + coach_code + "pics":
270
+ try:
271
+ # Prepare the transcript for the Textbox output
272
+ if os.path.exists(pics_file_path):
273
+ with open(pics_file_path, "r", encoding="UTF-8") as file:
274
+ output = file.read()
275
+ yield output
276
+ return
277
+ except FileNotFoundError:
278
+ yield "File '" + pics_file_path + "' not found."
279
+ return
280
+
281
+ ####### UPDATE #######
282
+ if user_input.startswith(overwrite + "bull:"):
283
+ try:
284
+ file_path, content = user_input[12:].split("|", 1)
285
+ file_path = user_summ_file_bullet.strip()
286
+ content = content.strip()
287
+
288
+ if file_path and content:
289
+ result = update_file(file_path, content)
290
+ yield result
291
+ return
292
+ else:
293
+ yield "Invalid format. Please use 'update_file: <file_path> | <content>'."
294
+ return
295
+ except Exception as e:
296
+ yield f"Error updating file: {str(e)}"
297
+ return
298
+
299
+ if user_input.startswith(overwrite + "pics:"):
300
+ try:
301
+ file_path, content = user_input[12:].split("|", 1)
302
+ file_path = pics_file_path.strip()
303
+ content = content.strip()
304
+
305
+ if file_path and content:
306
+ result = update_file(file_path, content)
307
+ yield result
308
+ return
309
+ else:
310
+ yield "Invalid format. Please use 'update_file: <file_path> | <content>'."
311
+ return
312
+ except Exception as e:
313
+ yield f"Error updating file: {str(e)}"
314
+ return
315
+
316
  if user_id == "NOTLOGGEDIN":
317
  raise gr.Error(f"You must be LOGGED IN to Chat. Refresh the page and try again.")
318
  if user_id == "":
 
398
  def admin():
399
  if user_id == "POIPOIPOI":
400
  # Set the directory path
401
+ directory_path = "data" # different on huggingface
402
 
403
  # Get a list of all files in the directory
404
  file_list = os.listdir(directory_path)
 
422
 
423
  def submit_name(name, password):
424
  name = name + "7777"
425
+ name = name.upper()
426
  pwd = password
427
+ pwd = pwd.upper()
428
  if name != pwd:
429
  name = "NOTLOGGEDIN"
430
  raise gr.Error(f"You must be LOGGED IN to Chat. Refresh the page, log in, and try again.")