X commited on
Commit
ebf66cb
·
verified ·
1 Parent(s): 46b30b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -97,12 +97,14 @@ def generate_response(message, history, username, current_chat_id):
97
  history_data[current_chat_id]["messages"] = history
98
  save_history(username, history_data)
99
 
100
- return history, gr.update(value=""), current_chat_id
 
 
101
 
102
  # === УПРАВЛЕНИЕ ЧАТАМИ ===
103
  def new_chat(username):
104
  if not username:
105
- return [], None, []
106
  chat_id = datetime.now().strftime("%Y%m%d_%H%M%S")
107
  history_data = load_history(username)
108
  history_data[chat_id] = {
@@ -111,7 +113,8 @@ def new_chat(username):
111
  "messages": []
112
  }
113
  save_history(username, history_data)
114
- return [], chat_id, get_chat_list(username)
 
115
 
116
  def load_chat(chat_title, username, current_chat_id):
117
  if not username or not chat_title:
@@ -135,14 +138,15 @@ def get_chat_list(username):
135
 
136
  def delete_chat(chat_title, username):
137
  if not username or not chat_title:
138
- return [], {}, None, []
139
  history_data = load_history(username)
140
  for cid, data in list(history_data.items()):
141
  if data["title"] == chat_title:
142
  del history_data[cid]
143
  break
144
  save_history(username, history_data)
145
- return [], history_data, None, get_chat_list(username)
 
146
 
147
  # === CSS ===
148
  CUSTOM_CSS = """
@@ -217,8 +221,8 @@ with gr.Blocks(title="OpenAirAI-X Chat") as demo:
217
  pro_badge = gr.HTML('<div class="pro-badge">👑 PRO</div>', visible=False)
218
  new_chat_btn = gr.Button("➕ Новый чат", variant="primary")
219
  gr.Markdown("### 📚 История чатов")
220
- # ЗАМЕНА: Используем Dropdown вместо Radio для стабильности в Gradio 6.0
221
- chat_list = gr.Dropdown(choices=[], label="Ваши чаты", interactive=True, allow_custom_value=False)
222
  delete_chat_btn = gr.Button("🗑️ Удалить выбранный чат", variant="stop", size="sm")
223
  logout_btn = gr.Button("🚪 Выйти", size="sm")
224
 
@@ -241,7 +245,7 @@ with gr.Blocks(title="OpenAirAI-X Chat") as demo:
241
  def handle_login(username, token):
242
  if not username.strip():
243
  gr.Warning("Введите имя пользователя!")
244
- return gr.update(), gr.update(), "Не авторизован", "", {}, None, [], gr.update(visible=False)
245
 
246
  is_pro = check_if_pro(token)
247
  history_data = load_history(username)
@@ -269,7 +273,7 @@ with gr.Blocks(title="OpenAirAI-X Chat") as demo:
269
  username,
270
  history_data,
271
  current_chat_id,
272
- chat_list_choices,
273
  gr.update(visible=is_pro, value=pro_badge_html)
274
  )
275
 
@@ -281,7 +285,7 @@ with gr.Blocks(title="OpenAirAI-X Chat") as demo:
281
  "",
282
  {},
283
  None,
284
- [],
285
  gr.update(visible=False)
286
  )
287
 
@@ -291,24 +295,17 @@ with gr.Blocks(title="OpenAirAI-X Chat") as demo:
291
  outputs=[main_interface, login_screen, user_info, username_state, history_state, current_chat_id_state, chat_list, pro_badge]
292
  )
293
 
 
294
  send_btn.click(
295
  fn=generate_response,
296
  inputs=[msg_input, chatbot, username_state, current_chat_id_state],
297
- outputs=[chatbot, msg_input, current_chat_id_state]
298
- ).then(
299
- fn=lambda u: get_chat_list(u),
300
- inputs=[username_state],
301
- outputs=[chat_list]
302
  )
303
 
304
  msg_input.submit(
305
  fn=generate_response,
306
  inputs=[msg_input, chatbot, username_state, current_chat_id_state],
307
- outputs=[chatbot, msg_input, current_chat_id_state]
308
- ).then(
309
- fn=lambda u: get_chat_list(u),
310
- inputs=[username_state],
311
- outputs=[chat_list]
312
  )
313
 
314
  new_chat_btn.click(
@@ -317,7 +314,6 @@ with gr.Blocks(title="OpenAirAI-X Chat") as demo:
317
  outputs=[chatbot, current_chat_id_state, chat_list]
318
  )
319
 
320
- # Dropdown использует событие 'change' для выбора элемента
321
  chat_list.change(
322
  fn=load_chat,
323
  inputs=[chat_list, username_state, current_chat_id_state],
 
97
  history_data[current_chat_id]["messages"] = history
98
  save_history(username, history_data)
99
 
100
+ # Возвращаем обновленный список чатов вместе с ответом
101
+ chat_list_choices = get_chat_list(username)
102
+ return history, gr.update(value=""), current_chat_id, gr.update(choices=chat_list_choices)
103
 
104
  # === УПРАВЛЕНИЕ ЧАТАМИ ===
105
  def new_chat(username):
106
  if not username:
107
+ return [], None, gr.update(choices=[])
108
  chat_id = datetime.now().strftime("%Y%m%d_%H%M%S")
109
  history_data = load_history(username)
110
  history_data[chat_id] = {
 
113
  "messages": []
114
  }
115
  save_history(username, history_data)
116
+ chat_list_choices = get_chat_list(username)
117
+ return [], chat_id, gr.update(choices=chat_list_choices, value=None)
118
 
119
  def load_chat(chat_title, username, current_chat_id):
120
  if not username or not chat_title:
 
138
 
139
  def delete_chat(chat_title, username):
140
  if not username or not chat_title:
141
+ return [], {}, None, gr.update(choices=[])
142
  history_data = load_history(username)
143
  for cid, data in list(history_data.items()):
144
  if data["title"] == chat_title:
145
  del history_data[cid]
146
  break
147
  save_history(username, history_data)
148
+ chat_list_choices = get_chat_list(username)
149
+ return [], history_data, None, gr.update(choices=chat_list_choices, value=None)
150
 
151
  # === CSS ===
152
  CUSTOM_CSS = """
 
