Spaces:
Paused
Paused
Update backend_deploy.py
Browse files- backend_deploy.py +30 -35
backend_deploy.py
CHANGED
|
@@ -504,57 +504,54 @@ def deploy_to_huggingface_space(
|
|
| 504 |
commit_message: Optional[str] = None,
|
| 505 |
history: Optional[List[Dict]] = None
|
| 506 |
) -> Tuple[bool, str, Optional[str]]:
|
| 507 |
-
"""
|
| 508 |
-
Deploy code to HuggingFace Spaces (create new or update existing)
|
| 509 |
-
|
| 510 |
-
Args:
|
| 511 |
-
code: Generated code to deploy
|
| 512 |
-
language: Target language/framework (html, gradio, streamlit, react, transformers.js, comfyui)
|
| 513 |
-
space_name: Name for the space (auto-generated if None, ignored if existing_repo_id provided)
|
| 514 |
-
token: HuggingFace API token
|
| 515 |
-
username: HuggingFace username
|
| 516 |
-
description: Space description
|
| 517 |
-
private: Whether to make the space private (only for new spaces)
|
| 518 |
-
existing_repo_id: If provided (username/space-name), updates this space instead of creating new one
|
| 519 |
-
commit_message: Custom commit message (defaults to "Deploy from anycoder" or "Update from anycoder")
|
| 520 |
-
history: Chat history (list of dicts with 'role' and 'content') - used to detect followups/imports
|
| 521 |
|
| 522 |
-
Returns:
|
| 523 |
-
Tuple of (success: bool, message: str, space_url: Optional[str])
|
| 524 |
-
"""
|
| 525 |
if not token:
|
| 526 |
token = os.getenv("HF_TOKEN")
|
|
|
|
| 527 |
if not token:
|
| 528 |
return False, "No HuggingFace token provided", None
|
| 529 |
|
| 530 |
try:
|
| 531 |
api = HfApi(token=token)
|
| 532 |
|
| 533 |
-
# Get username if not provided (needed for history tracking)
|
| 534 |
if not username:
|
| 535 |
try:
|
| 536 |
user_info = api.whoami()
|
| 537 |
username = user_info.get("name") or user_info.get("preferred_username") or "user"
|
|
|
|
| 538 |
except Exception as e:
|
| 539 |
-
|
| 540 |
|
| 541 |
-
# Check history for existing space if not explicitly provided
|
| 542 |
-
# This enables automatic updates for followup prompts and imported spaces
|
| 543 |
if not existing_repo_id and history:
|
| 544 |
existing_repo_id = extract_space_id_from_history(history, username)
|
| 545 |
if existing_repo_id:
|
| 546 |
-
print(f"[
|
| 547 |
|
| 548 |
-
# Determine if this is an update or new deployment
|
| 549 |
is_update = existing_repo_id is not None
|
|
|
|
| 550 |
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
print(f"[
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
|
| 559 |
# For React space updates (followup changes), handle SEARCH/REPLACE blocks
|
| 560 |
if is_update and language == "react":
|
|
@@ -835,11 +832,9 @@ def deploy_to_huggingface_space(
|
|
| 835 |
use_individual_uploads = True
|
| 836 |
|
| 837 |
except Exception as e:
|
| 838 |
-
print(f"[
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
return False, f"Error parsing transformers.js output: {str(e)}", None
|
| 842 |
-
|
| 843 |
elif language == "html":
|
| 844 |
html_code = parse_html_code(code)
|
| 845 |
(temp_path / "index.html").write_text(html_code, encoding='utf-8')
|
|
|
|
| 504 |
commit_message: Optional[str] = None,
|
| 505 |
history: Optional[List[Dict]] = None
|
| 506 |
) -> Tuple[bool, str, Optional[str]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 507 |
|
|
|
|
|
|
|
|
|
|
| 508 |
if not token:
|
| 509 |
token = os.getenv("HF_TOKEN")
|
| 510 |
+
print(f"[DEBUG] Token check: {'FOUND' if token else 'MISSING'}")
|
| 511 |
if not token:
|
| 512 |
return False, "No HuggingFace token provided", None
|
| 513 |
|
| 514 |
try:
|
| 515 |
api = HfApi(token=token)
|
| 516 |
|
|
|
|
| 517 |
if not username:
|
| 518 |
try:
|
| 519 |
user_info = api.whoami()
|
| 520 |
username = user_info.get("name") or user_info.get("preferred_username") or "user"
|
| 521 |
+
print(f"[DEBUG] Identified username: {username}")
|
| 522 |
except Exception as e:
|
| 523 |
+
print(f"[DEBUG] Whoami error: {e}")
|
| 524 |
|
|
|
|
|
|
|
| 525 |
if not existing_repo_id and history:
|
| 526 |
existing_repo_id = extract_space_id_from_history(history, username)
|
| 527 |
if existing_repo_id:
|
| 528 |
+
print(f"[DEBUG] Detected existing space from history: {existing_repo_id}")
|
| 529 |
|
|
|
|
| 530 |
is_update = existing_repo_id is not None
|
| 531 |
+
print(f"[DEBUG] Deployment Mode: {'UPDATE' if is_update else 'NEW'}")
|
| 532 |
|
| 533 |
+
# --- Logic xử lý SDK ---
|
| 534 |
+
sdk = detect_sdk_from_code(code, language)
|
| 535 |
+
print(f"[DEBUG] Detected SDK: {sdk} for language: {language}")
|
| 536 |
+
|
| 537 |
+
if not is_update and sdk != "docker" and language not in ["transformers.js"]:
|
| 538 |
+
repo_id = f"{username}/{space_name or f'anycoder-{uuid.uuid4().hex[:8]}'}"
|
| 539 |
+
print(f"[DEBUG] Attempting api.create_repo for: {repo_id}")
|
| 540 |
+
try:
|
| 541 |
+
api.create_repo(
|
| 542 |
+
repo_id=repo_id,
|
| 543 |
+
repo_type="space",
|
| 544 |
+
space_sdk=sdk,
|
| 545 |
+
private=private,
|
| 546 |
+
exist_ok=True
|
| 547 |
+
)
|
| 548 |
+
print(f"[DEBUG] Successfully created repo: {repo_id}")
|
| 549 |
+
except Exception as e:
|
| 550 |
+
print(f"[DEBUG] FAILED to create repo: {str(e)}")
|
| 551 |
+
return False, f"Failed to create space: {str(e)}", None
|
| 552 |
+
else:
|
| 553 |
+
repo_id = existing_repo_id
|
| 554 |
+
print(f"[DEBUG] Target repo ID: {repo_id}")
|
| 555 |
|
| 556 |
# For React space updates (followup changes), handle SEARCH/REPLACE blocks
|
| 557 |
if is_update and language == "react":
|
|
|
|
| 832 |
use_individual_uploads = True
|
| 833 |
|
| 834 |
except Exception as e:
|
| 835 |
+
print(f"[CRITICAL_ERROR] {str(e)}")
|
| 836 |
+
return False, f"Deployment error: {str(e)}", None
|
| 837 |
+
|
|
|
|
|
|
|
| 838 |
elif language == "html":
|
| 839 |
html_code = parse_html_code(code)
|
| 840 |
(temp_path / "index.html").write_text(html_code, encoding='utf-8')
|