hualinxin8615 commited on
Commit
585ad0e
·
verified ·
1 Parent(s): 98b541b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -33
app.py CHANGED
@@ -952,6 +952,62 @@ def handle_user_access_final(request: gr.Request):
952
  """
953
  return title, gr.HTML(error_html)
954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955
  # 修改后的主Gradio界面
956
  def create_main_interface():
957
  """创建主要的用户界面"""
@@ -959,21 +1015,7 @@ def create_main_interface():
959
  title_display = gr.Markdown("# 🔄 CIV3283 Learning Assistant Load Distributor", elem_id="title")
960
  content_display = gr.HTML("")
961
 
962
- # 添加隐藏缓存信息显示(用于调试,默认不显示)
963
- with gr.Accordion("🔧 System Information", open=False, visible=False):
964
- cache_info = gr.JSON(label="Cache Status", visible=False)
965
-
966
- def update_cache_info():
967
- return get_cache_status()
968
-
969
- # 每30秒更新一次缓存信息(如果界面是打开的)
970
- interface.load(
971
- fn=update_cache_info,
972
- outputs=cache_info,
973
- every=30
974
- )
975
-
976
- # 主要的负载均衡逻辑
977
  interface.load(
978
  fn=handle_user_access_final,
979
  outputs=[title_display, content_display]
@@ -983,21 +1025,26 @@ def create_main_interface():
983
 
984
  # 修改后的main函数
985
  if __name__ == "__main__":
986
- # 创建主界面
987
- main_interface = create_main_interface()
988
-
989
- # 启动主界面
990
- main_interface.launch()
991
-
992
- # 在程序退出时优雅地关闭后台线程
993
- import atexit
994
-
995
- def cleanup_on_exit():
996
- """程序退出时的清理函数"""
997
- global activity_cache
998
- if activity_cache:
999
- print("[cleanup_on_exit] Stopping background cache updates...")
1000
- activity_cache.stop_background_updates()
1001
-
1002
- # 注册退出时的清理函数
1003
- atexit.register(cleanup_on_exit)
 
 
 
 
 
 
952
  """
953
  return title, gr.HTML(error_html)
954
 
955
+ # 添加可选的管理界面(用于调试)
956
+ def create_admin_interface():
957
+ """创建管理界面(可选)- 简化版本"""
958
+ with gr.Blocks(title="CIV3283 Load Distributor - Admin") as admin_interface:
959
+ gr.Markdown("# 🔧 CIV3283 Load Distributor - Admin Panel")
960
+
961
+ with gr.Row():
962
+ cache_status_btn = gr.Button("📊 Check Cache Status", variant="secondary")
963
+ force_update_btn = gr.Button("🔄 Force Cache Update", variant="primary")
964
+
965
+ cache_info_display = gr.JSON(label="Cache Information")
966
+ status_message = gr.Markdown("")
967
+
968
+ def check_cache_status():
969
+ try:
970
+ status = get_cache_status()
971
+ if 'error' in status:
972
+ return status, "❌ Cache not available"
973
+ else:
974
+ age_min = status.get('age_minutes', 0)
975
+ spaces_count = status.get('spaces_count', 0)
976
+ is_fresh = status.get('is_fresh', False)
977
+
978
+ if is_fresh:
979
+ message = f"✅ Cache is fresh ({age_min:.1f} min old, {spaces_count} spaces)"
980
+ else:
981
+ message = f"⚠️ Cache is stale ({age_min:.1f} min old, {spaces_count} spaces)"
982
+
983
+ return status, message
984
+ except Exception as e:
985
+ return {"error": str(e)}, f"❌ Error checking cache: {str(e)}"
986
+
987
+ def force_update():
988
+ try:
989
+ result = force_cache_update()
990
+ if 'error' in result:
991
+ return result, "❌ Failed to update cache"
992
+ else:
993
+ # 获取更新后的状态
994
+ status = get_cache_status()
995
+ return status, "✅ Cache updated successfully"
996
+ except Exception as e:
997
+ return {"error": str(e)}, f"❌ Error updating cache: {str(e)}"
998
+
999
+ cache_status_btn.click(
1000
+ fn=check_cache_status,
1001
+ outputs=[cache_info_display, status_message]
1002
+ )
1003
+
1004
+ force_update_btn.click(
1005
+ fn=force_update,
1006
+ outputs=[cache_info_display, status_message]
1007
+ )
1008
+
1009
+ return admin_interface
1010
+
1011
  # 修改后的主Gradio界面
1012
  def create_main_interface():
1013
  """创建主要的用户界面"""
 
1015
  title_display = gr.Markdown("# 🔄 CIV3283 Learning Assistant Load Distributor", elem_id="title")
1016
  content_display = gr.HTML("")
1017
 
1018
+ # 主要负载均衡逻辑 - 页面加载时执行
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1019
  interface.load(
1020
  fn=handle_user_access_final,
1021
  outputs=[title_display, content_display]
 
1025
 
1026
  # 修改后的main函数
1027
  if __name__ == "__main__":
1028
+ import sys
1029
+
1030
+ # 检查是否需要管理界面
1031
+ enable_admin = "--admin" in sys.argv or os.environ.get("ENABLE_ADMIN", "false").lower() == "true"
1032
+
1033
+ if enable_admin:
1034
+ print("🔧 Starting with admin interface enabled")
1035
+ # 创建带管理界面的组合界面
1036
+ main_interface = create_main_interface()
1037
+ admin_interface = create_admin_interface()
1038
+
1039
+ # 使用TabbedInterface组合两个界面
1040
+ combined_interface = gr.TabbedInterface(
1041
+ [main_interface, admin_interface],
1042
+ ["🎯 Load Distributor", "🔧 Admin Panel"],
1043
+ title="CIV3283 Load Distributor System"
1044
+ )
1045
+ combined_interface.launch()
1046
+ else:
1047
+ print("🎯 Starting main interface only")
1048
+ # 只启动主界面
1049
+ main_interface = create_main_interface()
1050
+ main_interface.launch()