Spaces:
Sleeping
Sleeping
Joseph Pollack
commited on
adds repo creation with fallback , skip creation
Browse files- scripts/deploy_demo_space.py +81 -10
scripts/deploy_demo_space.py
CHANGED
|
@@ -483,13 +483,13 @@ os.environ['BRAND_PROJECT_URL'] = json.dumps({_json.dumps(self.brand_project_url
|
|
| 483 |
# Try multiple CLI variants depending on installed version
|
| 484 |
cli_attempts = [
|
| 485 |
["huggingface-cli", "repo", "create", self.space_id, "--repo-type", "space", "--space_sdk", "gradio"],
|
| 486 |
-
["hf", "repo", "create", self.space_id, "--repo-type", "space", "--
|
| 487 |
-
["huggingface-cli", "repo", "create", self.space_id, "--repo-type", "space"],
|
| 488 |
-
["hf", "repo", "create", self.space_id, "--repo-type", "space"],
|
| 489 |
]
|
| 490 |
|
| 491 |
last_err = None
|
| 492 |
-
for cmd in cli_attempts:
|
| 493 |
logger.info(f"Running command: {' '.join(cmd)}")
|
| 494 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 495 |
if result.returncode == 0:
|
|
@@ -497,10 +497,15 @@ os.environ['BRAND_PROJECT_URL'] = json.dumps({_json.dumps(self.brand_project_url
|
|
| 497 |
break
|
| 498 |
else:
|
| 499 |
last_err = result.stderr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
logger.warning(f"CLI attempt failed: {last_err}")
|
| 501 |
else:
|
| 502 |
logger.error(f"β Failed to create space via CLI: {last_err}")
|
| 503 |
-
|
|
|
|
| 504 |
|
| 505 |
# Verify the space exists and is recognized as a space
|
| 506 |
try:
|
|
@@ -520,6 +525,57 @@ os.environ['BRAND_PROJECT_URL'] = json.dumps({_json.dumps(self.brand_project_url
|
|
| 520 |
|
| 521 |
except Exception as e:
|
| 522 |
logger.error(f"β Error creating space with CLI: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
return False
|
| 524 |
|
| 525 |
def prepare_space_files(self) -> str:
|
|
@@ -827,7 +883,7 @@ os.environ['BRAND_PROJECT_URL'] = json.dumps({_json.dumps(self.brand_project_url
|
|
| 827 |
logger.error(f"β Error testing space: {e}")
|
| 828 |
return False
|
| 829 |
|
| 830 |
-
def deploy(self) -> bool:
|
| 831 |
"""Main deployment method"""
|
| 832 |
logger.info(f"π Starting demo space deployment for {self.model_id}")
|
| 833 |
|
|
@@ -835,9 +891,23 @@ os.environ['BRAND_PROJECT_URL'] = json.dumps({_json.dumps(self.brand_project_url
|
|
| 835 |
if not self.validate_model_exists():
|
| 836 |
return False
|
| 837 |
|
| 838 |
-
# Step 2: Create space repository
|
| 839 |
-
if not
|
| 840 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 841 |
|
| 842 |
# Step 3: Prepare files
|
| 843 |
temp_dir = self.prepare_space_files()
|
|
@@ -904,6 +974,7 @@ def main():
|
|
| 904 |
parser.add_argument("--brand-gh-url", help="Custom GitHub link URL (defaults to https://github.com/<org>)")
|
| 905 |
parser.add_argument("--brand-project-name", help="Project name to link in Join Us")
|
| 906 |
parser.add_argument("--brand-project-url", help="Project URL to link in Join Us")
|
|
|
|
| 907 |
|
| 908 |
args = parser.parse_args()
|
| 909 |
|
|
@@ -932,7 +1003,7 @@ def main():
|
|
| 932 |
brand_project_url=args.brand_project_url,
|
| 933 |
)
|
| 934 |
|
| 935 |
-
success = deployer.deploy()
|
| 936 |
|
| 937 |
if success:
|
| 938 |
print("\nβ
Deployment successful!")
|
|
|
|
| 483 |
# Try multiple CLI variants depending on installed version
|
| 484 |
cli_attempts = [
|
| 485 |
["huggingface-cli", "repo", "create", self.space_id, "--repo-type", "space", "--space_sdk", "gradio"],
|
| 486 |
+
["hf", "repo", "create", self.space_id, "--repo-type", "space", "--space_sdk", "gradio"],
|
| 487 |
+
["huggingface-cli", "repo", "create", self.space_id, "--repo-type", "space", "--space_sdk", "gradio", "--exist-ok"],
|
| 488 |
+
["hf", "repo", "create", self.space_id, "--repo-type", "space", "--space_sdk", "gradio", "--exist-ok"],
|
| 489 |
]
|
| 490 |
|
| 491 |
last_err = None
|
| 492 |
+
for attempt, cmd in enumerate(cli_attempts):
|
| 493 |
logger.info(f"Running command: {' '.join(cmd)}")
|
| 494 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 495 |
if result.returncode == 0:
|
|
|
|
| 497 |
break
|
| 498 |
else:
|
| 499 |
last_err = result.stderr
|
| 500 |
+
if "429" in str(result.stderr) or "Too Many Requests" in str(result.stderr):
|
| 501 |
+
logger.warning(f"Rate limited on attempt {attempt + 1}. Waiting 30 seconds before next attempt...")
|
| 502 |
+
time.sleep(30)
|
| 503 |
+
continue
|
| 504 |
logger.warning(f"CLI attempt failed: {last_err}")
|
| 505 |
else:
|
| 506 |
logger.error(f"β Failed to create space via CLI: {last_err}")
|
| 507 |
+
logger.info("π Attempting manual space creation fallback...")
|
| 508 |
+
return self._manual_space_creation_fallback()
|
| 509 |
|
| 510 |
# Verify the space exists and is recognized as a space
|
| 511 |
try:
|
|
|
|
| 525 |
|
| 526 |
except Exception as e:
|
| 527 |
logger.error(f"β Error creating space with CLI: {e}")
|
| 528 |
+
return self._manual_space_creation_fallback()
|
| 529 |
+
|
| 530 |
+
def _manual_space_creation_fallback(self) -> bool:
|
| 531 |
+
"""Manual fallback when all automated methods fail due to rate limiting"""
|
| 532 |
+
try:
|
| 533 |
+
logger.info("π Manual Space Creation Instructions:")
|
| 534 |
+
logger.info(f" Space ID: {self.space_id}")
|
| 535 |
+
logger.info(f" Space URL: {self.space_url}")
|
| 536 |
+
logger.info(f" Model ID: {self.model_id}")
|
| 537 |
+
logger.info(f" Demo Type: {self.demo_type}")
|
| 538 |
+
|
| 539 |
+
logger.info("\nπ§ To create the space manually:")
|
| 540 |
+
logger.info("1. Go to https://huggingface.co/new-space")
|
| 541 |
+
logger.info(f"2. Set Space name to: {self.space_name}")
|
| 542 |
+
logger.info("3. Select 'Gradio' as the SDK")
|
| 543 |
+
logger.info("4. Set to Public")
|
| 544 |
+
logger.info("5. Click 'Create Space'")
|
| 545 |
+
logger.info("\nπ After creating the space, run this command to upload files:")
|
| 546 |
+
logger.info(f" python scripts/deploy_demo_space.py --hf-token <token> --hf-username {self.hf_username} --model-id {self.model_id} --demo-type {self.demo_type} --space-name {self.space_name} --skip-creation")
|
| 547 |
+
|
| 548 |
+
# Check if space exists (user might have created it manually)
|
| 549 |
+
try:
|
| 550 |
+
if HF_HUB_AVAILABLE:
|
| 551 |
+
info = self.api.repo_info(self.space_id, repo_type="space")
|
| 552 |
+
if info:
|
| 553 |
+
logger.info("β
Space found! Proceeding with file upload...")
|
| 554 |
+
return True
|
| 555 |
+
except Exception:
|
| 556 |
+
pass
|
| 557 |
+
|
| 558 |
+
logger.info("\nβ³ Waiting for manual space creation...")
|
| 559 |
+
logger.info(" (The script will continue once the space is created)")
|
| 560 |
+
|
| 561 |
+
# Wait for user to create space manually
|
| 562 |
+
for i in range(30): # Wait up to 5 minutes
|
| 563 |
+
try:
|
| 564 |
+
if HF_HUB_AVAILABLE:
|
| 565 |
+
info = self.api.repo_info(self.space_id, repo_type="space")
|
| 566 |
+
if info:
|
| 567 |
+
logger.info("β
Space created manually! Proceeding...")
|
| 568 |
+
return True
|
| 569 |
+
except Exception:
|
| 570 |
+
pass
|
| 571 |
+
logger.info(f" Waiting... ({i+1}/30)")
|
| 572 |
+
time.sleep(10)
|
| 573 |
+
|
| 574 |
+
logger.error("β Space not created within timeout period")
|
| 575 |
+
return False
|
| 576 |
+
|
| 577 |
+
except Exception as e:
|
| 578 |
+
logger.error(f"β Error in manual fallback: {e}")
|
| 579 |
return False
|
| 580 |
|
| 581 |
def prepare_space_files(self) -> str:
|
|
|
|
| 883 |
logger.error(f"β Error testing space: {e}")
|
| 884 |
return False
|
| 885 |
|
| 886 |
+
def deploy(self, skip_creation: bool = False) -> bool:
|
| 887 |
"""Main deployment method"""
|
| 888 |
logger.info(f"π Starting demo space deployment for {self.model_id}")
|
| 889 |
|
|
|
|
| 891 |
if not self.validate_model_exists():
|
| 892 |
return False
|
| 893 |
|
| 894 |
+
# Step 2: Create space repository (skip if requested)
|
| 895 |
+
if not skip_creation:
|
| 896 |
+
if not self.create_space_repository():
|
| 897 |
+
return False
|
| 898 |
+
else:
|
| 899 |
+
logger.info("βοΈ Skipping space creation (--skip-creation flag)")
|
| 900 |
+
# Verify space exists
|
| 901 |
+
try:
|
| 902 |
+
if HF_HUB_AVAILABLE:
|
| 903 |
+
info = self.api.repo_info(self.space_id, repo_type="space")
|
| 904 |
+
if not info:
|
| 905 |
+
logger.error(f"β Space {self.space_id} not found. Please create it first.")
|
| 906 |
+
return False
|
| 907 |
+
logger.info(f"β
Found existing space: {self.space_url}")
|
| 908 |
+
except Exception as e:
|
| 909 |
+
logger.error(f"β Error verifying space: {e}")
|
| 910 |
+
return False
|
| 911 |
|
| 912 |
# Step 3: Prepare files
|
| 913 |
temp_dir = self.prepare_space_files()
|
|
|
|
| 974 |
parser.add_argument("--brand-gh-url", help="Custom GitHub link URL (defaults to https://github.com/<org>)")
|
| 975 |
parser.add_argument("--brand-project-name", help="Project name to link in Join Us")
|
| 976 |
parser.add_argument("--brand-project-url", help="Project URL to link in Join Us")
|
| 977 |
+
parser.add_argument("--skip-creation", action="store_true", help="Skip space creation and only upload files (for manual space creation)")
|
| 978 |
|
| 979 |
args = parser.parse_args()
|
| 980 |
|
|
|
|
| 1003 |
brand_project_url=args.brand_project_url,
|
| 1004 |
)
|
| 1005 |
|
| 1006 |
+
success = deployer.deploy(skip_creation=getattr(args, 'skip_creation', False))
|
| 1007 |
|
| 1008 |
if success:
|
| 1009 |
print("\nβ
Deployment successful!")
|