Spaces:
Running
Running
update
Browse files- backend_deploy.py +51 -36
- frontend/src/app/page.tsx +29 -7
backend_deploy.py
CHANGED
|
@@ -461,30 +461,42 @@ def deploy_to_huggingface_space(
|
|
| 461 |
# Don't create README - HuggingFace will auto-generate it
|
| 462 |
# We'll add the anycoder tag after deployment
|
| 463 |
|
| 464 |
-
#
|
| 465 |
-
# This
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
api.create_repo(
|
| 489 |
repo_id=repo_id,
|
| 490 |
repo_type="space",
|
|
@@ -492,18 +504,21 @@ def deploy_to_huggingface_space(
|
|
| 492 |
private=private,
|
| 493 |
exist_ok=True # Don't fail if exists
|
| 494 |
)
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
| 507 |
|
| 508 |
# Upload files
|
| 509 |
if not commit_message:
|
|
|
|
| 461 |
# Don't create README - HuggingFace will auto-generate it
|
| 462 |
# We'll add the anycoder tag after deployment
|
| 463 |
|
| 464 |
+
# ONLY create repo for NEW deployments (not updates!)
|
| 465 |
+
# This matches the Gradio version logic
|
| 466 |
+
if not is_update:
|
| 467 |
+
print(f"[Deploy] Creating NEW space: {repo_id}")
|
| 468 |
+
try:
|
| 469 |
+
if language == "transformers.js":
|
| 470 |
+
# For NEW transformers.js spaces, try to duplicate the template
|
| 471 |
+
print(f"[Deploy] Creating new transformers.js space: {repo_id}")
|
| 472 |
+
try:
|
| 473 |
+
from huggingface_hub import duplicate_space
|
| 474 |
+
|
| 475 |
+
# IMPORTANT: duplicate_space expects just the space name, not the full repo_id
|
| 476 |
+
# It will automatically prepend the username
|
| 477 |
+
print(f"[Deploy] Attempting to duplicate template space to: {space_name}")
|
| 478 |
+
result = duplicate_space(
|
| 479 |
+
from_id="static-templates/transformers.js",
|
| 480 |
+
to_id=space_name, # Just the space name, not username/space-name
|
| 481 |
+
token=token,
|
| 482 |
+
exist_ok=True
|
| 483 |
+
)
|
| 484 |
+
print(f"[Deploy] Template duplication result: {result}")
|
| 485 |
+
except Exception as e:
|
| 486 |
+
# If template duplication fails, fall back to regular create
|
| 487 |
+
print(f"[Deploy] Template duplication failed, creating regular static space: {e}")
|
| 488 |
+
import traceback
|
| 489 |
+
traceback.print_exc()
|
| 490 |
+
api.create_repo(
|
| 491 |
+
repo_id=repo_id,
|
| 492 |
+
repo_type="space",
|
| 493 |
+
space_sdk=sdk,
|
| 494 |
+
private=private,
|
| 495 |
+
exist_ok=True # Don't fail if exists
|
| 496 |
+
)
|
| 497 |
+
elif sdk != "docker":
|
| 498 |
+
# For non-Docker SDKs, create the repo normally
|
| 499 |
+
print(f"[Deploy] Creating new {sdk} space: {repo_id}")
|
| 500 |
api.create_repo(
|
| 501 |
repo_id=repo_id,
|
| 502 |
repo_type="space",
|
|
|
|
| 504 |
private=private,
|
| 505 |
exist_ok=True # Don't fail if exists
|
| 506 |
)
|
| 507 |
+
else:
|
| 508 |
+
# For Docker (React/Streamlit), create_repo is handled differently
|
| 509 |
+
print(f"[Deploy] Creating new Docker space: {repo_id}")
|
| 510 |
+
from huggingface_hub import create_repo
|
| 511 |
+
create_repo(
|
| 512 |
+
repo_id=repo_id,
|
| 513 |
+
repo_type="space",
|
| 514 |
+
space_sdk="docker",
|
| 515 |
+
token=token,
|
| 516 |
+
exist_ok=True
|
| 517 |
+
)
|
| 518 |
+
except Exception as e:
|
| 519 |
+
return False, f"Failed to create space: {str(e)}", None
|
| 520 |
+
else:
|
| 521 |
+
print(f"[Deploy] UPDATING existing space: {repo_id} (skipping create_repo)")
|
| 522 |
|
| 523 |
# Upload files
|
| 524 |
if not commit_message:
|
frontend/src/app/page.tsx
CHANGED
|
@@ -167,18 +167,28 @@ export default function Home() {
|
|
| 167 |
let existingSpace: string | null = null;
|
| 168 |
|
| 169 |
// Look for previous deployment or imported space in history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
if (messages.length > 0 && username) {
|
| 171 |
-
console.log('[Deploy] Scanning message history
|
| 172 |
|
| 173 |
for (let i = messages.length - 1; i >= 0; i--) {
|
| 174 |
const msg = messages[i];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
// Check for deployment messages
|
| 177 |
if (msg.role === 'assistant' && msg.content.includes('β
Deployed')) {
|
| 178 |
const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
|
| 179 |
if (match) {
|
| 180 |
existingSpace = match[1];
|
| 181 |
-
console.log('[Deploy] Found
|
| 182 |
break;
|
| 183 |
}
|
| 184 |
}
|
|
@@ -188,30 +198,42 @@ export default function Home() {
|
|
| 188 |
const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
|
| 189 |
if (match) {
|
| 190 |
existingSpace = match[1];
|
| 191 |
-
console.log('[Deploy] Found
|
| 192 |
break;
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
| 196 |
// Check for imported space messages - THE KEY PART!
|
| 197 |
if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) {
|
|
|
|
| 198 |
const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
|
|
|
|
| 199 |
if (match) {
|
| 200 |
const importedSpace = match[1];
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
// Only use imported space if user owns it (can update it)
|
| 202 |
if (importedSpace.startsWith(`${username}/`)) {
|
| 203 |
existingSpace = importedSpace;
|
| 204 |
-
console.log('[Deploy]
|
| 205 |
break;
|
| 206 |
} else {
|
| 207 |
-
console.log('[Deploy]
|
| 208 |
-
|
| 209 |
-
// (existingSpace remains null, triggering new deployment)
|
| 210 |
}
|
|
|
|
|
|
|
| 211 |
}
|
| 212 |
}
|
| 213 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
}
|
|
|
|
| 215 |
|
| 216 |
// Auto-generate space name (never prompt user)
|
| 217 |
let spaceName = undefined; // undefined = backend will auto-generate
|
|
|
|
| 167 |
let existingSpace: string | null = null;
|
| 168 |
|
| 169 |
// Look for previous deployment or imported space in history
|
| 170 |
+
console.log('[Deploy] ========== DEBUG START ==========');
|
| 171 |
+
console.log('[Deploy] Total messages in history:', messages.length);
|
| 172 |
+
console.log('[Deploy] Current username:', username);
|
| 173 |
+
console.log('[Deploy] Messages:', JSON.stringify(messages, null, 2));
|
| 174 |
+
|
| 175 |
if (messages.length > 0 && username) {
|
| 176 |
+
console.log('[Deploy] Scanning message history...');
|
| 177 |
|
| 178 |
for (let i = messages.length - 1; i >= 0; i--) {
|
| 179 |
const msg = messages[i];
|
| 180 |
+
console.log(`[Deploy] Checking message ${i}:`, {
|
| 181 |
+
role: msg.role,
|
| 182 |
+
contentPreview: msg.content.substring(0, 100),
|
| 183 |
+
startsWithImported: msg.content.startsWith('Imported Space from')
|
| 184 |
+
});
|
| 185 |
|
| 186 |
// Check for deployment messages
|
| 187 |
if (msg.role === 'assistant' && msg.content.includes('β
Deployed')) {
|
| 188 |
const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
|
| 189 |
if (match) {
|
| 190 |
existingSpace = match[1];
|
| 191 |
+
console.log('[Deploy] β
Found "β
Deployed" message:', existingSpace);
|
| 192 |
break;
|
| 193 |
}
|
| 194 |
}
|
|
|
|
| 198 |
const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
|
| 199 |
if (match) {
|
| 200 |
existingSpace = match[1];
|
| 201 |
+
console.log('[Deploy] β
Found "β
Updated" message:', existingSpace);
|
| 202 |
break;
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
// Check for imported space messages - THE KEY PART!
|
| 207 |
if (msg.role === 'user' && msg.content.startsWith('Imported Space from')) {
|
| 208 |
+
console.log('[Deploy] π― Found "Imported Space from" message!');
|
| 209 |
const match = msg.content.match(/huggingface\.co\/spaces\/([^\/\s\)]+\/[^\/\s\)]+)/);
|
| 210 |
+
console.log('[Deploy] Regex match result:', match);
|
| 211 |
if (match) {
|
| 212 |
const importedSpace = match[1];
|
| 213 |
+
console.log('[Deploy] Extracted space:', importedSpace);
|
| 214 |
+
console.log('[Deploy] Username to check:', username);
|
| 215 |
+
console.log('[Deploy] Starts with username?', importedSpace.startsWith(`${username}/`));
|
| 216 |
+
|
| 217 |
// Only use imported space if user owns it (can update it)
|
| 218 |
if (importedSpace.startsWith(`${username}/`)) {
|
| 219 |
existingSpace = importedSpace;
|
| 220 |
+
console.log('[Deploy] β
β
β
USER OWNS THIS SPACE! Will update:', existingSpace);
|
| 221 |
break;
|
| 222 |
} else {
|
| 223 |
+
console.log('[Deploy] β οΈ User does not own this space:', importedSpace);
|
| 224 |
+
console.log('[Deploy] Expected to start with:', `${username}/`);
|
|
|
|
| 225 |
}
|
| 226 |
+
} else {
|
| 227 |
+
console.log('[Deploy] β Regex did not match URL in message');
|
| 228 |
}
|
| 229 |
}
|
| 230 |
}
|
| 231 |
+
|
| 232 |
+
console.log('[Deploy] Final existingSpace value:', existingSpace);
|
| 233 |
+
} else {
|
| 234 |
+
console.log('[Deploy] Skipping scan - no messages or no username');
|
| 235 |
}
|
| 236 |
+
console.log('[Deploy] ========== DEBUG END ==========');
|
| 237 |
|
| 238 |
// Auto-generate space name (never prompt user)
|
| 239 |
let spaceName = undefined; // undefined = backend will auto-generate
|