update
Browse filesFiles changed (2)
- backend_deploy.py+51-36
- frontend/src/app/page.tsx+29-7
460 unmodified lines4614624634644654664674684694704714724734744754764774784794804814824834844854864874884894901 unmodified line492493494495496497498499500501502503504505506507508509460 unmodified lines# Don't create README - HuggingFace will auto-generate it# We'll add the anycoder tag after deployment# 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 templateprint(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 usernameprint(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-nametoken=token,exist_ok=True)print(f"[Deploy] Template duplication result: {result}")except Exception as e:# If template duplication fails, fall back to regular createprint(f"[Deploy] Template duplication failed, creating regular static space: {e}")import tracebacktraceback.print_exc()api.create_repo(repo_id=repo_id,repo_type="space",1 unmodified lineprivate=private,exist_ok=True # Don't fail if exists)else:# For all other cases (new or update), ensure repo existsprint(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 reposexist_ok=True # Don't fail if repo already exists)except Exception as e:return False, f"Failed to create/access space: {str(e)}", None# Upload filesif not commit_message:460 unmodified lines4614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015021 unmodified line504505506507508509510511512513514515516517518519520521522523524460 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 logicif 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 templateprint(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 usernameprint(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-nametoken=token,exist_ok=True)print(f"[Deploy] Template duplication result: {result}")except Exception as e:# If template duplication fails, fall back to regular createprint(f"[Deploy] Template duplication failed, creating regular static space: {e}")import tracebacktraceback.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 normallyprint(f"[Deploy] Creating new {sdk} space: {repo_id}")api.create_repo(repo_id=repo_id,repo_type="space",1 unmodified lineprivate=private,exist_ok=True # Don't fail if exists)else:# For Docker (React/Streamlit), create_repo is handled differentlyprint(f"[Deploy] Creating new Docker space: {repo_id}")from huggingface_hub import create_repocreate_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)}", Noneelse:print(f"[Deploy] UPDATING existing space: {repo_id} (skipping create_repo)")# Upload filesif not commit_message:
166 unmodified lines1671681691701711721731741751761771781791801811821831843 unmodified lines188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217166 unmodified lineslet existingSpace: string | null = null;// Look for previous deployment or imported space in historyif (messages.length > 0 && username) {console.log('[Deploy] Scanning message history for existing deployments...');for (let i = messages.length - 1; i >= 0; i--) {const msg = messages[i];// Check for deployment messagesif (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);break;}}3 unmodified linesconst match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);if (match) {existingSpace = match[1];console.log('[Deploy] Found previous update:', existingSpace);break;}}// Check for imported space messages - THE KEY PART!if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) {const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);if (match) {const importedSpace = match[1];// 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);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)}}}}}// Auto-generate space name (never prompt user)let spaceName = undefined; // undefined = backend will auto-generate166 unmodified lines1671681691701711721731741751761771781791801811821831841851861871881891901911921931943 unmodified lines198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239166 unmodified lineslet existingSpace: string | null = null;// Look for previous deployment or imported space in historyconsole.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 (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 messagesif (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 "β Deployed" message:', existingSpace);break;}}3 unmodified linesconst match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);if (match) {existingSpace = match[1];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] β β β USER OWNS THIS SPACE! Will update:', existingSpace);break;} else {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-generate
backend_deploy.py+51-36
frontend/src/app/page.tsx+29-7