fix deploy
Browse files268 unmodified lines269270271272273274275632 unmodified lines90890991091191291391491591691791891992092192292331 unmodified lines9559569579589599609611 unmodified line96396496596696796896929 unmodified lines9991000100110021003100410051 unmodified line1007100810091010101110121013268 unmodified lines# Look up the session to get user infoif token in user_sessions:session = user_sessions[token]return MockAuth(session["access_token"], session["username"])# Dev token format: dev_token_<username>_<timestamp>if token and token.startswith("dev_token_"):632 unmodified linesprint(f"[Deploy] Attempting deployment with token (first 10 chars): {user_token[:10]}...")print(f"[Deploy] Request parameters - language: {request.language}, space_name: {request.space_name}, existing_repo_id: {request.existing_repo_id}")# Check for existing deployed space in this sessionsession_token = authorization.replace("Bearer ", "") if authorization else Noneexisting_repo_id = request.existing_repo_id# PRIORITY 1: Check history for deployed/imported spaces (like Gradio version does)# This is more reliable than session tracking since history persists in frontendif request.history and auth.username:print(f"[Deploy] ========== CHECKING HISTORY ==========")print(f"[Deploy] History length: {len(request.history)} messages")print(f"[Deploy] Username: {auth.username}")# Log each message in history for debuggingfor i, msg in enumerate(request.history):31 unmodified linesif match:imported_space = match.group(1)# Only use if user owns itif imported_space.startswith(f"{auth.username}/"):print(f"[Deploy] ✅ Found imported space in history (user owns it): {imported_space}")if not existing_repo_id:existing_repo_id = imported_space1 unmodified lineelse:if not request.history:print(f"[Deploy] ⚠️ No history provided in request")if not auth.username:print(f"[Deploy] ⚠️ No username available")# PRIORITY 2: Check session for previously deployed spaces (fallback)29 unmodified linesprint(f"[Deploy] existing_repo_id: {existing_repo_id}")print(f"[Deploy] space_name: {request.space_name}")print(f"[Deploy] language: {request.language}")print(f"[Deploy] username: {auth.username}")print(f"[Deploy] ==========================================================")success, message, space_url = deploy_to_huggingface_space(1 unmodified linelanguage=request.language,space_name=request.space_name,token=user_token,username=auth.username,description=request.description if hasattr(request, 'description') else None,private=False,existing_repo_id=existing_repo_id,268 unmodified lines269270271272273274275276277278279280281282283284632 unmodified lines91791891992092192292392492592692792892993093193293393493593693793893994094194294394494594631 unmodified lines9789799809819829839841 unmodified line98698798898999099199229 unmodified lines10221023102410251026102710281 unmodified line1030103110321033103410351036268 unmodified lines# Look up the session to get user infoif token in user_sessions:session = user_sessions[token]username = session.get("username")# If username is missing from session (e.g., old session), try to fetch itif not username and session.get("user_info"):user_info = session["user_info"]username = user_info.get("name") or user_info.get("preferred_username") or "user"# Update the session with the username for future requestssession["username"] = usernamereturn MockAuth(session["access_token"], username)# Dev token format: dev_token_<username>_<timestamp>if token and token.startswith("dev_token_"):632 unmodified linesprint(f"[Deploy] Attempting deployment with token (first 10 chars): {user_token[:10]}...")print(f"[Deploy] Request parameters - language: {request.language}, space_name: {request.space_name}, existing_repo_id: {request.existing_repo_id}")# If username is missing, fetch it from HuggingFace APIusername = auth.usernameif not username:print(f"[Deploy] Username not found in auth, fetching from HuggingFace API...")try:from huggingface_hub import HfApihf_api = HfApi(token=user_token)user_info = hf_api.whoami()username = user_info.get("name") or user_info.get("preferred_username") or "user"print(f"[Deploy] Fetched username from HF API: {username}")except Exception as e:print(f"[Deploy] Warning: Could not fetch username from HF API: {e}")# Continue without username - the deploy function will try to fetch it again# Check for existing deployed space in this sessionsession_token = authorization.replace("Bearer ", "") if authorization else Noneexisting_repo_id = request.existing_repo_id# PRIORITY 1: Check history for deployed/imported spaces (like Gradio version does)# This is more reliable than session tracking since history persists in frontendif request.history and username:print(f"[Deploy] ========== CHECKING HISTORY ==========")print(f"[Deploy] History length: {len(request.history)} messages")print(f"[Deploy] Username: {username}")# Log each message in history for debuggingfor i, msg in enumerate(request.history):31 unmodified linesif match:imported_space = match.group(1)# Only use if user owns itif imported_space.startswith(f"{username}/"):print(f"[Deploy] ✅ Found imported space in history (user owns it): {imported_space}")if not existing_repo_id:existing_repo_id = imported_space1 unmodified lineelse:if not request.history:print(f"[Deploy] ⚠️ No history provided in request")if not username:print(f"[Deploy] ⚠️ No username available")# PRIORITY 2: Check session for previously deployed spaces (fallback)29 unmodified linesprint(f"[Deploy] existing_repo_id: {existing_repo_id}")print(f"[Deploy] space_name: {request.space_name}")print(f"[Deploy] language: {request.language}")print(f"[Deploy] username: {username}")print(f"[Deploy] ==========================================================")success, message, space_url = deploy_to_huggingface_space(1 unmodified linelanguage=request.language,space_name=request.space_name,token=user_token,username=username,description=request.description if hasattr(request, 'description') else None,private=False,existing_repo_id=existing_repo_id,
backend_api.py+30-7