Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
| 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[
|
|
|
|
| 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,
|
| 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__":
|