peterpeter8585 commited on
Commit
bfcd07a
Β·
verified Β·
1 Parent(s): 60a7f01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -30
app.py CHANGED
@@ -380,23 +380,18 @@ import json
380
  # βœ… λŒ€ν™” μš”μ•½ ν•¨μˆ˜
381
  # ──────────────────────────────
382
  def summarize_title(history):
383
- """λŒ€ν™” 전체λ₯Ό μš”μ•½ν•΄ 제λͺ© 생성"""
384
- if not history:
385
- return "μƒˆ λŒ€ν™”"
386
- text = "\n".join(f"User: {h[0]}\nAI: {h[1]}" for h in history[-3:])
387
  try:
388
- title = llm._call(f"λ‹€μŒ λŒ€ν™”λ₯Ό ν•œ 쀄 μš”μ•½ 제λͺ©μœΌλ‘œ λ§Œλ“€μ–΄μ€˜:\n{text}")
389
  return title.strip().replace("\n", " ")[:50]
390
  except Exception:
391
  return "μš”μ•½ μ‹€νŒ¨"
392
 
393
- # ──────────────────────────────
394
- # βœ… μ €μž₯된 기둝 관리
395
- # ──────────────────────────────
396
  def save_conversation(username, history):
397
  os.makedirs("user_logs", exist_ok=True)
398
- if not history:
399
- return
400
  title = summarize_title(history)
401
  fname = f"user_logs/{username}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
402
  with open(fname, "w", encoding="utf-8") as f:
@@ -405,17 +400,18 @@ def save_conversation(username, history):
405
  def list_conversations(username):
406
  os.makedirs("user_logs", exist_ok=True)
407
  files = [f for f in os.listdir("user_logs") if f.startswith(username)]
408
- items = []
409
  for f in files:
410
  with open(os.path.join("user_logs", f), encoding="utf-8") as jf:
411
- data = json.load(jf)
412
- items.append((data.get("title", f), f))
413
- return items
414
 
415
  def load_conversation(file):
416
  with open(os.path.join("user_logs", file), encoding="utf-8") as jf:
417
  data = json.load(jf)
418
  return data["history"]
 
419
  def chat(message, history):
420
  try:
421
  response = agent.run(message)
@@ -458,12 +454,12 @@ def chat(message, history):
458
  def load_selected(file):
459
  return load_conversation(file)
460
 
461
- with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHub LLM)") as demo:
 
 
 
462
  gr.Markdown("## πŸ€– PIXAL Assistant β€” LangChain 기반 λ©€ν‹°νˆ΄ μ—μ΄μ „νŠΈ")
463
 
464
- # Hugging Face 둜그인
465
- login = gr.LoginButton()
466
-
467
  with gr.Row():
468
  with gr.Column(scale=2):
469
  chatbot = gr.Chatbot(label="PIXAL λŒ€ν™”", height=600)
@@ -471,8 +467,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHu
471
  send = gr.Button("전솑")
472
  clear = gr.Button("μ΄ˆκΈ°ν™”")
473
 
474
- # hidden state
475
- username = gr.State("")
476
  msg.submit(chat, [msg, chatbot, username], [chatbot, chatbot, msg])
477
  send.click(chat, [msg, chatbot, username], [chatbot, chatbot, msg])
478
  clear.click(lambda: None, None, chatbot, queue=False)
@@ -484,21 +479,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHu
484
  load_btn = gr.Button("뢈러였기")
485
 
486
  def refresh_list(user):
487
- return gr.Dropdown.update(choices=[x[0] for x in list_conversations(user)])
 
488
 
489
  refresh_btn.click(refresh_list, [username], convo_files)
490
  load_btn.click(lambda f: load_conversation(f), [convo_files], chatbot)
491
 
492
- # 둜그인 성곡 μ‹œ μ‚¬μš©μžλͺ… μ €μž₯
493
- def on_login(user):
494
- return user.username, gr.Dropdown.update(choices=[x[0] for x in list_conversations(user.username)])
495
-
496
- login.login(on_login, outputs=[username, convo_files])
497
-
498
  if __name__ == "__main__":
