Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -380,23 +380,18 @@ import json
|
|
| 380 |
# β
λν μμ½ ν¨μ
|
| 381 |
# ββββββββββββββββββββββββββββββ
|
| 382 |
def summarize_title(history):
|
| 383 |
-
"""λν
|
| 384 |
-
if not history:
|
| 385 |
-
|
| 386 |
-
text = "\n".join(f"User: {h[0]}\nAI: {h[1]}" for h in history[-3:])
|
| 387 |
try:
|
| 388 |
-
title = llm._call(f"λ€μ
|
| 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 |
-
|
| 409 |
for f in files:
|
| 410 |
with open(os.path.join("user_logs", f), encoding="utf-8") as jf:
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
return
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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=[
|
|
|
|
| 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 |
|