Spaces:
Running
Running
fix
Browse files- backend_deploy.py +26 -7
backend_deploy.py
CHANGED
|
@@ -329,15 +329,22 @@ def deploy_to_huggingface_space(
|
|
| 329 |
except Exception as e:
|
| 330 |
return False, f"Failed to get user info: {str(e)}", None
|
| 331 |
|
| 332 |
-
# Generate space name if not provided
|
| 333 |
-
if not space_name:
|
| 334 |
space_name = f"anycoder-{uuid.uuid4().hex[:8]}"
|
|
|
|
| 335 |
|
| 336 |
# Clean space name (no spaces, lowercase, alphanumeric + hyphens)
|
| 337 |
space_name = re.sub(r'[^a-z0-9-]', '-', space_name.lower())
|
| 338 |
space_name = re.sub(r'-+', '-', space_name).strip('-')
|
| 339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
repo_id = f"{username}/{space_name}"
|
|
|
|
| 341 |
|
| 342 |
# Detect SDK
|
| 343 |
sdk = detect_sdk_from_code(code, language)
|
|
@@ -483,20 +490,32 @@ def deploy_to_huggingface_space(
|
|
| 483 |
if language == "transformers.js":
|
| 484 |
if not is_update:
|
| 485 |
print(f"[Deploy] Creating NEW transformers.js space via template duplication")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
try:
|
| 487 |
from huggingface_hub import duplicate_space
|
| 488 |
|
| 489 |
# duplicate_space expects just the space name (not full repo_id)
|
| 490 |
# Use strip() to clean the space name
|
| 491 |
-
|
|
|
|
|
|
|
| 492 |
duplicated_repo = duplicate_space(
|
| 493 |
from_id="static-templates/transformers.js",
|
| 494 |
-
to_id=
|
| 495 |
token=token,
|
| 496 |
exist_ok=True
|
| 497 |
)
|
| 498 |
print(f"[Deploy] Template duplication result: {duplicated_repo} (type: {type(duplicated_repo)})")
|
| 499 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
# Handle potential RepoUrl object errors
|
| 501 |
error_msg = str(e)
|
| 502 |
if "'url'" in error_msg or "RepoUrl" in error_msg:
|
|
@@ -513,9 +532,6 @@ def deploy_to_huggingface_space(
|
|
| 513 |
return False, f"Failed to create transformers.js space: {str(e)}", None
|
| 514 |
else:
|
| 515 |
# Other errors - report them
|
| 516 |
-
print(f"[Deploy] Template duplication failed: {e}")
|
| 517 |
-
import traceback
|
| 518 |
-
traceback.print_exc()
|
| 519 |
return False, f"Failed to create transformers.js space: {str(e)}", None
|
| 520 |
else:
|
| 521 |
# For updates, verify we can access the existing space
|
|
@@ -693,6 +709,9 @@ def deploy_to_huggingface_space(
|
|
| 693 |
return True, f"✅ {action} successfully to {repo_id}!", space_url
|
| 694 |
|
| 695 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 696 |
return False, f"Deployment error: {str(e)}", None
|
| 697 |
|
| 698 |
|
|
|
|
| 329 |
except Exception as e:
|
| 330 |
return False, f"Failed to get user info: {str(e)}", None
|
| 331 |
|
| 332 |
+
# Generate space name if not provided or empty
|
| 333 |
+
if not space_name or space_name.strip() == "":
|
| 334 |
space_name = f"anycoder-{uuid.uuid4().hex[:8]}"
|
| 335 |
+
print(f"[Deploy] Auto-generated space name: {space_name}")
|
| 336 |
|
| 337 |
# Clean space name (no spaces, lowercase, alphanumeric + hyphens)
|
| 338 |
space_name = re.sub(r'[^a-z0-9-]', '-', space_name.lower())
|
| 339 |
space_name = re.sub(r'-+', '-', space_name).strip('-')
|
| 340 |
|
| 341 |
+
# Ensure space_name is not empty after cleaning
|
| 342 |
+
if not space_name:
|
| 343 |
+
space_name = f"anycoder-{uuid.uuid4().hex[:8]}"
|
| 344 |
+
print(f"[Deploy] Space name was empty after cleaning, regenerated: {space_name}")
|
| 345 |
+
|
| 346 |
repo_id = f"{username}/{space_name}"
|
| 347 |
+
print(f"[Deploy] Using repo_id: {repo_id}")
|
| 348 |
|
| 349 |
# Detect SDK
|
| 350 |
sdk = detect_sdk_from_code(code, language)
|
|
|
|
| 490 |
if language == "transformers.js":
|
| 491 |
if not is_update:
|
| 492 |
print(f"[Deploy] Creating NEW transformers.js space via template duplication")
|
| 493 |
+
print(f"[Deploy] space_name value: '{space_name}' (type: {type(space_name)})")
|
| 494 |
+
|
| 495 |
+
# Safety check for space_name
|
| 496 |
+
if not space_name:
|
| 497 |
+
return False, "Internal error: space_name is None after generation", None
|
| 498 |
+
|
| 499 |
try:
|
| 500 |
from huggingface_hub import duplicate_space
|
| 501 |
|
| 502 |
# duplicate_space expects just the space name (not full repo_id)
|
| 503 |
# Use strip() to clean the space name
|
| 504 |
+
clean_space_name = space_name.strip()
|
| 505 |
+
print(f"[Deploy] Attempting to duplicate template space to: {clean_space_name}")
|
| 506 |
+
|
| 507 |
duplicated_repo = duplicate_space(
|
| 508 |
from_id="static-templates/transformers.js",
|
| 509 |
+
to_id=clean_space_name,
|
| 510 |
token=token,
|
| 511 |
exist_ok=True
|
| 512 |
)
|
| 513 |
print(f"[Deploy] Template duplication result: {duplicated_repo} (type: {type(duplicated_repo)})")
|
| 514 |
except Exception as e:
|
| 515 |
+
print(f"[Deploy] Exception during duplicate_space: {type(e).__name__}: {str(e)}")
|
| 516 |
+
import traceback
|
| 517 |
+
traceback.print_exc()
|
| 518 |
+
|
| 519 |
# Handle potential RepoUrl object errors
|
| 520 |
error_msg = str(e)
|
| 521 |
if "'url'" in error_msg or "RepoUrl" in error_msg:
|
|
|
|
| 532 |
return False, f"Failed to create transformers.js space: {str(e)}", None
|
| 533 |
else:
|
| 534 |
# Other errors - report them
|
|
|
|
|
|
|
|
|
|
| 535 |
return False, f"Failed to create transformers.js space: {str(e)}", None
|
| 536 |
else:
|
| 537 |
# For updates, verify we can access the existing space
|
|
|
|
| 709 |
return True, f"✅ {action} successfully to {repo_id}!", space_url
|
| 710 |
|
| 711 |
except Exception as e:
|
| 712 |
+
print(f"[Deploy] Top-level exception caught: {type(e).__name__}: {str(e)}")
|
| 713 |
+
import traceback
|
| 714 |
+
traceback.print_exc()
|
| 715 |
return False, f"Deployment error: {str(e)}", None
|
| 716 |
|
| 717 |
|