nxphi47 commited on
Commit
09793d9
1 Parent(s): 00448bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -24
app.py CHANGED
@@ -67,7 +67,7 @@ if SAVE_LOGS:
67
  GET_LOG_CMD = os.environ.get("GET_LOG_CMD", "").strip()
68
 
69
  print(f'SAVE_LOGS: {SAVE_LOGS} | {LOG_PATH}')
70
- print(f'GET_LOG_CMD: {GET_LOG_CMD}')
71
 
72
  # ! !! Whether to delete the folder, ONLY SET THIS IF YOU WANT TO DELETE SAVED MODEL ON PERSISTENT DISC
73
  DELETE_FOLDER = os.environ.get("DELETE_FOLDER", "")
@@ -885,10 +885,12 @@ def _setup_events(self) -> None:
885
 
886
  def update_time(c_time, chatbot_state):
887
  # if chatbot_state is empty, register a new conversaion with the current timestamp
888
- assert len(chatbot_state) > 0, f'empty chatbot state'
889
- if len(chatbot_state) == 1:
890
- assert chatbot_state[-1][-1] is None, f'invalid [[message, None]] , got {chatbot_state}'
891
  return gr.Number(value=time.time(), label='current_time', visible=False), chatbot_state
 
 
 
892
  else:
893
  return c_time, chatbot_state
894
 
@@ -985,10 +987,18 @@ def _setup_events(self) -> None:
985
  # )
986
 
987
 
 
 
 
 
 
 
 
 
988
  # replace
989
  gr.ChatInterface._setup_stop_events = _setup_stop_events
990
  gr.ChatInterface._setup_events = _setup_events
991
-
992
 
993
 
994
  @document()
