peterpeter8585 commited on
Commit
295f49c
ยท
verified ยท
1 Parent(s): f5fffed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -4
app.py CHANGED
@@ -423,6 +423,45 @@ def get_hf_user(token):
423
  except Exception:
424
  pass
425
  return "guest"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
  def chat(message, history, hf_token):
427
  username = get_hf_user(hf_token) if hf_token else "guest"
428
  try:
@@ -457,11 +496,15 @@ def chat(message, history, hf_token):
457
  if username:
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,7 +539,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (HF Auth)") as dem
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__":
 
423
  except Exception:
424
  pass
425
  return "guest"
426
+ import re, json
427
+
428
+ def chat(message, history, system_prompt):
429
+ try:
430
+ # ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€ ํฌํ•จ ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
431
+ prompt = f"{system_prompt}\n\n์‚ฌ์šฉ์ž: {message}" if system_prompt else message
432
+
433
+ raw_response = agent.invoke(prompt)
434
+
435
+ # ๋ฌธ์ž์—ด ๋ณ€ํ™˜
436
+ text = str(raw_response)
437
+
438
+ # โœ… JSON ๋ธ”๋ก์ด ์žˆ์„ ๊ฒฝ์šฐ ํŒŒ์‹ฑ
439
+ match = re.search(r"\{.*\}", text, re.DOTALL)
440
+ if match:
441
+ try:
442
+ obj = json.loads(match.group(0))
443
+ # "action_input" ๋˜๋Š” "Final Answer" ํ‚ค ์šฐ์„  ์ถœ๋ ฅ
444
+ if "Final Answer" in obj:
445
+ output = obj["Final Answer"]
446
+ else:
447
+ # ํ˜น์‹œ "output"์ด๋‚˜ "content" ๊ฐ™์€ ํ‚ค๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ๊ฑธ ์‚ฌ์šฉ
448
+ for k in ("output", "content", "answer"):
449
+ if k in obj:
450
+ output = obj[k]
451
+ break
452
+ else:
453
+ output = text
454
+ except Exception:
455
+ output = text
456
+ else:
457
+ output = text
458
+
459
+ except Exception as e:
460
+ output = f"โš ๏ธ ์˜ค๋ฅ˜: {e}"
461
+
462
+ history = history + [(message, output)]
463
+ return history, history, ""
464
+ '''
465
  def chat(message, history, hf_token):
466
  username = get_hf_user(hf_token) if hf_token else "guest"
467
  try:
 
496
  if username:
497
  save_conversation(username, history)
498
  return history, history, "" # ์ž…๋ ฅ ์ดˆ๊ธฐํ™”
499
+ '''
500
+ # ์˜ˆ: hf_token (ํ˜น์€ username) ์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋„๋ก ๋ณ€๊ฒฝ
501
+ def refresh_conversation_list(_=None):
502
+ if not os.path.exists("user_logs"):
503
+ return gr.update(choices=[], value=None)
504
  files = sorted(os.listdir("user_logs"), reverse=True)
505
  titles = [f.replace(".json", "") for f in files]
506
+ return gr.update(choices=titles, value=titles[0] if titles else None)
507
+
508
  def load_selected(file):
509
  return load_conversation(file)
510
 
 
539
  refresh_btn = gr.Button("๐Ÿ”„ ๋ชฉ๋ก ์ƒˆ๋กœ๊ณ ์นจ")
540
  load_btn = gr.Button("๋ถˆ๋Ÿฌ์˜ค๊ธฐ")
541
 
542
+ refresh_btn.click(refresh_conversation_list, None, convo_files)
543
  load_btn.click(load_selected, [convo_files], chatbot)
544
 
545
  if __name__ == "__main__":