Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -731,10 +731,11 @@ def create_status_display(space_activity):
|
|
| 731 |
|
| 732 |
return status_display
|
| 733 |
|
| 734 |
-
|
| 735 |
-
|
|
|
|
| 736 |
|
| 737 |
-
print(f"[
|
| 738 |
|
| 739 |
# 第一步:过滤掉不符合基本条件的空间
|
| 740 |
basic_available_spaces = []
|
|
@@ -745,7 +746,7 @@ def select_space_with_enhanced_collision_avoidance(space_activity, student_id, r
|
|
| 745 |
not space['is_recently_allocated_local']):
|
| 746 |
basic_available_spaces.append(space)
|
| 747 |
|
| 748 |
-
print(f"[
|
| 749 |
|
| 750 |
if not basic_available_spaces:
|
| 751 |
# 生成详细的错误信息
|
|
@@ -767,19 +768,19 @@ def select_space_with_enhanced_collision_avoidance(space_activity, student_id, r
|
|
| 767 |
f"**Please try again in 1-2 minutes.**"
|
| 768 |
)
|
| 769 |
|
| 770 |
-
print(f"[
|
| 771 |
raise gr.Error(error_msg, duration=10)
|
| 772 |
|
| 773 |
# 第二步:选择最优空间并进行最终验证
|
| 774 |
selected_space = basic_available_spaces[0] # 已经按idle_time排序
|
| 775 |
space_name = selected_space['space_name']
|
| 776 |
|
| 777 |
-
print(f"[
|
| 778 |
|
| 779 |
# 第三步:最终防撞车检查 - 再次验证本地分配状态
|
| 780 |
is_local_conflict, conflicting_student = local_tracker.is_recently_allocated_locally(space_name)
|
| 781 |
if is_local_conflict:
|
| 782 |
-
print(f"[
|
| 783 |
|
| 784 |
# 寻找替代空间
|
| 785 |
alternative_spaces = [s for s in basic_available_spaces[1:]
|
|
@@ -788,37 +789,52 @@ def select_space_with_enhanced_collision_avoidance(space_activity, student_id, r
|
|
| 788 |
if alternative_spaces:
|
| 789 |
selected_space = alternative_spaces[0]
|
| 790 |
space_name = selected_space['space_name']
|
| 791 |
-
print(f"[
|
| 792 |
else:
|
| 793 |
error_msg = (
|
| 794 |
f"🚫 **Collision detected and no alternatives available**\n\n"
|
| 795 |
f"The system detected a potential conflict with another student's allocation.\n\n"
|
| 796 |
f"**Please try again in 10-15 seconds.**"
|
| 797 |
)
|
| 798 |
-
print(f"[
|
| 799 |
raise gr.Error(error_msg, duration=8)
|
| 800 |
|
| 801 |
# 第四步:立即记录本地分配(在写入文件之前)
|
| 802 |
local_tracker.record_local_allocation(space_name, student_id)
|
| 803 |
-
print(f"[
|
| 804 |
|
| 805 |
# 第五步:记录到文件和远程
|
| 806 |
write_success = write_allocation_record(space_name, student_id, repo.local_dir)
|
| 807 |
if write_success:
|
| 808 |
push_success = simple_push_allocation_record(repo, space_name, student_id)
|
| 809 |
if not push_success:
|
| 810 |
-
print(f"[
|
| 811 |
else:
|
| 812 |
-
print(f"[
|
| 813 |
|
| 814 |
-
# 第六步:生成结果
|
| 815 |
-
status_display =
|
| 816 |
redirect_url = f"https://huggingface.co/spaces/CIV3283/{space_name}/?check={student_id}"
|
| 817 |
|
| 818 |
-
print(f"[
|
| 819 |
|
| 820 |
return redirect_to_space(redirect_url, selected_space, status_display)
|
| 821 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
def redirect_to_space(redirect_url, selected_space, status_display):
|
| 823 |
"""Display redirect information with manual click option"""
|
| 824 |
|
|
@@ -880,9 +896,9 @@ def redirect_to_space(redirect_url, selected_space, status_display):
|
|
| 880 |
|
| 881 |
return gr.HTML(redirect_html)
|
| 882 |
|
| 883 |
-
def
|
| 884 |
-
"""
|
| 885 |
-
print(f"[
|
| 886 |
|
| 887 |
# Initialize connection to data storage
|
| 888 |
repo = init_data_storage_connection()
|
|
@@ -901,9 +917,8 @@ def load_balance_user_final(student_id):
|
|
| 901 |
# 使用缓存版本的分析函数
|
| 902 |
space_activity = analyze_space_activity_cached(available_spaces, LOCAL_DATA_DIR)
|
| 903 |
|
| 904 |
-
#
|
| 905 |
-
return
|
| 906 |
-
|
| 907 |
|
| 908 |
def get_url_params(request: gr.Request):
|
| 909 |
"""Extract URL parameters from request"""
|
|
@@ -916,8 +931,48 @@ def get_url_params(request: gr.Request):
|
|
| 916 |
return "Load Distributor", None
|
| 917 |
return "Load Distributor", None
|
| 918 |
|
| 919 |
-
def
|
| 920 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 921 |
title, check_id = get_url_params(request)
|
| 922 |
|
| 923 |
if not check_id:
|
|
@@ -936,7 +991,7 @@ def handle_user_access_final(request: gr.Request):
|
|
| 936 |
|
| 937 |
# Valid student ID - perform cached load balancing
|
| 938 |
try:
|
| 939 |
-
result =
|
| 940 |
return title, result
|
| 941 |
except Exception as e:
|
| 942 |
# Handle any errors during load balancing
|
|
|
|
| 731 |
|
| 732 |
return status_display
|
| 733 |
|
| 734 |
+
# 修改select_space函数以使用新的状态显示
|
| 735 |
+
def select_space_with_enhanced_collision_avoidance_cached(space_activity, student_id, repo):
|
| 736 |
+
"""使用缓存的增强防撞空间选择函数"""
|
| 737 |
|
| 738 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Starting selection for student: {student_id}")
|
| 739 |
|
| 740 |
# 第一步:过滤掉不符合基本条件的空间
|
| 741 |
basic_available_spaces = []
|
|
|
|
| 746 |
not space['is_recently_allocated_local']):
|
| 747 |
basic_available_spaces.append(space)
|
| 748 |
|
| 749 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Basic available spaces: {len(basic_available_spaces)}")
|
| 750 |
|
| 751 |
if not basic_available_spaces:
|
| 752 |
# 生成详细的错误信息
|
|
|
|
| 768 |
f"**Please try again in 1-2 minutes.**"
|
| 769 |
)
|
| 770 |
|
| 771 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] No available spaces: {error_msg}")
|
| 772 |
raise gr.Error(error_msg, duration=10)
|
| 773 |
|
| 774 |
# 第二步:选择最优空间并进行最终验证
|
| 775 |
selected_space = basic_available_spaces[0] # 已经按idle_time排序
|
| 776 |
space_name = selected_space['space_name']
|
| 777 |
|
| 778 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Preliminary selection: {space_name}")
|
| 779 |
|
| 780 |
# 第三步:最终防撞车检查 - 再次验证本地分配状态
|
| 781 |
is_local_conflict, conflicting_student = local_tracker.is_recently_allocated_locally(space_name)
|
| 782 |
if is_local_conflict:
|
| 783 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] COLLISION DETECTED! {space_name} recently allocated to {conflicting_student}")
|
| 784 |
|
| 785 |
# 寻找替代空间
|
| 786 |
alternative_spaces = [s for s in basic_available_spaces[1:]
|
|
|
|
| 789 |
if alternative_spaces:
|
| 790 |
selected_space = alternative_spaces[0]
|
| 791 |
space_name = selected_space['space_name']
|
| 792 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Using alternative space: {space_name}")
|
| 793 |
else:
|
| 794 |
error_msg = (
|
| 795 |
f"🚫 **Collision detected and no alternatives available**\n\n"
|
| 796 |
f"The system detected a potential conflict with another student's allocation.\n\n"
|
| 797 |
f"**Please try again in 10-15 seconds.**"
|
| 798 |
)
|
| 799 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] No alternatives available")
|
| 800 |
raise gr.Error(error_msg, duration=8)
|
| 801 |
|
| 802 |
# 第四步:立即记录本地分配(在写入文件之前)
|
| 803 |
local_tracker.record_local_allocation(space_name, student_id)
|
| 804 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Local allocation recorded BEFORE file write")
|
| 805 |
|
| 806 |
# 第五步:记录到文件和远程
|
| 807 |
write_success = write_allocation_record(space_name, student_id, repo.local_dir)
|
| 808 |
if write_success:
|
| 809 |
push_success = simple_push_allocation_record(repo, space_name, student_id)
|
| 810 |
if not push_success:
|
| 811 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Warning: Allocation recorded locally but not synced to remote")
|
| 812 |
else:
|
| 813 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Warning: Failed to record allocation to file")
|
| 814 |
|
| 815 |
+
# 第六步:生成结果(使用带缓存信息的状态显示)
|
| 816 |
+
status_display = create_status_display_with_cache_info(space_activity)
|
| 817 |
redirect_url = f"https://huggingface.co/spaces/CIV3283/{space_name}/?check={student_id}"
|
| 818 |
|
| 819 |
+
print(f"[select_space_with_enhanced_collision_avoidance_cached] Final allocation: {space_name} -> {student_id}")
|
| 820 |
|
| 821 |
return redirect_to_space(redirect_url, selected_space, status_display)
|
| 822 |
|
| 823 |
+
def get_cache_status():
|
| 824 |
+
"""获取缓存状态(用于调试或管理)"""
|
| 825 |
+
global activity_cache
|
| 826 |
+
if activity_cache:
|
| 827 |
+
return activity_cache.get_cache_info()
|
| 828 |
+
return {"error": "Cache not initialized"}
|
| 829 |
+
|
| 830 |
+
def force_cache_update():
|
| 831 |
+
"""强制更新缓存(用于调试或管理)"""
|
| 832 |
+
global activity_cache
|
| 833 |
+
if activity_cache:
|
| 834 |
+
activity_cache.force_update()
|
| 835 |
+
return {"status": "Cache updated"}
|
| 836 |
+
return {"error": "Cache not initialized"}
|
| 837 |
+
|
| 838 |
def redirect_to_space(redirect_url, selected_space, status_display):
|
| 839 |
"""Display redirect information with manual click option"""
|
| 840 |
|
|
|
|
| 896 |
|
| 897 |
return gr.HTML(redirect_html)
|
| 898 |
|
| 899 |
+
def load_balance_user_cached(student_id):
|
| 900 |
+
"""使用缓存的负载均衡函数"""
|
| 901 |
+
print(f"[load_balance_user_cached] Starting cached load balancing for student ID: {student_id}")
|
| 902 |
|
| 903 |
# Initialize connection to data storage
|
| 904 |
repo = init_data_storage_connection()
|
|
|
|
| 917 |
# 使用缓存版本的分析函数
|
| 918 |
space_activity = analyze_space_activity_cached(available_spaces, LOCAL_DATA_DIR)
|
| 919 |
|
| 920 |
+
# Select space with enhanced collision avoidance
|
| 921 |
+
return select_space_with_enhanced_collision_avoidance(space_activity, student_id, repo)
|
|
|
|
| 922 |
|
| 923 |
def get_url_params(request: gr.Request):
|
| 924 |
"""Extract URL parameters from request"""
|
|
|
|
| 931 |
return "Load Distributor", None
|
| 932 |
return "Load Distributor", None
|
| 933 |
|
| 934 |
+
def create_status_display_with_cache_info(space_activity):
|
| 935 |
+
"""创建包含缓存信息的状态显示"""
|
| 936 |
+
global activity_cache
|
| 937 |
+
|
| 938 |
+
# 获取缓存状态
|
| 939 |
+
cache_info = activity_cache.get_cache_info() if activity_cache else None
|
| 940 |
+
|
| 941 |
+
status_display = "📊 **Current Space Status (sorted by availability):**<br><br>"
|
| 942 |
+
|
| 943 |
+
# 显示缓存信息
|
| 944 |
+
if cache_info:
|
| 945 |
+
if cache_info['is_fresh']:
|
| 946 |
+
cache_status = f"✅ Fresh (updated {cache_info['age_minutes']:.1f} min ago)"
|
| 947 |
+
else:
|
| 948 |
+
cache_status = f"⚠️ Stale (updated {cache_info['age_minutes']:.1f} min ago)"
|
| 949 |
+
|
| 950 |
+
status_display += f"🔄 **Cache Status:** {cache_status}<br>"
|
| 951 |
+
status_display += f"📋 **Cached Spaces:** {cache_info['spaces_count']}<br><br>"
|
| 952 |
+
|
| 953 |
+
# 显示本地分配记录摘要
|
| 954 |
+
local_summary = local_tracker.get_recent_allocations_summary()
|
| 955 |
+
if local_summary:
|
| 956 |
+
status_display += "🔒 **Recent Local Allocations:**<br>"
|
| 957 |
+
for alloc in local_summary:
|
| 958 |
+
status_display += f" • {alloc}<br>"
|
| 959 |
+
status_display += "<br>"
|
| 960 |
+
|
| 961 |
+
for i, space in enumerate(space_activity, 1):
|
| 962 |
+
status_display += f"{i}. **{space['space_name']}**<br>"
|
| 963 |
+
status_display += f" • Status: {space['status']}<br>"
|
| 964 |
+
status_display += f" • Last activity: {space['last_activity_str']}<br>"
|
| 965 |
+
|
| 966 |
+
# 显示缓存状态(可选)
|
| 967 |
+
if 'cached_status' in space:
|
| 968 |
+
status_display += f" • Cache: {space['cached_status']}<br>"
|
| 969 |
+
|
| 970 |
+
status_display += "<br>"
|
| 971 |
+
|
| 972 |
+
return status_display
|
| 973 |
+
|
| 974 |
+
def handle_user_access_cached(request: gr.Request):
|
| 975 |
+
"""使用缓存的用户访问处理"""
|
| 976 |
title, check_id = get_url_params(request)
|
| 977 |
|
| 978 |
if not check_id:
|
|
|
|
| 991 |
|
| 992 |
# Valid student ID - perform cached load balancing
|
| 993 |
try:
|
| 994 |
+
result = load_balance_user_cached(check_id)
|
| 995 |
return title, result
|
| 996 |
except Exception as e:
|
| 997 |
# Handle any errors during load balancing
|