update
Browse filesFiles changed (2)
- backend_deploy.py+51-36
- frontend/src/app/page.tsx+29-7
460 unmodified lines4614624634644654644654664674684694704714724734744754764774784794804814824834844854864874664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015021 unmodified line504505506495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524460 unmodified lines # Don't create README - HuggingFace will auto-generate it # We'll add the anycoder tag after deployment # ONLY create repo for NEW deployments (not updates!) # This matches the Gradio version logic # ALWAYS create/ensure repo exists (with exist_ok=True) # This works for both new spaces and updates! try: if language == "transformers.js" and not is_update: # For NEW transformers.js spaces, try to duplicate the template print(f"[Deploy] Creating new transformers.js space: {repo_id}") try: from huggingface_hub import duplicate_space # IMPORTANT: duplicate_space expects just the space name, not the full repo_id # It will automatically prepend the username print(f"[Deploy] Attempting to duplicate template space to: {space_name}") result = duplicate_space( from_id="static-templates/transformers.js", to_id=space_name, # Just the space name, not username/space-name token=token, exist_ok=True ) print(f"[Deploy] Template duplication result: {result}") except Exception as e: # If template duplication fails, fall back to regular create print(f"[Deploy] Template duplication failed, creating regular static space: {e}") import traceback traceback.print_exc() if not is_update: print(f"[Deploy] Creating NEW space: {repo_id}") try: if language == "transformers.js": # For NEW transformers.js spaces, try to duplicate the template print(f"[Deploy] Creating new transformers.js space: {repo_id}") try: from huggingface_hub import duplicate_space # IMPORTANT: duplicate_space expects just the space name, not the full repo_id # It will automatically prepend the username print(f"[Deploy] Attempting to duplicate template space to: {space_name}") result = duplicate_space( from_id="static-templates/transformers.js", to_id=space_name, # Just the space name, not username/space-name token=token, exist_ok=True ) print(f"[Deploy] Template duplication result: {result}") except Exception as e: # If template duplication fails, fall back to regular create print(f"[Deploy] Template duplication failed, creating regular static space: {e}") import traceback traceback.print_exc() api.create_repo( repo_id=repo_id, repo_type="space", space_sdk=sdk, private=private, exist_ok=True # Don't fail if exists ) elif sdk != "docker": # For non-Docker SDKs, create the repo normally print(f"[Deploy] Creating new {sdk} space: {repo_id}") api.create_repo( repo_id=repo_id, repo_type="space",1 unmodified line private=private, exist_ok=True # Don't fail if exists ) else: # For all other cases (new or update), ensure repo exists print(f"[Deploy] Ensuring repo exists: {repo_id} (is_update={is_update})") api.create_repo( repo_id=repo_id, repo_type="space", space_sdk=sdk, private=private if not is_update else None, # Only set private for new repos exist_ok=True # Don't fail if repo already exists ) except Exception as e: return False, f"Failed to create/access space: {str(e)}", None else: # For Docker (React/Streamlit), create_repo is handled differently print(f"[Deploy] Creating new Docker space: {repo_id}") from huggingface_hub import create_repo create_repo( repo_id=repo_id, repo_type="space", space_sdk="docker", token=token, exist_ok=True ) except Exception as e: return False, f"Failed to create space: {str(e)}", None else: print(f"[Deploy] UPDATING existing space: {repo_id} (skipping create_repo)") # Upload files if not commit_message:166 unmodified lines1671681691701711721731741751711761771781791801811821831841851861871881891901811911921931943 unmodified lines198199200191201202203204205206207208209210211212213214215216217218219204220221222207208209223224225226227228229230231232233234235236237238239166 unmodified lines let existingSpace: string | null = null; // Look for previous deployment or imported space in history console.log('[Deploy] ========== DEBUG START =========='); console.log('[Deploy] Total messages in history:', messages.length); console.log('[Deploy] Current username:', username); console.log('[Deploy] Messages:', JSON.stringify(messages, null, 2)); if (messages.length > 0 && username) { console.log('[Deploy] Scanning message history for existing deployments...'); console.log('[Deploy] Scanning message history...'); for (let i = messages.length - 1; i >= 0; i--) { const msg = messages[i]; console.log(`[Deploy] Checking message ${i}:`, { role: msg.role, contentPreview: msg.content.substring(0, 100), startsWithImported: msg.content.startsWith('Imported Space from') }); // Check for deployment messages if (msg.role === 'assistant' && msg.content.includes('β
Deployed')) { const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/); if (match) { existingSpace = match[1]; console.log('[Deploy] Found previous deployment:', existingSpace); console.log('[Deploy] β
Found "β
Deployed" message:', existingSpace); break; } }3 unmodified lines const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/); if (match) { existingSpace = match[1]; console.log('[Deploy] Found previous update:', existingSpace); console.log('[Deploy] β
Found "β
Updated" message:', existingSpace); break; } } // Check for imported space messages - THE KEY PART! if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) { console.log('[Deploy] π― Found "Imported Space from" message!'); const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/); console.log('[Deploy] Regex match result:', match); if (match) { const importedSpace = match[1]; console.log('[Deploy] Extracted space:', importedSpace); console.log('[Deploy] Username to check:', username); console.log('[Deploy] Starts with username?', importedSpace.startsWith(`${username}/`)); // Only use imported space if user owns it (can update it) if (importedSpace.startsWith(`${username}/`)) { existingSpace = importedSpace; console.log('[Deploy] Found imported space (user owns it):', existingSpace); console.log('[Deploy] β
β
β
USER OWNS THIS SPACE! Will update:', existingSpace); break; } else { console.log('[Deploy] Found imported space but user does not own it:', importedSpace); // If user doesn't own the imported space, we'll create a new one // (existingSpace remains null, triggering new deployment) console.log('[Deploy] β οΈ User does not own this space:', importedSpace); console.log('[Deploy] Expected to start with:', `${username}/`); } } else { console.log('[Deploy] β Regex did not match URL in message'); } } } console.log('[Deploy] Final existingSpace value:', existingSpace); } else { console.log('[Deploy] Skipping scan - no messages or no username'); } console.log('[Deploy] ========== DEBUG END ==========');
// Auto-generate space name (never prompt user) let spaceName = undefined; // undefined = backend will auto-generatebackend_deploy.py+51-36
frontend/src/app/page.tsx+29-7