@@ -1218,7 +1228,7 @@ def chat_response_stream_multiturn(
1218
 
1219
  message = message.strip()
1220
 
1221
- if message.strip() == GET_LOG_CMD:
1222
  print_log_file()
1223
  yield "Finish printed log. Please clear the chatbox now."
1224
  return
@@ -1313,15 +1323,36 @@ def format_conversation(history):
1313
  return _str
1314
 
1315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1316
  def maybe_upload_to_dataset():
 
1317
  global LOG_FILE, DATA_SET_REPO_PATH, SAVE_LOGS
1318
  if SAVE_LOGS and os.path.exists(LOG_PATH) and DATA_SET_REPO_PATH != "":
1319
- with open(LOG_PATH, 'r', encoding='utf-8') as f:
1320
- convos = {}
1321
- for l in f:
1322
- if l:
1323
- item = json.loads(l)
1324
- convos[item['key']] = item
1325
  AGG_LOG_PATH = LOG_PATH + ".agg.json"
1326
  with open(AGG_LOG_PATH, 'w', encoding='utf-8') as fo:
1327
  json.dump(convos, fo, indent=4, ensure_ascii=False)
@@ -1345,15 +1376,12 @@ def print_log_file():
1345
  global LOG_FILE, LOG_PATH
1346
  if SAVE_LOGS and os.path.exists(LOG_PATH):
1347
  with open(LOG_PATH, 'r', encoding='utf-8') as f:
1348
- convos = {}
1349
- for l in f:
1350
- if l:
1351
- item = json.loads(l)
1352
- convos[item['key']] = item
1353
  print(f'Printing log from {LOG_PATH}')
1354
- for k, v in convos.items():
 
1355
  history = v.pop('history')
1356
- print(f'######--{v}--##')
1357
  _str = format_conversation(history)
1358
  print(_str)
1359
  maybe_upload_to_dataset()
@@ -1818,8 +1846,8 @@ def launch():
1818
  if ENABLE_AGREE_POPUP:
1819
  demo.load(None, None, None, _js=AGREE_POP_SCRIPTS)
1820
 
1821
- demo.queue()
1822
- demo.launch(server_port=PORT)
1823
  else:
1824
  descriptions = model_desc
1825
  if DISPLAY_MODEL_PATH:
@@ -1863,8 +1891,8 @@ def launch():
1863
  demo.load(None, None, None, _js=AGREE_POP_SCRIPTS)
1864
 
1865
 
1866
- demo.queue()
1867
- demo.launch(server_port=PORT)
1868
 
1869
 
1870
  def main():
@@ -1874,4 +1902,3 @@ def main():
1874
 
1875
  if __name__ == "__main__":
1876
  main()
1877
-
 
67
  GET_LOG_CMD = os.environ.get("GET_LOG_CMD", "").strip()
68
 
69
  print(f'SAVE_LOGS: {SAVE_LOGS} | {LOG_PATH}')
70
+ # print(f'GET_LOG_CMD: {GET_LOG_CMD}')
71
 
72
  # ! !! Whether to delete the folder, ONLY SET THIS IF YOU WANT TO DELETE SAVED MODEL ON PERSISTENT DISC
73
  DELETE_FOLDER = os.environ.get("DELETE_FOLDER", "")
 
885
 
886
  def update_time(c_time, chatbot_state):
887
  # if chatbot_state is empty, register a new conversaion with the current timestamp
888
+ # assert len(chatbot_state) > 0, f'empty chatbot state'
889
+ if len(chatbot_state) <= 1:
 
890
  return gr.Number(value=time.time(), label='current_time', visible=False), chatbot_state
891
+ # elif len(chatbot_state) == 1:
892
+ # # assert chatbot_state[-1][-1] is None, f'invalid [[message, None]] , got {chatbot_state}'
893
+ # return gr.Number(value=time.time(), label='current_time', visible=False), chatbot_state
894
  else:
895
  return c_time, chatbot_state
896
 
 
987
  # )
988
 
989
 
990
+ def _display_input(
991
+ self, message: str, history: list[list[str | None]]
992
+ ) -> tuple[list[list[str | None]], list[list[str | None]]]:
993
+ if message is not None and message.strip() != "":
994
+ history.append([message, None])
995
+ return history, history
996
+
997
+
998
  # replace
999
  gr.ChatInterface._setup_stop_events = _setup_stop_events
1000
  gr.ChatInterface._setup_events = _setup_events
1001
+ gr.ChatInterface._display_input = _display_input
1002
 
1003
 
1004
  @document()
 
1228
 
1229
  message = message.strip()
1230
 
1231
+ if GET_LOG_CMD != "" and message.strip() == GET_LOG_CMD:
1232
  print_log_file()
1233
  yield "Finish printed log. Please clear the chatbox now."
1234
  return
 
1323
  return _str
1324
 
1325
 
1326
+ def aggregate_convos():
1327
+ from datetime import datetime
1328
+ global LOG_FILE, DATA_SET_REPO_PATH, SAVE_LOGS
1329
+ assert os.path.exists(LOG_PATH), f'{LOG_PATH} not found'
1330
+ convos = None
1331
+ irregular_count = 1
1332
+ with open(LOG_PATH, 'r', encoding='utf-8') as f:
1333
+ convos = {}
1334
+ for i, l in enumerate(f):
1335
+ if l:
1336
+ item = json.loads(l)
1337
+ key = item['key']
1338
+ try:
1339
+ key = float(key)
1340
+ except Exception as e:
1341
+ key = -1
1342
+ if key > 0.0:
1343
+ item_key = datetime.fromtimestamp(key).strftime("%Y-%m-%d %H:%M:%S")
1344
+ else:
1345
+ key = item_key = f'e{irregular_count}'
1346
+ irregular_count += 1
1347
+ item['key'] = item_key
1348
+ convos[key] = item
1349
+ return convos
1350
+
1351
  def maybe_upload_to_dataset():
1352
+ from datetime import datetime
1353
  global LOG_FILE, DATA_SET_REPO_PATH, SAVE_LOGS
1354
  if SAVE_LOGS and os.path.exists(LOG_PATH) and DATA_SET_REPO_PATH != "":
1355
+ convos = aggregate_convos()
 
 
 
 
 
1356
  AGG_LOG_PATH = LOG_PATH + ".agg.json"
1357
  with open(AGG_LOG_PATH, 'w', encoding='utf-8') as fo:
1358
  json.dump(convos, fo, indent=4, ensure_ascii=False)
 
1376
  global LOG_FILE, LOG_PATH
1377
  if SAVE_LOGS and os.path.exists(LOG_PATH):
1378
  with open(LOG_PATH, 'r', encoding='utf-8') as f:
1379
+ convos = aggregate_convos()
 
 
 
 
1380
  print(f'Printing log from {LOG_PATH}')
1381
+ items = list(convos.items())
1382
+ for k, v in items[-10:]:
1383
  history = v.pop('history')
1384
+ print(f'######--{v}--#####')
1385
  _str = format_conversation(history)
1386
  print(_str)
1387
  maybe_upload_to_dataset()
 
1846
  if ENABLE_AGREE_POPUP:
1847
  demo.load(None, None, None, _js=AGREE_POP_SCRIPTS)
1848
 
1849
+ demo.queue(api_open=False)
1850
+ demo.launch(server_port=PORT, show_api=False)
1851
  else:
1852
  descriptions = model_desc
1853
  if DISPLAY_MODEL_PATH:
 
1891
  demo.load(None, None, None, _js=AGREE_POP_SCRIPTS)
1892
 
1893
 
1894
+ demo.queue(api_open=False)
1895
+ demo.launch(server_port=PORT, show_api=False)
1896
 
1897
 
1898
  def main():
 
1902
 
1903
  if __name__ == "__main__":
1904
  main()