fix deploy
Browse files- backend_api.py +30 -7
backend_api.py
CHANGED
|
@@ -269,7 +269,16 @@ def get_auth_from_header(authorization: Optional[str] = None):
|
|
| 269 |
# Look up the session to get user info
|
| 270 |
if token in user_sessions:
|
| 271 |
session = user_sessions[token]
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
# Dev token format: dev_token_<username>_<timestamp>
|
| 275 |
if token and token.startswith("dev_token_"):
|
|
@@ -908,16 +917,30 @@ async def deploy(
|
|
| 908 |
print(f"[Deploy] Attempting deployment with token (first 10 chars): {user_token[:10]}...")
|
| 909 |
print(f"[Deploy] Request parameters - language: {request.language}, space_name: {request.space_name}, existing_repo_id: {request.existing_repo_id}")
|
| 910 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 911 |
# Check for existing deployed space in this session
|
| 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
|
| 918 |
print(f"[Deploy] ========== CHECKING HISTORY ==========")
|
| 919 |
print(f"[Deploy] History length: {len(request.history)} messages")
|
| 920 |
-
print(f"[Deploy] Username: {
|
| 921 |
|
| 922 |
# Log each message in history for debugging
|
| 923 |
for i, msg in enumerate(request.history):
|
|
@@ -955,7 +978,7 @@ async def deploy(
|
|
| 955 |
if match:
|
| 956 |
imported_space = match.group(1)
|
| 957 |
# Only use if user owns it
|
| 958 |
-
if imported_space.startswith(f"{
|
| 959 |
print(f"[Deploy] ✅ Found imported space in history (user owns it): {imported_space}")
|
| 960 |
if not existing_repo_id:
|
| 961 |
existing_repo_id = imported_space
|
|
@@ -963,7 +986,7 @@ async def deploy(
|
|
| 963 |
else:
|
| 964 |
if not request.history:
|
| 965 |
print(f"[Deploy] ⚠️ No history provided in request")
|
| 966 |
-
if not
|
| 967 |
print(f"[Deploy] ⚠️ No username available")
|
| 968 |
|
| 969 |
# PRIORITY 2: Check session for previously deployed spaces (fallback)
|
|
@@ -999,7 +1022,7 @@ async def deploy(
|
|
| 999 |
print(f"[Deploy] existing_repo_id: {existing_repo_id}")
|
| 1000 |
print(f"[Deploy] space_name: {request.space_name}")
|
| 1001 |
print(f"[Deploy] language: {request.language}")
|
| 1002 |
-
print(f"[Deploy] username: {
|
| 1003 |
print(f"[Deploy] ==========================================================")
|
| 1004 |
|
| 1005 |
success, message, space_url = deploy_to_huggingface_space(
|
|
@@ -1007,7 +1030,7 @@ async def deploy(
|
|
| 1007 |
language=request.language,
|
| 1008 |
space_name=request.space_name,
|
| 1009 |
token=user_token,
|
| 1010 |
-
username=
|
| 1011 |
description=request.description if hasattr(request, 'description') else None,
|
| 1012 |
private=False,
|
| 1013 |
existing_repo_id=existing_repo_id,
|
|
|
|
| 269 |
# Look up the session to get user info
|
| 270 |
if token in user_sessions:
|
| 271 |
session = user_sessions[token]
|
| 272 |
+
username = session.get("username")
|
| 273 |
+
|
| 274 |
+
# If username is missing from session (e.g., old session), try to fetch it
|
| 275 |
+
if not username and session.get("user_info"):
|
| 276 |
+
user_info = session["user_info"]
|
| 277 |
+
username = user_info.get("name") or user_info.get("preferred_username") or "user"
|
| 278 |
+
# Update the session with the username for future requests
|
| 279 |
+
session["username"] = username
|
| 280 |
+
|
| 281 |
+
return MockAuth(session["access_token"], username)
|
| 282 |
|
| 283 |
# Dev token format: dev_token_<username>_<timestamp>
|
| 284 |
if token and token.startswith("dev_token_"):
|
|
|
|
| 917 |
print(f"[Deploy] Attempting deployment with token (first 10 chars): {user_token[:10]}...")
|
| 918 |
print(f"[Deploy] Request parameters - language: {request.language}, space_name: {request.space_name}, existing_repo_id: {request.existing_repo_id}")
|
| 919 |
|
| 920 |
+
# If username is missing, fetch it from HuggingFace API
|
| 921 |
+
username = auth.username
|
| 922 |
+
if not username:
|
| 923 |
+
print(f"[Deploy] Username not found in auth, fetching from HuggingFace API...")
|
| 924 |
+
try:
|
| 925 |
+
from huggingface_hub import HfApi
|
| 926 |
+
hf_api = HfApi(token=user_token)
|
| 927 |
+
user_info = hf_api.whoami()
|
| 928 |
+
username = user_info.get("name") or user_info.get("preferred_username") or "user"
|
| 929 |
+
print(f"[Deploy] Fetched username from HF API: {username}")
|
| 930 |
+
except Exception as e:
|
| 931 |
+
print(f"[Deploy] Warning: Could not fetch username from HF API: {e}")
|
| 932 |
+
# Continue without username - the deploy function will try to fetch it again
|
| 933 |
+
|
| 934 |
# Check for existing deployed space in this session
|
| 935 |
session_token = authorization.replace("Bearer ", "") if authorization else None
|
| 936 |
existing_repo_id = request.existing_repo_id
|
| 937 |
|
| 938 |
# PRIORITY 1: Check history for deployed/imported spaces (like Gradio version does)
|
| 939 |
# This is more reliable than session tracking since history persists in frontend
|
| 940 |
+
if request.history and username:
|
| 941 |
print(f"[Deploy] ========== CHECKING HISTORY ==========")
|
| 942 |
print(f"[Deploy] History length: {len(request.history)} messages")
|
| 943 |
+
print(f"[Deploy] Username: {username}")
|
| 944 |
|
| 945 |
# Log each message in history for debugging
|
| 946 |
for i, msg in enumerate(request.history):
|
|
|
|
| 978 |
if match:
|
| 979 |
imported_space = match.group(1)
|
| 980 |
# Only use if user owns it
|
| 981 |
+
if imported_space.startswith(f"{username}/"):
|
| 982 |
print(f"[Deploy] ✅ Found imported space in history (user owns it): {imported_space}")
|
| 983 |
if not existing_repo_id:
|
| 984 |
existing_repo_id = imported_space
|
|
|
|
| 986 |
else:
|
| 987 |
if not request.history:
|
| 988 |
print(f"[Deploy] ⚠️ No history provided in request")
|
| 989 |
+
if not username:
|
| 990 |
print(f"[Deploy] ⚠️ No username available")
|
| 991 |
|
| 992 |
# PRIORITY 2: Check session for previously deployed spaces (fallback)
|
|
|
|
| 1022 |
print(f"[Deploy] existing_repo_id: {existing_repo_id}")
|
| 1023 |
print(f"[Deploy] space_name: {request.space_name}")
|
| 1024 |
print(f"[Deploy] language: {request.language}")
|
| 1025 |
+
print(f"[Deploy] username: {username}")
|
| 1026 |
print(f"[Deploy] ==========================================================")
|
| 1027 |
|
| 1028 |
success, message, space_url = deploy_to_huggingface_space(
|
|
|
|
| 1030 |
language=request.language,
|
| 1031 |
space_name=request.space_name,
|
| 1032 |
token=user_token,
|
| 1033 |
+
username=username,
|
| 1034 |
description=request.description if hasattr(request, 'description') else None,
|
| 1035 |
private=False,
|
| 1036 |
existing_repo_id=existing_repo_id,
|