fix
Browse files- backend_api.py +39 -13
- frontend/src/app/page.tsx +3 -1
- frontend/src/types/index.ts +1 -0
backend_api.py
CHANGED
|
@@ -207,6 +207,7 @@ class DeploymentRequest(BaseModel):
|
|
| 207 |
requirements: Optional[str] = None
|
| 208 |
existing_repo_id: Optional[str] = None # For updating existing spaces
|
| 209 |
commit_message: Optional[str] = None
|
|
|
|
| 210 |
|
| 211 |
|
| 212 |
class AuthStatus(BaseModel):
|
|
@@ -911,9 +912,41 @@ async def deploy(
|
|
| 911 |
session_token = authorization.replace("Bearer ", "") if authorization else None
|
| 912 |
existing_repo_id = request.existing_repo_id
|
| 913 |
|
| 914 |
-
#
|
| 915 |
-
# This
|
| 916 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 917 |
session = user_sessions[session_token]
|
| 918 |
|
| 919 |
# Ensure deployed_spaces exists (for backward compatibility with old sessions)
|
|
@@ -931,20 +964,13 @@ async def deploy(
|
|
| 931 |
if space.get("language") == request.language:
|
| 932 |
session_space_id = space.get("repo_id")
|
| 933 |
print(f"[Deploy] ✅ Found existing space in session for {request.language}: {session_space_id}")
|
| 934 |
-
|
| 935 |
-
if not existing_repo_id:
|
| 936 |
-
existing_repo_id = session_space_id
|
| 937 |
-
print(f"[Deploy] Using session space: {existing_repo_id}")
|
| 938 |
-
elif existing_repo_id != session_space_id:
|
| 939 |
-
# Frontend and session disagree - trust the session (more recent)
|
| 940 |
-
print(f"[Deploy] ⚠️ Frontend provided {existing_repo_id} but session has {session_space_id}. Using session.")
|
| 941 |
-
existing_repo_id = session_space_id
|
| 942 |
break
|
| 943 |
|
| 944 |
if not existing_repo_id:
|
| 945 |
print(f"[Deploy] ⚠️ No existing space found for language: {request.language}")
|
| 946 |
-
|
| 947 |
-
print(f"[Deploy] ⚠️ No session found. session_token: {session_token[:10] if session_token else 'None'}")
|
| 948 |
|
| 949 |
# Use the standalone deployment function
|
| 950 |
print(f"[Deploy] ========== CALLING deploy_to_huggingface_space ==========")
|
|
|
|
| 207 |
requirements: Optional[str] = None
|
| 208 |
existing_repo_id: Optional[str] = None # For updating existing spaces
|
| 209 |
commit_message: Optional[str] = None
|
| 210 |
+
history: List[Dict] = [] # Chat history for tracking deployed spaces
|
| 211 |
|
| 212 |
|
| 213 |
class AuthStatus(BaseModel):
|
|
|
|
| 912 |
session_token = authorization.replace("Bearer ", "") if authorization else None
|
| 913 |
existing_repo_id = request.existing_repo_id
|
| 914 |
|
| 915 |
+
# PRIORITY 1: Check history for deployed/imported spaces (like Gradio version does)
|
| 916 |
+
# This is more reliable than session tracking since history persists in frontend
|
| 917 |
+
if request.history and auth.username:
|
| 918 |
+
print(f"[Deploy] Checking history for deployed spaces ({len(request.history)} messages)...")
|
| 919 |
+
for msg in request.history:
|
| 920 |
+
role = msg.get('role', '')
|
| 921 |
+
content = msg.get('content', '')
|
| 922 |
+
|
| 923 |
+
# Check for deployment confirmations
|
| 924 |
+
if role == 'assistant' and ('✅ Deployed!' in content or '✅ Updated!' in content):
|
| 925 |
+
import re
|
| 926 |
+
match = re.search(r'huggingface\.co/spaces/([^/\s\)]+/[^/\s\)]+)', content)
|
| 927 |
+
if match:
|
| 928 |
+
history_space_id = match.group(1)
|
| 929 |
+
print(f"[Deploy] ✅ Found deployed space in history: {history_space_id}")
|
| 930 |
+
if not existing_repo_id:
|
| 931 |
+
existing_repo_id = history_space_id
|
| 932 |
+
break
|
| 933 |
+
|
| 934 |
+
# Check for imports
|
| 935 |
+
elif role == 'user' and 'import' in content.lower():
|
| 936 |
+
import re
|
| 937 |
+
match = re.search(r'huggingface\.co/spaces/([^/\s\)]+/[^/\s\)]+)', content)
|
| 938 |
+
if match:
|
| 939 |
+
imported_space = match.group(1)
|
| 940 |
+
# Only use if user owns it
|
| 941 |
+
if imported_space.startswith(f"{auth.username}/"):
|
| 942 |
+
print(f"[Deploy] ✅ Found imported space in history (user owns it): {imported_space}")
|
| 943 |
+
if not existing_repo_id:
|
| 944 |
+
existing_repo_id = imported_space
|
| 945 |
+
break
|
| 946 |
+
|
| 947 |
+
# PRIORITY 2: Check session for previously deployed spaces (fallback)
|
| 948 |
+
# This helps when history isn't passed from frontend
|
| 949 |
+
if not existing_repo_id and session_token and session_token in user_sessions:
|
| 950 |
session = user_sessions[session_token]
|
| 951 |
|
| 952 |
# Ensure deployed_spaces exists (for backward compatibility with old sessions)
|
|
|
|
| 964 |
if space.get("language") == request.language:
|
| 965 |
session_space_id = space.get("repo_id")
|
| 966 |
print(f"[Deploy] ✅ Found existing space in session for {request.language}: {session_space_id}")
|
| 967 |
+
existing_repo_id = session_space_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
break
|
| 969 |
|
| 970 |
if not existing_repo_id:
|
| 971 |
print(f"[Deploy] ⚠️ No existing space found for language: {request.language}")
|
| 972 |
+
elif not existing_repo_id:
|
| 973 |
+
print(f"[Deploy] ⚠️ No session found and no history provided. session_token: {session_token[:10] if session_token else 'None'}")
|
| 974 |
|
| 975 |
# Use the standalone deployment function
|
| 976 |
print(f"[Deploy] ========== CALLING deploy_to_huggingface_space ==========")
|
frontend/src/app/page.tsx
CHANGED
|
@@ -439,6 +439,7 @@ export default function Home() {
|
|
| 439 |
const deployRequest: any = {
|
| 440 |
code: generatedCode,
|
| 441 |
language: selectedLanguage,
|
|
|
|
| 442 |
};
|
| 443 |
|
| 444 |
// Only include optional fields if they have values
|
|
@@ -455,7 +456,8 @@ export default function Home() {
|
|
| 455 |
space_name: deployRequest.space_name,
|
| 456 |
language: deployRequest.language,
|
| 457 |
has_code: !!deployRequest.code,
|
| 458 |
-
code_length: deployRequest.code?.length
|
|
|
|
| 459 |
});
|
| 460 |
console.log('[Deploy] Full request object:', JSON.stringify(deployRequest, null, 2).substring(0, 500));
|
| 461 |
|
|
|
|
| 439 |
const deployRequest: any = {
|
| 440 |
code: generatedCode,
|
| 441 |
language: selectedLanguage,
|
| 442 |
+
history: messages.map(msg => ({ role: msg.role, content: msg.content })) // Pass full chat history
|
| 443 |
};
|
| 444 |
|
| 445 |
// Only include optional fields if they have values
|
|
|
|
| 456 |
space_name: deployRequest.space_name,
|
| 457 |
language: deployRequest.language,
|
| 458 |
has_code: !!deployRequest.code,
|
| 459 |
+
code_length: deployRequest.code?.length,
|
| 460 |
+
history_length: deployRequest.history?.length
|
| 461 |
});
|
| 462 |
console.log('[Deploy] Full request object:', JSON.stringify(deployRequest, null, 2).substring(0, 500));
|
| 463 |
|
frontend/src/types/index.ts
CHANGED
|
@@ -49,6 +49,7 @@ export interface DeploymentRequest {
|
|
| 49 |
requirements?: string;
|
| 50 |
existing_repo_id?: string; // For updating existing spaces
|
| 51 |
commit_message?: string;
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
export interface DeploymentResponse {
|
|
|
|
| 49 |
requirements?: string;
|
| 50 |
existing_repo_id?: string; // For updating existing spaces
|
| 51 |
commit_message?: string;
|
| 52 |
+
history?: Array<{role: string; content: string}>; // Chat history for tracking
|
| 53 |
}
|
| 54 |
|
| 55 |
export interface DeploymentResponse {
|