Rulga commited on
Commit
44ab690
·
1 Parent(s): 5beae81

Add model change functionality and update model information display

Browse files
Files changed (1) hide show
  1. app.py +50 -49
app.py CHANGED
@@ -261,6 +261,46 @@ def respond_and_clear(message, history, conversation_id):
261
 
262
  return error_history, conversation_id, ""
263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  # Create interface
265
  with gr.Blocks() as demo:
266
  with gr.Tabs():
@@ -308,13 +348,7 @@ with gr.Blocks() as demo:
308
  )
309
  update_kb_btn.click(update_kb, None, kb_status)
310
  rebuild_kb_btn.click(rebuild_kb, None, kb_status)
311
- def clear_conversation():
312
- """Clear conversation and save history before clearing"""
313
- # Save current history if there's a conversation
314
- if chatbot and conversation_id:
315
- save_chat_history(chatbot, conversation_id)
316
- return [], None
317
-
318
  clear_btn.click(clear_conversation, None, [chatbot, conversation_id])
319
 
320
  with gr.Tab("Model Settings"):
@@ -423,49 +457,16 @@ with gr.Blocks() as demo:
423
  inputs=[],
424
  outputs=[analysis_output]
425
  )
426
-
427
- # Добавим функцию для обновления информации о модели
428
- def update_model_info(model_key):
429
- """Update model information display"""
430
- model = MODELS[model_key]
431
- return f"""
432
- **Current Model:** {model['name']}
433
-
434
- **Model ID:** `{model['id']}`
435
 
436
- **Description:** {model['description']}
437
-
438
- **Type:** {model['type']}
439
- """
440
-
441
- # Добавим функцию для смены модели
442
- def change_model(model_key):
443
- """Change active model"""
444
- global client, ACTIVE_MODEL
445
 
446
- try:
447
- # Update active model
448
- ACTIVE_MODEL = MODELS[model_key]
449
-
450
- # Reinitialize client with new model
451
- client = InferenceClient(
452
- ACTIVE_MODEL["id"],
453
- token=HF_TOKEN
454
- )
455
-
456
- return update_model_info(model_key)
457
- except Exception as e:
458
- return f"Error changing model: {str(e)}"
459
-
460
- # Добавим обработчик события изменения модели
461
- model_selector.change(
462
- fn=change_model,
463
- inputs=[model_selector],
464
- outputs=[model_info]
465
- )
466
-
467
- # При инициализации установим информацию о текущей модели
468
- model_info.value = update_model_info(DEFAULT_MODEL)
469
 
470
  # Launch application
471
  if __name__ == "__main__":
@@ -473,4 +474,4 @@ if __name__ == "__main__":
473
  if not load_vector_store():
474
  print("Knowledge base not found. Please create it through the interface.")
475
 
476
- demo.launch()
 
261
 
262
  return error_history, conversation_id, ""
263
 
264
+ # Функции для обновления информации о модели
265
+ def update_model_info(model_key):
266
+ """Update model information display"""
267
+ model = MODELS[model_key]
268
+ return f"""
269
+ **Current Model:** {model['name']}
270
+
271
+ **Model ID:** `{model['id']}`
272
+
273
+ **Description:** {model['description']}
274
+
275
+ **Type:** {model['type']}
276
+ """
277
+
278
+ # Функция для смены модели
279
+ def change_model(model_key):
280
+ """Change active model"""
281
+ global client, ACTIVE_MODEL
282
+
283
+ try:
284
+ # Update active model
285
+ ACTIVE_MODEL = MODELS[model_key]
286
+
287
+ # Reinitialize client with new model
288
+ client = InferenceClient(
289
+ ACTIVE_MODEL["id"],
290
+ token=HF_TOKEN
291
+ )
292
+
293
+ return update_model_info(model_key)
294
+ except Exception as e:
295
+ return f"Error changing model: {str(e)}"
296
+
297
+ def clear_conversation():
298
+ """Clear conversation and save history before clearing"""
299
+ # Save current history if there's a conversation
300
+ if chatbot and conversation_id:
301
+ save_chat_history(chatbot, conversation_id)
302
+ return [], None
303
+
304
  # Create interface
305
  with gr.Blocks() as demo:
306
  with gr.Tabs():
 
348
  )
349
  update_kb_btn.click(update_kb, None, kb_status)
350
  rebuild_kb_btn.click(rebuild_kb, None, kb_status)
351
+
 
 
 
 
 
 
352
  clear_btn.click(clear_conversation, None, [chatbot, conversation_id])
353
 
354
  with gr.Tab("Model Settings"):
 
457
  inputs=[],
458
  outputs=[analysis_output]
459
  )
 
 
 
 
 
 
 
 
 
460
 
461
+ # ПЕРЕМЕЩЕНО ВНУТРЬ БЛОКА: Добавляем обработчик события изменения модели
462
+ model_selector.change(
463
+ fn=change_model,
464
+ inputs=[model_selector],
465
+ outputs=[model_info]
466
+ )
 
 
 
467
 
468
+ # ПЕРЕМЕЩЕНО ВНУТРЬ БЛОКА: При инициализации устанавливаем информацию о текущей модели
469
+ model_info.update(value=update_model_info(DEFAULT_MODEL))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
 
471
  # Launch application
472
  if __name__ == "__main__":
 
474
  if not load_vector_store():
475
  print("Knowledge base not found. Please create it through the interface.")
476
 
477
+ demo.launch()