Jay1121 commited on
Commit
14794d8
·
verified ·
1 Parent(s): 6b9634f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -36
app.py CHANGED
@@ -209,12 +209,13 @@ footer { display: none !important; }
209
  # ------------------------------------------------------------------
210
  # 5. App (최종 수정: State 사용 + API 생성 차단)
211
  # ------------------------------------------------------------------
 
 
212
  with gr.Blocks(theme=gr.themes.Base(), css=PC_COM_CSS, title="CHOLLIAN 98") as demo:
213
- gr.Markdown("# ≪ ≫")
214
  gr.Markdown(">> 01410 접속 성공... [대화실]에 입장하셨습니다.")
215
 
216
  # [중요] 채팅 기록을 저장할 '기억 저장소' (화면엔 안 보임)
217
- # Chatbot 컴포넌트를 직접 입력으로 쓰지 않기 위해 사용
218
  history_state = gr.State([])
219
 
220
  chatbot = gr.Chatbot(show_label=False, elem_classes="chatbot")
@@ -236,62 +237,63 @@ with gr.Blocks(theme=gr.themes.Base(), css=PC_COM_CSS, title="CHOLLIAN 98") as d
236
  btn3 = gr.Button("오늘 기분 꿀꿀하네..", elem_classes="example-btn")
237
  btn4 = gr.Button("야 밥 뭐먹지 추천좀", elem_classes="example-btn")
238
 
239
- # [핵심 수정] 채팅창(chatbot)을 입력으로 쓰지 않고, state를 입력으로 사용
240
- def predict(user_input, history):
241
  history = history or []
 
 
 
 
 
 
 
 
 
 
 
 
242
  hist_pairs = []
243
- for u, b in history:
244
- if u is None or b is None:
245
- continue
246
  hist_pairs.append((u, b))
247
 
 
248
  bot_out = chat_response(user_input, hist_pairs)
249
 
250
- # 히스토
251
- new_history = list(history) + [[user_input, bot_out]]
252
-
253
- # 리턴: 입력창 비우기, State 업데이트, Chatbot 업데이트
254
- return "", new_history, new_history
255
 
256
- # [핵심 수정] api_name=False로 설정하여 스키마 생성 에러 원천 차단
 
257
  msg.submit(
258
- predict,
259
- [msg, history_state],
260
- [msg, history_state, chatbot],
261
- queue=False,
262
- api_name=False
263
  )
 
264
  submit_btn.click(
265
- predict,
266
- [msg, history_state],
267
- [msg, history_state, chatbot],
268
- queue=False,
269
- api_name=False
270
  )
271
 
272
- # 초기화: State와 Chatbot 둘 다 비움
273
  clear.click(
274
- lambda: ([], []),
275
- None,
276
- [history_state, chatbot],
277
- queue=False,
278
- api_name=False
279
  )
280
 
281
- # 예시 버튼 연결
282
  for btn, text in [
283
  (btn1, "하이 방가방가"),
284
  (btn2, "밸런스게임 ㄱㄱ"),
285
  (btn3, "오늘 기분 꿀꿀하네.."),
286
  (btn4, "야 밥 뭐먹지 추천좀")
287
  ]:
288
- # 버튼 클릭 시 텍스트 박스에 채우고 -> 바로 전송
289
  btn.click(lambda: text, None, msg, queue=False, api_name=False).then(
290
- predict,
291
- [msg, history_state],
292
- [msg, history_state, chatbot],
293
- queue=False,
294
- api_name=False
295
  )
296
 
297
  if __name__ == "__main__":
 
209
  # ------------------------------------------------------------------
210
  # 5. App (최종 수정: State 사용 + API 생성 차단)
211
  # ------------------------------------------------------------------
212
+ # 5. App (최종 수정: State 사용 + API 생성 차단 + 즉시 반응 패치)
213
+ # ------------------------------------------------------------------
214
  with gr.Blocks(theme=gr.themes.Base(), css=PC_COM_CSS, title="CHOLLIAN 98") as demo:
215
+ gr.Markdown("# ≪ 세 요 ≫")
216
  gr.Markdown(">> 01410 접속 성공... [대화실]에 입장하셨습니다.")
217
 
218
  # [중요] 채팅 기록을 저장할 '기억 저장소' (화면엔 안 보임)
 
219
  history_state = gr.State([])
220
 
221
  chatbot = gr.Chatbot(show_label=False, elem_classes="chatbot")
 
237
  btn3 = gr.Button("오늘 기분 꿀꿀하네..", elem_classes="example-btn")
238
  btn4 = gr.Button("야 밥 뭐먹지 추천좀", elem_classes="example-btn")
239
 
240
+ # [수정 1] 사용자 입력 화면에 '즉시' 띄우는 함수
241
+ def user(user_input, history):
242
  history = history or []
243
+ # 봇 응답 자리에 None을 채워서 일단 화면에 표시 (즉시 반응)
244
+ new_history = history + [[user_input, None]]
245
+ return "", new_history, new_history
246
+
247
+ # [수정 2] 봇의 응답을 생성해서 업데이트하는 함수
248
+ def bot(history):
249
+ if not history:
250
+ return history, history
251
+
252
+ user_input = history[-1][0]
253
+
254
+ # LLM에 보낼 히스토리 정리
255
  hist_pairs = []
256
+ for u, b in history[:-1]:
257
+ if u is None or b is None: continue
 
258
  hist_pairs.append((u, b))
259
 
260
+ # 답변 생성 (시간이 좀 걸림)
261
  bot_out = chat_response(user_input, hist_pairs)
262
 
263
+ # 마지막 None 자에 답변 채워넣기
264
+ history[-1][1] = bot_out
265
+ return history, history
 
 
266
 
267
+ # [수정 3] 이벤트 연결 (user 실행 -> bot 실행 순서로 체이닝)
268
+ # 이렇게 해야 엔터 치자마자 내 글이 먼저 뜨고, 똘배가 고민하는 게 보임
269
  msg.submit(
270
+ user, [msg, history_state], [msg, history_state, chatbot], queue=False, api_name=False
271
+ ).then(
272
+ bot, [history_state], [history_state, chatbot], queue=False, api_name=False
 
 
273
  )
274
+
275
  submit_btn.click(
276
+ user, [msg, history_state], [msg, history_state, chatbot], queue=False, api_name=False
277
+ ).then(
278
+ bot, [history_state], [history_state, chatbot], queue=False, api_name=False
 
 
279
  )
280
 
281
+ # 초기화
282
  clear.click(
283
+ lambda: ([], []), None, [history_state, chatbot], queue=False, api_name=False
 
 
 
 
284
  )
285
 
286
+ # 예시 버튼 연결 (버튼 클릭 -> 텍스트 입력 -> 유저 함수 -> 봇 함수)
287
  for btn, text in [
288
  (btn1, "하이 방가방가"),
289
  (btn2, "밸런스게임 ㄱㄱ"),
290
  (btn3, "오늘 기분 꿀꿀하네.."),
291
  (btn4, "야 밥 뭐먹지 추천좀")
292
  ]:
 
293
  btn.click(lambda: text, None, msg, queue=False, api_name=False).then(
294
+ user, [msg, history_state], [msg, history_state, chatbot], queue=False, api_name=False
295
+ ).then(
296
+ bot, [history_state], [history_state, chatbot], queue=False, api_name=False
 
 
297
  )
298
 
299
  if __name__ == "__main__":