peterpeter8585 commited on
Commit
8f6e2f3
Β·
verified Β·
1 Parent(s): cc8a692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -55
app.py CHANGED
@@ -396,32 +396,66 @@ os.chdir(os.path.dirname(os.path.abspath(__file__)))
396
  os.makedirs("user_logs", exist_ok=True)
397
 
398
  # --- λŒ€ν™” 기둝 μ €μž₯/뢈러였기 ---
399
- def save_conversation(username, history, conv_name="current"):
400
- """κ³„μ •λ³„λ‘œ λŒ€ν™”κΈ°λ‘μ„ pickle둜 μ‹€μ‹œκ°„ μ €μž₯"""
401
- os.makedirs(f"user_logs/{username}", exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  title = summarize_title(history)
403
- fname = f"user_logs/{username}/{conv_name}.pkl"
 
 
 
 
 
404
  with open(fname, "wb") as f:
405
- pickle.dump({"title": title, "history": history}, f)
406
 
407
- def load_conversation(username, conv_name="current"):
408
- path = f"user_logs/{username}/{conv_name}.pkl"
409
- if not os.path.exists(path):
 
 
 
 
410
  return []
411
- with open(path, "rb") as f:
412
  data = pickle.load(f)
413
- return data.get("history", [])
414
-
415
- def list_conversations(username):
416
- os.makedirs(f"user_logs/{username}", exist_ok=True)
417
- files = [f for f in os.listdir(f"user_logs/{username}") if f.endswith(".pkl")]
418
- titles = []
419
- for f in files:
420
- with open(f"user_logs/{username}/" + f, "rb") as fp:
421
- data = pickle.load(fp)
422
- titles.append((data.get("title", f), f))
423
- return titles
424
-
 
 
 
 
 
 
 
 
 
 
 
 
425
  # --- chat ν•¨μˆ˜ μˆ˜μ • ---
426
  def chat(message, history, username="guest", conv_name="current"):
427
  try:
@@ -452,13 +486,7 @@ def chat(message, history, username="guest", conv_name="current"):
452
  return history, history, ""
453
 
454
  # --- 뢈러였기 λ²„νŠΌ ν•¨μˆ˜ ---
455
- def load_selected(username, file):
456
- path = f"user_logs/{username}/{file}"
457
- if not os.path.exists(path):
458
- return []
459
- with open(path, "rb") as f:
460
- data = pickle.load(f)
461
- return data.get("history", [])
462
  # ──────────────────────────────
463
  # βœ… 둜그인 ν›„ μ‚¬μš©μž 정보 κ°€μ Έμ˜€κΈ°
464
  # ──────────────────────────────
@@ -512,32 +540,6 @@ def chat(message, history, hf_token):
512
  return history, history, "" # μž…λ ₯ μ΄ˆκΈ°ν™”
513
  '''
514
  # 예: hf_token (ν˜Ήμ€ username) 을 μž…λ ₯으둜 받도둝 λ³€κ²½
515
- def refresh_conversation_list(username="guest"):
516
- """계정별 λŒ€ν™” λͺ©λ‘μ„ μƒˆλ‘œκ³ μΉ¨ (Gradio Dropdown μ—…λ°μ΄νŠΈμš©)"""
517
- base_dir = os.path.join("user_logs", username)
518
- os.makedirs(base_dir, exist_ok=True)
519
-
520
- files = sorted(
521
- [f for f in os.listdir(base_dir) if f.endswith(".pkl")],
522
- reverse=True
523
- )
524
-
525
- # 제λͺ© λͺ©λ‘ λ§Œλ“€κΈ°
526
- titles = []
527
- for f in files:
528
- try:
529
- with open(os.path.join(base_dir, f), "rb") as fp:
530
- data = pickle.load(fp)
531
- title = data.get("title", f.replace(".pkl", ""))
532
- except Exception:
533
- title = f.replace(".pkl", "")
534
- titles.append(title)
535
-
536
- # Dropdown μ—…λ°μ΄νŠΈ
537
- if titles:
538
- return gr.update(choices=titles, value=titles[0])
539
- else:
540
- return gr.update(choices=[], value=None)
541
 
542
 
543
 
@@ -573,7 +575,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (HF Auth)") as dem
573
  load_btn = gr.Button("뢈러였���")
574
 
575
  refresh_btn.click(refresh_conversation_list, None, convo_files)
576
- load_btn.click(load_selected, [convo_files], chatbot)
577
 
578
  if __name__ == "__main__":
579
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
396
  os.makedirs("user_logs", exist_ok=True)
397
 
398
  # --- λŒ€ν™” 기둝 μ €μž₯/뢈러였기 ---
399
+ def save_conversation(username, history):
400
+ """둜그인된 μ‚¬μš©μžλŠ” μ—¬λŸ¬ λŒ€ν™”λ₯Ό ν•˜λ‚˜μ˜ pickle νŒŒμΌμ— μ €μž₯"""
401
+ if not username or username.lower() == "guest":
402
+ return
403
+
404
+ fname = f"{username}.pkl"
405
+ # κΈ°μ‘΄ 데이터 뢈러였기
406
+ data = {}
407
+ if os.path.exists(fname):
408
+ try:
409
+ with open(fname, "rb") as f:
410
+ data = pickle.load(f)
411
+ except Exception:
412
+ data = {}
413
+
414
+ # μƒˆλ‘œμš΄ λŒ€ν™” μš”μ•½ 제λͺ©κ³Ό ν•¨κ»˜ μΆ”κ°€
415
  title = summarize_title(history)
416
+ data[title] = {
417
+ "title": title,
418
+ "updated": datetime.datetime.now().isoformat(),
419
+ "history": history
420
+ }
421
+
422
  with open(fname, "wb") as f:
423
+ pickle.dump(data, f)
424
 
425
+ # --- λŒ€ν™” 뢈러였기 ---
426
+ def load_conversation(username, conv_title=None):
427
+ """νŠΉμ • μ‚¬μš©μžμ™€ λŒ€ν™” 제λͺ©μœΌλ‘œ 뢈러였기"""
428
+ if not username or username.lower() == "guest":
429
+ return []
430
+ fname = f"{username}.pkl"
431
+ if not os.path.exists(fname):
432
  return []
433
+ with open(fname, "rb") as f:
434
  data = pickle.load(f)
435
+ if conv_title and conv_title in data:
436
+ return data[conv_title]["history"]
437
+ elif data:
438
+ # κ°€μž₯ 졜근 λŒ€ν™” λ°˜ν™˜
439
+ latest = max(data.values(), key=lambda x: x["updated"])
440
+ return latest["history"]
441
+ return []
442
+
443
+ # --- λŒ€ν™” λͺ©λ‘ κ°±μ‹  ---
444
+ def refresh_conversation_list(username=None):
445
+ """μ‚¬μš©μžλ³„ μ €μž₯된 λŒ€ν™” 제λͺ© λͺ©λ‘μ„ μƒˆλ‘œκ³ μΉ¨"""
446
+ if not username or username.lower() == "guest":
447
+ return gr.update(choices=[], value=None)
448
+ fname = f"{username}.pkl"
449
+ if not os.path.exists(fname):
450
+ return gr.update(choices=[], value=None)
451
+ try:
452
+ with open(fname, "rb") as f:
453
+ data = pickle.load(f)
454
+ titles = sorted(data.keys(), reverse=True)
455
+ return gr.update(choices=titles, value=titles[0] if titles else None)
456
+ except Exception as e:
457
+ print(f"⚠️ λͺ©λ‘ 뢈러였기 였λ₯˜: {e}")
458
+ return gr.update(choices=[], value=None)
459
  # --- chat ν•¨μˆ˜ μˆ˜μ • ---
460
  def chat(message, history, username="guest", conv_name="current"):
461
  try:
 
486
  return history, history, ""
487
 
488
  # --- 뢈러였기 λ²„νŠΌ ν•¨μˆ˜ ---
489
+
 
 
 
 
 
 
490
  # ──────────────────────────────
491
  # βœ… 둜그인 ν›„ μ‚¬μš©μž 정보 κ°€μ Έμ˜€κΈ°
492
  # ──────────────────────────────
 
540
  return history, history, "" # μž…λ ₯ μ΄ˆκΈ°ν™”
541
  '''
542
  # 예: hf_token (ν˜Ήμ€ username) 을 μž…λ ₯으둜 받도둝 λ³€κ²½
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
 
544
 
545
 
 
575
  load_btn = gr.Button("뢈러였���")
576
 
577
  refresh_btn.click(refresh_conversation_list, None, convo_files)
578
+ load_btn.click(load_conversation, [convo_files], chatbot)
579
 
580
  if __name__ == "__main__":
581
  demo.launch(server_name="0.0.0.0", server_port=7860)