Jay1121 commited on
Commit
db04905
·
verified ·
1 Parent(s): 9d39fc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -50
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
  import gradio as gr
3
  from huggingface_hub import hf_hub_download
4
  from llama_cpp import Llama
@@ -39,6 +38,7 @@ def chat_response(user_input, history_pairs):
39
  """
40
  history_pairs: [(user, bot), ...] 형태로 받아서 llama_cpp messages로 변환
41
  """
 
42
  clean_input = (user_input or "").replace(" ", "")
43
 
44
  greeting_words = ["안녕", "ㅎㅇ", "하이", "반가", "접속"]
@@ -60,7 +60,7 @@ def chat_response(user_input, history_pairs):
60
  final_instruction = user_input
61
 
62
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
63
- for u, b in (history_pairs or []):
64
  if u is None or b is None:
65
  continue
66
  messages.append({"role": "user", "content": str(u)})
@@ -185,19 +185,8 @@ button.primary:hover { background: var(--pc-white) !important; }
185
  }
186
  #clear-btn:hover { color: var(--pc-white) !important; border-color: var(--pc-white) !important; }
187
 
188
- .examples {
189
- margin-top: 20px !important;
190
- border-top: 1px dashed var(--pc-grey);
191
- padding-top: 10px;
192
- }
193
- .examples .label { display: none !important; }
194
- .examples div.gallery {
195
- display: flex !important;
196
- flex-direction: row !important;
197
- gap: 10px !important;
198
- flex-wrap: wrap !important;
199
- }
200
- .examples button {
201
  background: transparent !important;
202
  color: var(--pc-cyan) !important;
203
  border: 1px solid var(--pc-cyan) !important;
@@ -205,19 +194,25 @@ button.primary:hover { background: var(--pc-white) !important; }
205
  padding: 5px 15px !important;
206
  font-size: 16px !important;
207
  font-family: 'NeoDunggeunmo', monospace !important;
 
 
 
 
 
 
 
208
  }
209
 
210
  footer { display: none !important; }
211
  """
212
 
213
  # ------------------------------------------------------------------
214
- # 5. App (가로 배치 + API 엔드포인트 생성)
215
  # ------------------------------------------------------------------
216
  with gr.Blocks(theme=gr.themes.Base(), css=PC_COM_CSS, title="CHOLLIAN 98") as demo:
217
  gr.Markdown("# ≪ 십 리 안 ≫")
218
  gr.Markdown(">> 01410 접속 성공... [대화실]에 입장하셨습니다.")
219
 
220
- # ✅ Chatbot은 4.36.1에서 list-of-lists 포맷이 제일 안전
221
  chatbot = gr.Chatbot(show_label=False, elem_classes="chatbot")
222
 
223
  with gr.Row(elem_classes="input-container"):
@@ -229,22 +224,18 @@ with gr.Blocks(theme=gr.themes.Base(), css=PC_COM_CSS, title="CHOLLIAN 98") as d
229
 
230
  clear = gr.Button("[ 화면 지우기 ]", elem_id="clear-btn")
231
 
232
- gr.Examples(
233
- examples=[
234
- "하이 방가방가",
235
- "밸런스게임 ㄱㄱ",
236
- "오늘 기분 꿀꿀하네..",
237
- "야 뭐먹지 추천좀",
238
- ],
239
- inputs=msg,
240
- label="빠른 입력",
241
- cache_examples=False
242
- )
243
 
244
  def predict(user_input, history):
245
  history = history or []
246
-
247
- # 모델로 넘길 히스토리: [(u,b), ...]
248
  hist_pairs = []
249
  for u, b in history:
250
  if u is None or b is None:
@@ -252,28 +243,25 @@ with gr.Blocks(theme=gr.themes.Base(), css=PC_COM_CSS, title="CHOLLIAN 98") as d
252
  hist_pairs.append((u, b))
253
 
254
  bot_out = chat_response(user_input, hist_pairs)
255
-
256
- # Gradio Chatbot 출력: [[u,b], ...]
257
  new_history = [list(x) for x in history] + [[user_input, bot_out]]
258
  return "", new_history
259
 
260
- # api_name 때문에 API 문서에 엔드포인트가 생김 -> "No API found" 사라짐
261
- msg.submit(
262
- predict,
263
- inputs=[msg, chatbot],
264
- outputs=[msg, chatbot],
265
- queue=True,
266
- api_name="chat"
267
- )
268
- submit_btn.click(
269
- predict,
270
- inputs=[msg, chatbot],
271
- outputs=[msg, chatbot],
272
- queue=True,
273
- api_name="chat"
274
- )
275
- clear.click(lambda: [], None, chatbot, queue=False, api_name="clear")
276
 
277
  if __name__ == "__main__":
278
- # Spaces면 보통 포트 지정 하는 게 안전함 (지정해도 되는데, 이게 덜 말썽남)
279
- demo.queue().launch(server_name="0.0.0.0")
 
 
1
  import gradio as gr
2
  from huggingface_hub import hf_hub_download
3
  from llama_cpp import Llama
 
38
  """
