peterpeter8585 commited on
Commit
60a7f01
Β·
verified Β·
1 Parent(s): a1a7ddf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -13
app.py CHANGED
@@ -371,10 +371,51 @@ agent=initialize_agent(tools,llm,agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT
371
  #agent = create_structured_chat_agent(llm, tools, prompt)
372
  #agent= AgentExecutor(agent=agent, tools=tools,memory=mem)
373
 
 
 
 
 
374
 
375
  # ──────────────────────────────
376
- # βœ… Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  # ──────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  def chat(message, history):
379
  try:
380
  response = agent.run(message)
@@ -409,21 +450,55 @@ def chat(message, history):
409
  response = f"⚠️ 였λ₯˜: {e}"
410
 
411
  history = history + [(message, response)]
412
- return history, history
 
 
 
 
 
 
413
 
414
  with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHub LLM)") as demo:
415
- gr.Markdown("""
416
- ## πŸ€– PIXAL Assistant
417
- **LangChain 기반 λ©€ν‹°νˆ΄ μ—μ΄μ „νŠΈ**
418
- 🧰 DuckDuckGo · Wikipedia · Math · Requests · Python REPL · File · Time
419
- """)
420
- chatbot = gr.Chatbot(label="PIXAL λŒ€ν™”", height=600)
421
- msg = gr.Textbox(label="λ©”μ‹œμ§€", placeholder="λͺ…λ Ή λ˜λŠ” μ§ˆλ¬Έμ„ μž…λ ₯ν•˜μ„Έμš”...")
422
- clear = gr.Button("μ΄ˆκΈ°ν™”")
423
 
424
- msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
425
- clear.click(lambda: None, None, chatbot, queue=False)
426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427
 
428
  if __name__ == "__main__":
429
- demo.launch()
 
 
 
 
 
 
371
  #agent = create_structured_chat_agent(llm, tools, prompt)
372
  #agent= AgentExecutor(agent=agent, tools=tools,memory=mem)
373
 
374
+ # ... (μœ„μ˜ LLM, tools, agent μ„€μ • 뢀뢄은 동일)
375
+
376
+
377
+ import json
378
 
379
  # ──────────────────────────────
380
+ # βœ… λŒ€ν™” μš”μ•½ ν•¨μˆ˜
381
+ # ──────────────────────────────
382
+ def summarize_title(history):
383
+ """λŒ€ν™” 전체λ₯Ό μš”μ•½ν•΄ 제λͺ© 생성"""
384
+ if not history:
385
+ return "μƒˆ λŒ€ν™”"
386
+ text = "\n".join(f"User: {h[0]}\nAI: {h[1]}" for h in history[-3:])
387
+ try:
388
+ title = llm._call(f"λ‹€μŒ λŒ€ν™”λ₯Ό ν•œ 쀄 μš”μ•½ 제λͺ©μœΌλ‘œ λ§Œλ“€μ–΄μ€˜:\n{text}")
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:
403
+ json.dump({"title": title, "history": history}, f, ensure_ascii=False, indent=2)
404
+
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
+ items = []
409
+ for f in files:
410
+ with open(os.path.join("user_logs", f), encoding="utf-8") as jf:
411
+ data = json.load(jf)
412
+ items.append((data.get("title", f), f))
413
+ return items
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)
 
450
  response = f"⚠️ 였λ₯˜: {e}"
451
 
452
  history = history + [(message, response)]
453
+ return history, history,""
454
+
455
+ # ──────────────────────────────
456
+ # βœ… Gradio UI
457
+ # ──────────────────────────────
458
+ def load_selected(file):
459
+ return load_conversation(file)
460
 
461
  with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (LangChain + GitHub LLM)") as demo:
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)
470
+ msg = gr.Textbox(label="λ©”μ‹œμ§€", placeholder="μž…λ ₯ ν›„ Enter λ˜λŠ” 전솑 클릭")
471
+ send = gr.Button("전솑")
472
+ clear = gr.Button("μ΄ˆκΈ°ν™”")
473
+
474
+ # hidden state
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)
479
+
480
+ with gr.Column(scale=1):
481
+ gr.Markdown("### πŸ’Ύ μ €μž₯된 λŒ€ν™” 기둝")
482
+ convo_files = gr.Dropdown(label="λŒ€ν™” 선택", choices=[])
483
+ refresh_btn = gr.Button("πŸ”„ λͺ©λ‘ μƒˆλ‘œκ³ μΉ¨")
484
+ load_btn = gr.Button("뢈러였기")
485
+
486
+ def refresh_list(user):
487
+ return gr.Dropdown.update(choices=[x[0] for x in list_conversations(user)])
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
+