akhaliq HF Staff commited on
Commit
5c91796
·
1 Parent(s): 5f45790
Files changed (2) hide show
  1. backend_api.py +55 -8
  2. backend_deploy.py +7 -0
backend_api.py CHANGED
@@ -908,23 +908,47 @@ async def deploy(
908
  print(f"[Deploy] Request parameters - language: {request.language}, space_name: {request.space_name}, existing_repo_id: {request.existing_repo_id}")
909
 
910
  # Check for existing deployed space in this session
911
- existing_repo_id = request.existing_repo_id
912
  session_token = authorization.replace("Bearer ", "") if authorization else None
 
913
 
914
- # If no existing_repo_id provided, check session for previously deployed spaces
915
- if not existing_repo_id and session_token and session_token in user_sessions:
 
916
  session = user_sessions[session_token]
917
  deployed_spaces = session.get("deployed_spaces", [])
918
 
 
 
 
 
919
  # Find the most recent space for this language
920
  for space in reversed(deployed_spaces):
921
  if space.get("language") == request.language:
922
- existing_repo_id = space.get("repo_id")
923
- print(f"[Deploy] Found existing space for {request.language}: {existing_repo_id}")
 
 
 
 
 
 
 
 
924
  break
 
 
 
 
 
925
 
926
  # Use the standalone deployment function
927
- print(f"[Deploy] Calling deploy_to_huggingface_space with existing_repo_id: {existing_repo_id}")
 
 
 
 
 
 
928
  success, message, space_url = deploy_to_huggingface_space(
929
  code=request.code,
930
  language=request.language,
@@ -940,7 +964,9 @@ async def deploy(
940
  if success:
941
  # Extract repo_id from space_url
942
  repo_id = space_url.split("/spaces/")[-1] if space_url else None
943
- print(f"[Deploy] Success! Repo ID: {repo_id}")
 
 
944
 
945
  # Track deployed space in session for follow-up updates
946
  if session_token and session_token in user_sessions:
@@ -948,6 +974,9 @@ async def deploy(
948
  session = user_sessions[session_token]
949
  deployed_spaces = session.get("deployed_spaces", [])
950
 
 
 
 
951
  # Update or add the space
952
  space_entry = {
953
  "repo_id": repo_id,
@@ -956,11 +985,29 @@ async def deploy(
956
  }
957
 
958
  # Remove old entry for same repo_id if exists
 
959
  deployed_spaces = [s for s in deployed_spaces if s.get("repo_id") != repo_id]
 
 
 
 
 
 
 
960
  deployed_spaces.append(space_entry)
961
 
962
  session["deployed_spaces"] = deployed_spaces
963
- print(f"[Deploy] Tracked space in session: {repo_id}")
 
 
 
 
 
 
 
 
 
 
964
 
965
  return {
966
  "success": True,
 
908
  print(f"[Deploy] Request parameters - language: {request.language}, space_name: {request.space_name}, existing_repo_id: {request.existing_repo_id}")
909
 
910
  # Check for existing deployed space in this session
 
911
  session_token = authorization.replace("Bearer ", "") if authorization else None
912
+ existing_repo_id = request.existing_repo_id
913
 
914
+ # ALWAYS check session for previously deployed spaces (even if frontend provides existing_repo_id)
915
+ # This ensures we catch spaces that were deployed in this session
916
+ if session_token and session_token in user_sessions:
917
  session = user_sessions[session_token]
918
  deployed_spaces = session.get("deployed_spaces", [])
919
 
920
+ print(f"[Deploy] Checking session for existing spaces. Found {len(deployed_spaces)} deployed spaces.")
921
+ for i, space in enumerate(deployed_spaces):
922
+ print(f"[Deploy] Space {i+1}: repo_id={space.get('repo_id')}, language={space.get('language')}, timestamp={space.get('timestamp')}")
923
+
924
  # Find the most recent space for this language
925
  for space in reversed(deployed_spaces):
926
  if space.get("language") == request.language:
927
+ session_space_id = space.get("repo_id")
928
+ print(f"[Deploy] Found existing space in session for {request.language}: {session_space_id}")
929
+ # Use session space if no explicit existing_repo_id provided, OR if they match
930
+ if not existing_repo_id:
931
+ existing_repo_id = session_space_id
932
+ print(f"[Deploy] Using session space: {existing_repo_id}")
933
+ elif existing_repo_id != session_space_id:
934
+ # Frontend and session disagree - trust the session (more recent)
935
+ print(f"[Deploy] ⚠️ Frontend provided {existing_repo_id} but session has {session_space_id}. Using session.")
936
+ existing_repo_id = session_space_id
937
  break
938
+
939
+ if not existing_repo_id:
940
+ print(f"[Deploy] ⚠️ No existing space found for language: {request.language}")
941
+ else:
942
+ print(f"[Deploy] ⚠️ No session found. session_token: {session_token[:10] if session_token else 'None'}")
943
 
944
  # Use the standalone deployment function
945
+ print(f"[Deploy] ========== CALLING deploy_to_huggingface_space ==========")
946
+ print(f"[Deploy] existing_repo_id: {existing_repo_id}")
947
+ print(f"[Deploy] space_name: {request.space_name}")
948
+ print(f"[Deploy] language: {request.language}")
949
+ print(f"[Deploy] username: {auth.username}")
950
+ print(f"[Deploy] ==========================================================")
951
+
952
  success, message, space_url = deploy_to_huggingface_space(
953
  code=request.code,
954
  language=request.language,
 
964
  if success:
965
  # Extract repo_id from space_url
966
  repo_id = space_url.split("/spaces/")[-1] if space_url else None
967
+ print(f"[Deploy] Success! Repo ID: {repo_id}")
968
+ print(f"[Deploy] Space URL: {space_url}")
969
+ print(f"[Deploy] Message: {message}")
970
 
971
  # Track deployed space in session for follow-up updates
972
  if session_token and session_token in user_sessions:
 
974
  session = user_sessions[session_token]
975
  deployed_spaces = session.get("deployed_spaces", [])
976
 
977
+ print(f"[Deploy] 📝 Tracking space in session...")
978
+ print(f"[Deploy] Current deployed_spaces count: {len(deployed_spaces)}")
979
+
980
  # Update or add the space
981
  space_entry = {
982
  "repo_id": repo_id,
 
985
  }
986
 
987
  # Remove old entry for same repo_id if exists
988
+ old_count = len(deployed_spaces)
989
  deployed_spaces = [s for s in deployed_spaces if s.get("repo_id") != repo_id]
990
+ if old_count != len(deployed_spaces):
991
+ print(f"[Deploy] Removed old entry for {repo_id}")
992
+
993
+ # Also remove old entries for same language (keep only most recent per language)
994
+ # This ensures we always update the same space for a given language
995
+ deployed_spaces = [s for s in deployed_spaces if s.get("language") != request.language]
996
+
997
  deployed_spaces.append(space_entry)
998
 
999
  session["deployed_spaces"] = deployed_spaces
1000
+ print(f"[Deploy] Tracked space in session: {repo_id}")
1001
+ print(f"[Deploy] New deployed_spaces count: {len(deployed_spaces)}")
1002
+ print(f"[Deploy] All deployed spaces: {[s.get('repo_id') for s in deployed_spaces]}")
1003
+ else:
1004
+ print(f"[Deploy] ⚠️ Could not extract repo_id from space_url: {space_url}")
1005
+ else:
1006
+ if not session_token:
1007
+ print(f"[Deploy] ⚠️ No session_token provided for tracking")
1008
+ elif session_token not in user_sessions:
1009
+ print(f"[Deploy] ⚠️ Session not found: {session_token[:10]}...")
1010
+ print(f"[Deploy] Available sessions: {[k[:10] for k in list(user_sessions.keys())[:5]]}")
1011
 
1012
  return {
1013
  "success": True,
backend_deploy.py CHANGED
@@ -517,6 +517,13 @@ def deploy_to_huggingface_space(
517
  # Determine if this is an update or new deployment
518
  is_update = existing_repo_id is not None
519
 
 
 
 
 
 
 
 
520
  if is_update:
521
  # Use existing repo
522
  repo_id = existing_repo_id
 
517
  # Determine if this is an update or new deployment
518
  is_update = existing_repo_id is not None
519
 
520
+ print(f"[Deploy] ========== DEPLOYMENT DECISION ==========")
521
+ print(f"[Deploy] existing_repo_id provided: {existing_repo_id}")
522
+ print(f"[Deploy] history provided: {history is not None} (length: {len(history) if history else 0})")
523
+ print(f"[Deploy] username: {username}")
524
+ print(f"[Deploy] is_update: {is_update}")
525
+ print(f"[Deploy] ============================================")
526
+
527
  if is_update:
528
  # Use existing repo
529
  repo_id = existing_repo_id