221
  pro_badge = gr.HTML('<div class="pro-badge">👑 PRO</div>', visible=False)
222
  new_chat_btn = gr.Button("➕ Новый чат", variant="primary")
223
  gr.Markdown("### 📚 История чатов")
224
+ # Используем Listbox - он стабильнее для списков в Gradio 6
225
+ chat_list = gr.Listbox(choices=[], label="Ваши чаты", interactive=True, type="value")
226
  delete_chat_btn = gr.Button("🗑️ Удалить выбранный чат", variant="stop", size="sm")
227
  logout_btn = gr.Button("🚪 Выйти", size="sm")
228
 
 
245
  def handle_login(username, token):
246
  if not username.strip():
247
  gr.Warning("Введите имя пользователя!")
248
+ return gr.update(), gr.update(), "Не авторизован", "", {}, None, gr.update(choices=[]), gr.update(visible=False)
249
 
250
  is_pro = check_if_pro(token)
251
  history_data = load_history(username)
 
273
  username,
274
  history_data,
275
  current_chat_id,
276
+ gr.update(choices=chat_list_choices, value=None),
277
  gr.update(visible=is_pro, value=pro_badge_html)
278
  )
279
 
 
285
  "",
286
  {},
287
  None,
288
+ gr.update(choices=[]),
289
  gr.update(visible=False)
290
  )
291
 
 
295
  outputs=[main_interface, login_screen, user_info, username_state, history_state, current_chat_id_state, chat_list, pro_badge]
296
  )
297
 
298
+ # Объединяем генерацию и обновление списка в одну функцию для надежности
299
  send_btn.click(
300
  fn=generate_response,
301
  inputs=[msg_input, chatbot, username_state, current_chat_id_state],
302
+ outputs=[chatbot, msg_input, current_chat_id_state, chat_list]
 
 
 
 
303
  )
304
 
305
  msg_input.submit(
306
  fn=generate_response,
307
  inputs=[msg_input, chatbot, username_state, current_chat_id_state],
308
+ outputs=[chatbot, msg_input, current_chat_id_state, chat_list]
 
 
 
 
309
  )
310
 
311
  new_chat_btn.click(
 
314
  outputs=[chatbot, current_chat_id_state, chat_list]
315
  )
316
 
 
317
  chat_list.change(
318
  fn=load_chat,
319
  inputs=[chat_list, username_state, current_chat_id_state],