Junhoee commited on
Commit
17c1525
Β·
verified Β·
1 Parent(s): 37263b2

Update app_gradio.py

Browse files
Files changed (1) hide show
  1. app_gradio.py +27 -11
app_gradio.py CHANGED
@@ -6,8 +6,8 @@ from threading import Lock
6
  import gradio as gr
7
 
8
  from megumin_agent.chat import ChatServices
9
- from megumin_agent.chat import chat_once
10
  from megumin_agent.chat import create_chat_services
 
11
 
12
 
13
  INITIAL_GREETING = "λ‚΄ 이름은 메ꡬ밍! ν™λ§ˆμ‘± 제일의 λ§ˆλ²•μ‚¬μ΄μž, 폭렬 λ§ˆλ²•μ„ νŽΌμΉ˜λŠ” 자!"
@@ -682,22 +682,38 @@ async def respond(
682
  session_id: str | None,
683
  ):
684
  if not message.strip():
685
- return history, session_id, "", "", current_badge()
 
686
 
687
- reply, active_session_id = await chat_once(
 
 
 
 
 
 
 
688
  user_message=message,
689
  services=get_services(),
690
  session_id=session_id,
691
- )
692
- updated_history = list(history)
693
- updated_history.append({"role": "user", "content": message})
694
- updated_history.append(
695
- {
 
 
 
 
 
 
 
 
696
  "role": "assistant",
697
- "content": reply or "μ˜€λŠ˜μ€ 마λ ₯의 흐름이 쑰금 λΆˆμ•ˆμ •ν•˜κ΅°μš”. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ?",
698
  }
699
- )
700
- return updated_history, active_session_id, "", "", current_badge(
701
  message,
702
  updated_history[-1]["content"],
703
  )
 
6
  import gradio as gr
7
 
8
  from megumin_agent.chat import ChatServices
 
9
  from megumin_agent.chat import create_chat_services
10
+ from megumin_agent.chat import stream_chat
11
 
12
 
13
  INITIAL_GREETING = "λ‚΄ 이름은 메ꡬ밍! ν™λ§ˆμ‘± 제일의 λ§ˆλ²•μ‚¬μ΄μž, 폭렬 λ§ˆλ²•μ„ νŽΌμΉ˜λŠ” 자!"
 
682
  session_id: str | None,
683
  ):
684
  if not message.strip():
685
+ yield history, session_id, "", "", current_badge()
686
+ return
687
 
688
+ updated_history = list(history)
689
+ updated_history.append({"role": "user", "content": message})
690
+ updated_history.append({"role": "assistant", "content": ""})
691
+ yield updated_history, session_id, "", "λ‹΅λ³€ 생성 쀑...", current_badge(message)
692
+
693
+ active_session_id = session_id
694
+ got_reply = False
695
+ async for partial_text, active_session_id in stream_chat(
696
  user_message=message,
697
  services=get_services(),
698
  session_id=session_id,
699
+ ):
700
+ got_reply = True
701
+ updated_history[-1] = {"role": "assistant", "content": partial_text}
702
+ yield (
703
+ updated_history,
704
+ active_session_id,
705
+ "",
706
+ "λ‹΅λ³€ 생성 쀑...",
707
+ current_badge(message, partial_text),
708
+ )
709
+
710
+ if not got_reply:
711
+ updated_history[-1] = {
712
  "role": "assistant",
713
+ "content": "μ˜€λŠ˜μ€ 마λ ₯의 흐름이 쑰금 λΆˆμ•ˆμ •ν•˜κ΅°μš”. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ?",
714
  }
715
+
716
+ yield updated_history, active_session_id, "", "", current_badge(
717
  message,
718
  updated_history[-1]["content"],
719
  )