ll7098ll commited on
Commit
ee46b7e
·
verified ·
1 Parent(s): 7e4954c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -35,16 +35,23 @@ def start_new_chat_session():
35
  # 초기 세션 시작
36
  start_new_chat_session()
37
 
38
- def respond(user_input, history):
 
39
  global chat_session
40
- history.append((user_input, ""))
41
- yield "", history
42
 
43
- response = chat_session.send_message(user_input)
44
- full_response = response.text # Get the entire response text
45
 
46
- history[-1] = (user_input, full_response) # Update history with the full response
47
- yield "", history
 
 
 
 
 
 
 
 
48
 
49
  def clear_chat(chatbot):
50
  start_new_chat_session()
@@ -52,14 +59,14 @@ def clear_chat(chatbot):
52
  return []
53
 
54
  with gr.Blocks() as demo:
55
- gr.Markdown("<div style='font-size: 30px; font-weight: bold;'>언어치료 전문가</div>")
56
- gr.Markdown("언어 치료에 대한 고민을 상담해줍니다.")
 
57
  chatbot = gr.Chatbot(label="채팅창")
58
- msg = gr.Textbox(label="입력")
59
- clear = gr.Button("초기화")
60
 
61
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
62
- # chatbot 상태 초기화 및 새로운 세션 시작
63
- clear.click(clear_chat, outputs=[chatbot], queue=False)
64
 
65
  demo.launch()
 
35
  # 초기 세션 시작
36
  start_new_chat_session()
37
 
38
+ def respond(message, chat_history):
39
+ """사용자 입력에 응답하고 채팅 기록을 업데이트합니다."""
40
  global chat_session
 
 
41
 
42
+ if chat_session is None:
43
+ start_new_chat_session()
44
 
45
+ chat_history.append((message, ""))
46
+
47
+ # 스트리밍 방식으로 응답 생성
48
+ response = chat_session.send_message(message, stream=True)
49
+ full_response = ""
50
+ for chunk in response:
51
+ full_response += chunk.text
52
+ chat_history[-1] = (message, full_response)
53
+ yield chat_history, # Gradio 업데이트
54
+ yield chat_history # 최종 업데이트
55
 
56
  def clear_chat(chatbot):
57
  start_new_chat_session()
 
59
  return []
60
 
61
  with gr.Blocks() as demo:
62
+ gr.Markdown("# 언어치료 전문가")
63
+ gr.Markdown("언어 치료에 대한 고민을 상담해줍니다.")
64
+
65
  chatbot = gr.Chatbot(label="채팅창")
66
+ message = gr.Textbox(label="입력")
67
+ clear_button = gr.Button("초기화")
68
 
69
+ message.submit(respond, [message, chatbot], [chatbot]) # chatbot 만 업데이트
70
+ clear_button.click(clear_chat, outputs=[chatbot])
 
71
 
72
  demo.launch()