peterpeter8585 commited on
Commit
f5fffed
·
verified ·
1 Parent(s): 54880c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -426,7 +426,31 @@ def get_hf_user(token):
426
  def chat(message, history, hf_token):
427
  username = get_hf_user(hf_token) if hf_token else "guest"
428
  try:
429
- response = agent.run(message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
  except Exception as e:
431
  response = f"⚠️ 오류: {e}"
432
  history = history + [(message, response)]
@@ -434,11 +458,10 @@ def chat(message, history, hf_token):
434
  save_conversation(username, history)
435
  return history, history, "" # 입력 초기화
436
 
437
- def refresh_list(hf_token):
438
- username = get_hf_user(hf_token)
439
- items = list_conversations(username)
440
- return gr.Dropdown.update(choices=[x[1] for x in items])
441
-
442
  def load_selected(file):
443
  return load_conversation(file)
444
 
@@ -473,7 +496,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (HF Auth)") as dem
473
  refresh_btn = gr.Button("🔄 목록 새로고침")
474
  load_btn = gr.Button("불러오기")
475
 
476
- refresh_btn.click(refresh_list, [hf_token], convo_files)
477
  load_btn.click(load_selected, [convo_files], chatbot)
478
 
479
  if __name__ == "__main__":
 
426
  def chat(message, history, hf_token):
427
  username = get_hf_user(hf_token) if hf_token else "guest"
428
  try:
429
+ response = agent.invoke(message)
430
+ if isinstance(response, dict):
431
+ if "action_input" in response:
432
+ response = response["action_input"]
433
+ elif "output" in response:
434
+ response = response["output"]
435
+ elif "text" in response:
436
+ response = response["text"]
437
+ else:
438
+ response = str(response)
439
+ elif isinstance(response, str):
440
+ # "Final Answer"가 포함된 문자열이면 그 부분만 추출
441
+ if '"action_input":' in response:
442
+ import re, json
443
+ match = re.search(r'["\']action_input["\']\s*:\s*["\'](.*?)["\']', response)
444
+ if match:
445
+ response = match.group(1)
446
+ elif "Final Answer" in response:
447
+ # {"action": "Final Answer", "action_input": "..."} 형식일 때
448
+ try:
449
+ data = json.loads(response)
450
+ if isinstance(data, dict) and "action_input" in data:
451
+ response = data["action_input"]
452
+ except Exception:
453
+ response = response.replace("Final Answer", "").strip()
454
  except Exception as e:
455
  response = f"⚠️ 오류: {e}"
456
  history = history + [(message, response)]
 
458
  save_conversation(username, history)
459
  return history, history, "" # 입력 초기화
460
 
461
+ def refresh_conversation_list():
462
+ files = sorted(os.listdir("user_logs"), reverse=True)
463
+ titles = [f.replace(".json", "") for f in files]
464
+ return gr.update(choices=titles, value=titles[-1] if titles else None)
 
465
  def load_selected(file):
466
  return load_conversation(file)
467
 
 
496
  refresh_btn = gr.Button("🔄 목록 새로고침")
497
  load_btn = gr.Button("불러오기")
498
 
499
+ refresh_btn.click(refresh_conversation_list, [hf_token], convo_files)
500
  load_btn.click(load_selected, [convo_files], chatbot)
501
 
502
  if __name__ == "__main__":