Update app.py
Browse files
app.py
CHANGED
|
@@ -29,8 +29,21 @@ model = genai.GenerativeModel(
|
|
| 29 |
chat_session = model.start_chat(history=[])
|
| 30 |
|
| 31 |
def respond(user_input, history):
|
|
|
|
|
|
|
|
|
|
| 32 |
response = chat_session.send_message(user_input)
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return "", history
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
|
@@ -39,14 +52,7 @@ with gr.Blocks() as demo:
|
|
| 39 |
msg = gr.Textbox(label="입력")
|
| 40 |
clear = gr.Button("초기화")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
def update_chatbot(history):
|
| 44 |
-
chatbot.update(history)
|
| 45 |
-
return history
|
| 46 |
-
|
| 47 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot], queue=False)
|
| 48 |
-
# 입력 후 즉시 챗봇 업데이트
|
| 49 |
-
msg.submit(update_chatbot, [chatbot], [chatbot], queue=False)
|
| 50 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 51 |
|
| 52 |
demo.launch()
|
|
|
|
| 29 |
chat_session = model.start_chat(history=[])
|
| 30 |
|
| 31 |
def respond(user_input, history):
|
| 32 |
+
history.append((user_input, "")) # 빈 답변 추가
|
| 33 |
+
chatbot.append((user_input, "")) # 채팅창에 빈 답변 추가
|
| 34 |
+
|
| 35 |
response = chat_session.send_message(user_input)
|
| 36 |
+
|
| 37 |
+
# 응답을 실시간으로 타이핑 효과 구현
|
| 38 |
+
for i in range(len(response.text)):
|
| 39 |
+
history[-1] = (user_input, response.text[:i+1]) # 답변 부분만 업데이트
|
| 40 |
+
chatbot.append(history[-1]) # 채팅창 업데이트
|
| 41 |
+
yield "", history # 업데이트된 채팅창 반환
|
| 42 |
+
time.sleep(0.5) # 타이핑 속도 조절
|
| 43 |
+
|
| 44 |
+
history.append((user_input, response.text)) # 완전한 답변 추가
|
| 45 |
+
chatbot.append(history[-1]) # 채팅창 업데이트
|
| 46 |
+
|
| 47 |
return "", history
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
|
|
|
| 52 |
msg = gr.Textbox(label="입력")
|
| 53 |
clear = gr.Button("초기화")
|
| 54 |
|
| 55 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 57 |
|
| 58 |
demo.launch()
|