peterpeter8585 commited on
Commit
61404fd
Β·
verified Β·
1 Parent(s): bfcd07a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -6
app.py CHANGED
@@ -380,12 +380,12 @@ import json
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
 
@@ -409,9 +409,77 @@ def list_conversations(username):
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)
@@ -488,4 +556,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant") as demo:
488
  if __name__ == "__main__":
489
  demo.launch(server_name="0.0.0.0", server_port=7860)
490
 
491
-
 
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", " ")[:60]
389
  except Exception:
390
  return "μš”μ•½ μ‹€νŒ¨"
391
 
 
409
 
410
  def load_conversation(file):
411
  with open(os.path.join("user_logs", file), encoding="utf-8") as jf:
412
+ return json.load(jf)["history"]
 
413
 
414
+ # ──────────────────────────────
415
+ # βœ… 둜그인 ν›„ μ‚¬μš©μž 정보 κ°€μ Έμ˜€κΈ°
416
+ # ──────────────────────────────
417
+ def get_hf_user(token):
418
+ """HF OAuth ν† ν°μœΌλ‘œ μ‚¬μš©μž 정보 쑰회"""
419
+ try:
420
+ r = requests.get("https://huggingface.co/api/whoami-v2", headers={"Authorization": f"Bearer {token}"})
421
+ if r.status_code == 200:
422
+ data = r.json()
423
+ return data.get("name") or data.get("email") or "unknown_user"
424
+ except Exception:
425
+ pass
426
+ return "guest"
427
+ def chat(message, history, hf_token):
428
+ username = get_hf_user(hf_token) if hf_token else "guest"
429
+ try:
430
+ response = agent.run(message)
431
+ except Exception as e:
432
+ response = f"⚠️ 였λ₯˜: {e}"
433
+ history = history + [(message, response)]
434
+ if username:
435
+ save_conversation(username, history)
436
+ return history, history, "" # μž…λ ₯ μ΄ˆκΈ°ν™”
437
+
438
+ def refresh_list(hf_token):
439
+ username = get_hf_user(hf_token)
440
+ items = list_conversations(username)
441
+ return gr.Dropdown.update(choices=[x[1] for x in items])
442
+
443
+ def load_selected(file):
444
+ return load_conversation(file)
445
+
446
+
447
+ # ──────────────────────────────
448
+ # βœ… Gradio UI with HF Auth
449
+ # ──────────────────────────────
450
+ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (HF Auth)") as demo:
451
+ gr.Markdown("## πŸ€– PIXAL Assistant β€” Hugging Face 계정 기반 λŒ€ν™” μ €μž₯")
452
+
453
+ hf_login = gr.LoginButton("huggingface", label="πŸ”‘ Hugging Face둜 둜그인")
454
+ hf_token = gr.State()
455
+
456
+ @hf_login.click(inputs=None, outputs=hf_token)
457
+ def login(token): # 둜그인 ν›„ token λ°˜ν™˜
458
+ return token
459
+
460
+ with gr.Row():
461
+ with gr.Column(scale=2):
462
+ chatbot = gr.Chatbot(label="PIXAL λŒ€ν™”", height=600, render_markdown=True)
463
+ msg = gr.Textbox(label="λ©”μ‹œμ§€", placeholder="μž…λ ₯ ν›„ Enter λ˜λŠ” 전솑 클릭")
464
+ send = gr.Button("전솑")
465
+ clear = gr.Button("μ΄ˆκΈ°ν™”")
466
+
467
+ msg.submit(chat, [msg, chatbot, hf_token], [chatbot, chatbot, msg])
468
+ send.click(chat, [msg, chatbot, hf_token], [chatbot, chatbot, msg])
469
+ clear.click(lambda: None, None, chatbot, queue=False)
470
+
471
+ with gr.Column(scale=1):
472
+ gr.Markdown("### πŸ’Ύ μ €μž₯된 λŒ€ν™” 기둝")
473
+ convo_files = gr.Dropdown(label="λŒ€ν™” 선택", choices=[])
474
+ refresh_btn = gr.Button("πŸ”„ λͺ©λ‘ μƒˆλ‘œκ³ μΉ¨")
475
+ load_btn = gr.Button("뢈러였기")
476
+
477
+ refresh_btn.click(refresh_list, [hf_token], convo_files)
478
+ load_btn.click(load_selected, [convo_files], chatbot)
479
+
480
+ if __name__ == "__main__":
481
+ demo.launch(server_name="0.0.0.0", server_port=7860)
482
+ '''
483
  def chat(message, history):
484
  try:
485
  response = agent.run(message)
 
556
  if __name__ == "__main__":
557
  demo.launch(server_name="0.0.0.0", server_port=7860)
558
 
559
+ '''