Spaces:
Running
Running
fix transformers js deploy
Browse files- backend_deploy.py +97 -27
backend_deploy.py
CHANGED
|
@@ -486,30 +486,37 @@ def deploy_to_huggingface_space(
|
|
| 486 |
try:
|
| 487 |
from huggingface_hub import duplicate_space
|
| 488 |
|
| 489 |
-
# duplicate_space expects just the space name
|
| 490 |
-
|
| 491 |
-
|
|
|
|
| 492 |
from_id="static-templates/transformers.js",
|
| 493 |
-
to_id=space_name,
|
| 494 |
token=token,
|
| 495 |
exist_ok=True
|
| 496 |
)
|
| 497 |
-
print(f"[Deploy] Template duplication result: {
|
| 498 |
except Exception as e:
|
| 499 |
-
#
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
else:
|
| 514 |
# For updates, verify we can access the existing space
|
| 515 |
try:
|
|
@@ -540,8 +547,65 @@ def deploy_to_huggingface_space(
|
|
| 540 |
commit_message = "Update from anycoder" if is_update else "Deploy from anycoder"
|
| 541 |
|
| 542 |
try:
|
| 543 |
-
if
|
| 544 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
import time
|
| 546 |
|
| 547 |
# Get list of files to upload from temp directory
|
|
@@ -564,19 +628,18 @@ def deploy_to_huggingface_space(
|
|
| 564 |
if not file_path.exists():
|
| 565 |
return False, f"Failed to upload: {filename} not found", None
|
| 566 |
|
| 567 |
-
# Upload with retry logic
|
| 568 |
success = False
|
| 569 |
last_error = None
|
| 570 |
|
| 571 |
for attempt in range(max_attempts):
|
| 572 |
try:
|
| 573 |
-
#
|
| 574 |
api.upload_file(
|
| 575 |
path_or_fileobj=str(file_path),
|
| 576 |
path_in_repo=filename,
|
| 577 |
repo_id=repo_id,
|
| 578 |
repo_type="space"
|
| 579 |
-
# NO commit_message - HF API handles this automatically for spaces
|
| 580 |
)
|
| 581 |
success = True
|
| 582 |
print(f"[Deploy] Successfully uploaded {filename}")
|
|
@@ -594,14 +657,12 @@ def deploy_to_huggingface_space(
|
|
| 594 |
if not success:
|
| 595 |
return False, f"Failed to upload {filename} after {max_attempts} attempts: {last_error}", None
|
| 596 |
else:
|
| 597 |
-
# For other languages, use upload_folder
|
| 598 |
-
# MATCH GRADIO: No commit_message for spaces
|
| 599 |
print(f"[Deploy] Uploading folder to {repo_id}")
|
| 600 |
api.upload_folder(
|
| 601 |
folder_path=str(temp_path),
|
| 602 |
repo_id=repo_id,
|
| 603 |
repo_type="space"
|
| 604 |
-
# NO commit_message - HF API handles this automatically for spaces
|
| 605 |
)
|
| 606 |
except Exception as e:
|
| 607 |
return False, f"Failed to upload files: {str(e)}", None
|
|
@@ -618,6 +679,15 @@ def deploy_to_huggingface_space(
|
|
| 618 |
# Don't fail deployment if README modification fails
|
| 619 |
print(f"Warning: Could not add anycoder tag to README: {e}")
|
| 620 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 622 |
action = "Updated" if is_update else "Deployed"
|
| 623 |
return True, f"β
{action} successfully to {repo_id}!", space_url
|
|
|
|
| 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 |
+
print(f"[Deploy] Attempting to duplicate template space to: {space_name.strip()}")
|
| 492 |
+
duplicated_repo = duplicate_space(
|
| 493 |
from_id="static-templates/transformers.js",
|
| 494 |
+
to_id=space_name.strip(),
|
| 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:
|
| 503 |
+
# For RepoUrl object issues, check if the space was actually created successfully
|
| 504 |
+
print(f"[Deploy] RepoUrl error detected, checking if space was created: {error_msg}")
|
| 505 |
+
try:
|
| 506 |
+
# Check if space exists by trying to access it
|
| 507 |
+
space_info = api.space_info(repo_id)
|
| 508 |
+
if space_info:
|
| 509 |
+
print(f"[Deploy] Space exists despite RepoUrl error, continuing with deployment")
|
| 510 |
+
else:
|
| 511 |
+
return False, f"Failed to create transformers.js space: {str(e)}", None
|
| 512 |
+
except Exception as check_error:
|
| 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
|
| 522 |
try:
|
|
|
|
| 547 |
commit_message = "Update from anycoder" if is_update else "Deploy from anycoder"
|
| 548 |
|
| 549 |
try:
|
| 550 |
+
if language == "transformers.js":
|
| 551 |
+
# Special handling for transformers.js - create NEW temp files for each upload
|
| 552 |
+
# This matches the working pattern in ui.py
|
| 553 |
+
import time
|
| 554 |
+
|
| 555 |
+
# Get the parsed files from earlier
|
| 556 |
+
files_to_upload = [
|
| 557 |
+
("index.html", files.get('index.html')),
|
| 558 |
+
("index.js", files.get('index.js')),
|
| 559 |
+
("style.css", files.get('style.css'))
|
| 560 |
+
]
|
| 561 |
+
|
| 562 |
+
max_attempts = 3
|
| 563 |
+
for file_name, file_content in files_to_upload:
|
| 564 |
+
if not file_content:
|
| 565 |
+
return False, f"Missing content for {file_name}", None
|
| 566 |
+
|
| 567 |
+
success = False
|
| 568 |
+
last_error = None
|
| 569 |
+
|
| 570 |
+
for attempt in range(max_attempts):
|
| 571 |
+
temp_file_path = None
|
| 572 |
+
try:
|
| 573 |
+
# Create a NEW temp file for this upload (key difference from old approach)
|
| 574 |
+
with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False, encoding='utf-8') as f:
|
| 575 |
+
f.write(file_content)
|
| 576 |
+
temp_file_path = f.name
|
| 577 |
+
|
| 578 |
+
# Upload the file without commit_message (HF handles this for spaces)
|
| 579 |
+
api.upload_file(
|
| 580 |
+
path_or_fileobj=temp_file_path,
|
| 581 |
+
path_in_repo=file_name,
|
| 582 |
+
repo_id=repo_id,
|
| 583 |
+
repo_type="space"
|
| 584 |
+
)
|
| 585 |
+
success = True
|
| 586 |
+
print(f"[Deploy] Successfully uploaded {file_name}")
|
| 587 |
+
break
|
| 588 |
+
|
| 589 |
+
except Exception as e:
|
| 590 |
+
last_error = e
|
| 591 |
+
error_str = str(e)
|
| 592 |
+
print(f"[Deploy] Upload error for {file_name}: {error_str}")
|
| 593 |
+
if "403" in error_str or "Forbidden" in error_str:
|
| 594 |
+
return False, f"Permission denied uploading {file_name}. Check your token has write access to {repo_id}.", None
|
| 595 |
+
|
| 596 |
+
if attempt < max_attempts - 1:
|
| 597 |
+
time.sleep(2) # Wait before retry
|
| 598 |
+
print(f"[Deploy] Retry {attempt + 1}/{max_attempts} for {file_name}")
|
| 599 |
+
finally:
|
| 600 |
+
# Clean up temp file
|
| 601 |
+
if temp_file_path and os.path.exists(temp_file_path):
|
| 602 |
+
os.unlink(temp_file_path)
|
| 603 |
+
|
| 604 |
+
if not success:
|
| 605 |
+
return False, f"Failed to upload {file_name} after {max_attempts} attempts: {last_error}", None
|
| 606 |
+
|
| 607 |
+
elif use_individual_uploads:
|
| 608 |
+
# For React, Streamlit: upload each file individually
|
| 609 |
import time
|
| 610 |
|
| 611 |
# Get list of files to upload from temp directory
|
|
|
|
| 628 |
if not file_path.exists():
|
| 629 |
return False, f"Failed to upload: {filename} not found", None
|
| 630 |
|
| 631 |
+
# Upload with retry logic
|
| 632 |
success = False
|
| 633 |
last_error = None
|
| 634 |
|
| 635 |
for attempt in range(max_attempts):
|
| 636 |
try:
|
| 637 |
+
# Upload without commit_message - HF API handles this for spaces
|
| 638 |
api.upload_file(
|
| 639 |
path_or_fileobj=str(file_path),
|
| 640 |
path_in_repo=filename,
|
| 641 |
repo_id=repo_id,
|
| 642 |
repo_type="space"
|
|
|
|
| 643 |
)
|
| 644 |
success = True
|
| 645 |
print(f"[Deploy] Successfully uploaded {filename}")
|
|
|
|
| 657 |
if not success:
|
| 658 |
return False, f"Failed to upload {filename} after {max_attempts} attempts: {last_error}", None
|
| 659 |
else:
|
| 660 |
+
# For other languages, use upload_folder
|
|
|
|
| 661 |
print(f"[Deploy] Uploading folder to {repo_id}")
|
| 662 |
api.upload_folder(
|
| 663 |
folder_path=str(temp_path),
|
| 664 |
repo_id=repo_id,
|
| 665 |
repo_type="space"
|
|
|
|
| 666 |
)
|
| 667 |
except Exception as e:
|
| 668 |
return False, f"Failed to upload files: {str(e)}", None
|
|
|
|
| 679 |
# Don't fail deployment if README modification fails
|
| 680 |
print(f"Warning: Could not add anycoder tag to README: {e}")
|
| 681 |
|
| 682 |
+
# For transformers.js updates, trigger a space restart to ensure changes take effect
|
| 683 |
+
if is_update and language == "transformers.js":
|
| 684 |
+
try:
|
| 685 |
+
api.restart_space(repo_id=repo_id)
|
| 686 |
+
print(f"[Deploy] Restarted space after update: {repo_id}")
|
| 687 |
+
except Exception as restart_error:
|
| 688 |
+
# Don't fail the deployment if restart fails, just log it
|
| 689 |
+
print(f"Note: Could not restart space after update: {restart_error}")
|
| 690 |
+
|
| 691 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 692 |
action = "Updated" if is_update else "Deployed"
|
| 693 |
return True, f"β
{action} successfully to {repo_id}!", space_url
|