39
  history_pairs: [(user, bot), ...] 형태로 받아서 llama_cpp messages로 변환
40
  """
41
+ history_pairs = history_pairs or []
42
  clean_input = (user_input or "").replace(" ", "")
43
 
44
  greeting_words = ["안녕", "ㅎㅇ", "하이", "반가", "접속"]
 
60
  final_instruction = user_input
61
 
62
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
63
+ for u, b in history_pairs:
64
  if u is None or b is None:
65
  continue
66
  messages.append({"role": "user", "content": str(u)})
 
185
  }
186
  #clear-btn:hover { color: var(--pc-white) !important; border-color: var(--pc-white) !important; }
187
 
188
+ /* 수동 예시 버튼 스타일 */
189
+ .example-btn {
 
 
 
 
 
 
 
 
 
 
 
190
  background: transparent !important;
191
  color: var(--pc-cyan) !important;
192
  border: 1px solid var(--pc-cyan) !important;
 
194
  padding: 5px 15px !important;
195
  font-size: 16px !important;
196
  font-family: 'NeoDunggeunmo', monospace !important;
197
+ margin-right: 8px !important;
198
+ margin-bottom: 8px !important;
199
+ }
200
+ .example-btn:hover {
201
+ background: var(--pc-cyan) !important;
202
+ color: #000 !important;
203
+ cursor: pointer !important;
204
  }
205
 
206
  footer { display: none !important; }
207
  """
208
 
209
  # ------------------------------------------------------------------
210
+ # 5. App (안정화 버전: Queue OFF, Examples 수동 구현)
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
  chatbot = gr.Chatbot(show_label=False, elem_classes="chatbot")
217
 
218
  with gr.Row(elem_classes="input-container"):
 
224
 
225
  clear = gr.Button("[ 화면 지우기 ]", elem_id="clear-btn")
226
 
227
+ # ------------------------------------------------------
228
+ # [수정] gr.Examples 대신 수동 버튼 사용 (버그 회피)
229
+ # ------------------------------------------------------
230
+ gr.Markdown(">> 빠른 명령어 입력 (클릭)", elem_id="example-label")
231
+ with gr.Row():
232
+ btn1 = gr.Button("하이 방가방가", elem_classes="example-btn")
233
+ btn2 = gr.Button("밸런스게임 ㄱㄱ", elem_classes="example-btn")
234
+ btn3 = gr.Button("오늘 기분 꿀꿀하네..", elem_classes="example-btn")
235
+ btn4 = gr.Button(" 밥 뭐먹지 추천좀", elem_classes="example-btn")
 
 
236
 
237
  def predict(user_input, history):
238
  history = history or []
 
 
239
  hist_pairs = []
240
  for u, b in history:
241
  if u is None or b is None:
 
243
  hist_pairs.append((u, b))
244
 
245
  bot_out = chat_response(user_input, hist_pairs)
 
 
246
  new_history = [list(x) for x in history] + [[user_input, bot_out]]
247
  return "", new_history
248
 
249
+ # 이벤트 핸들러: queue=False 설정으로 'No API found' 방지
250
+ msg.submit(predict, [msg, chatbot], [msg, chatbot], queue=False)
251
+ submit_btn.click(predict, [msg, chatbot], [msg, chatbot], queue=False)
252
+ clear.click(lambda: [], None, chatbot, queue=False)
253
+
254
+ # 예시 버튼 클릭 로직 연결
255
+ for btn, text in [
256
+ (btn1, "하이 방가방가"),
257
+ (btn2, "밸런스게임 ㄱㄱ"),
258
+ (btn3, "오늘 기분 꿀꿀하네.."),
259
+ (btn4, "야 밥 뭐먹지 추천좀")
260
+ ]:
261
+ btn.click(lambda: text, None, msg, queue=False).then(
262
+ predict, [msg, chatbot], [msg, chatbot], queue=False
263
+ )
 
264
 
265
  if __name__ == "__main__":
266
+ # queue() 메서드 삭제 HTTP 모드 실행
267
+ demo.launch(server_name="0.0.0.0", server_port=7860)