499
  demo.launch(server_name="0.0.0.0", server_port=7860)
500
 
501
- # ──────────────────────────────
502
- # βœ… Gradio UI
503
- # ──────────────────────────────
504
 
 
380
  # βœ… λŒ€ν™” μš”μ•½ ν•¨μˆ˜
381
  # ──────────────────────────────
382
  def summarize_title(history):
383
+ """λŒ€ν™” μš”μ•½ 제λͺ©"""
384
+ if not history: return "μƒˆ λŒ€ν™”"
385
+ text = "\n".join(f"User:{h[0]} AI:{h[1]}" for h in history[-3:])
 
386
  try:
387
+ title = llm._call(f"λ‹€μŒ λŒ€ν™”μ˜ 제λͺ©μ„ ν•œ μ€„λ‘œ μš”μ•½ν•΄μ€˜:\n{text}")
388
  return title.strip().replace("\n", " ")[:50]
389
  except Exception:
390
  return "μš”μ•½ μ‹€νŒ¨"
391
 
 
 
 
392
  def save_conversation(username, history):
393
  os.makedirs("user_logs", exist_ok=True)
394
+ if not history: return
 
395
  title = summarize_title(history)
396
  fname = f"user_logs/{username}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
397
  with open(fname, "w", encoding="utf-8") as f:
 
400
  def list_conversations(username):
401
  os.makedirs("user_logs", exist_ok=True)
402
  files = [f for f in os.listdir("user_logs") if f.startswith(username)]
403
+ data = []
404
  for f in files:
405
  with open(os.path.join("user_logs", f), encoding="utf-8") as jf:
406
+ info = json.load(jf)
407
+ data.append((info.get("title", f), f))
408
+ return data
409
 
410
  def load_conversation(file):
411
  with open(os.path.join("user_logs", file), encoding="utf-8") as jf:
412
  data = json.load(jf)
413
  return data["history"]
414
+
415
  def chat(message, history):
416
  try:
417
  response = agent.run(message)
 
454
  def load_selected(file):
455
  return load_conversation(file)
456
 
457
+ # ──────────────────────────────
458
+ # βœ… Gradio UI
459
+ # ──────────────────────────────
460
+ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant") as demo:
461
  gr.Markdown("## πŸ€– PIXAL Assistant β€” LangChain 기반 λ©€ν‹°νˆ΄ μ—μ΄μ „νŠΈ")
462
 
 
 
 
463
  with gr.Row():
464
  with gr.Column(scale=2):
465
  chatbot = gr.Chatbot(label="PIXAL λŒ€ν™”", height=600)
 
467
  send = gr.Button("전솑")
468
  clear = gr.Button("μ΄ˆκΈ°ν™”")
469
 
470
+ username = gr.Textbox(label="Hugging Face μ‚¬μš©μžλͺ…", placeholder="둜그인 λŒ€μ‹  이름 μž…λ ₯", value=os.getenv("HF_USER", "guest"))
 
471
  msg.submit(chat, [msg, chatbot, username], [chatbot, chatbot, msg])
472
  send.click(chat, [msg, chatbot, username], [chatbot, chatbot, msg])
473
  clear.click(lambda: None, None, chatbot, queue=False)
 
479
  load_btn = gr.Button("뢈러였기")
480
 
481
  def refresh_list(user):
482
+ if not user: return gr.Dropdown.update(choices=[])
483
+ return gr.Dropdown.update(choices=[x[1] for x in list_conversations(user)])
484
 
485
  refresh_btn.click(refresh_list, [username], convo_files)
486
  load_btn.click(lambda f: load_conversation(f), [convo_files], chatbot)
487
 
 
 
 
 
 
 
488
  if __name__ == "__main__":
489
  demo.launch(server_name="0.0.0.0", server_port=7860)
490
 
 
 
 
491