fix making new commits
Browse files412 unmodified lines4134144154164174183 unmodified lines4224234244254264274288 unmodified lines4374384394404414426 unmodified lines4494504514524534541 unmodified line456457458459460461462419 unmodified lines882883884885886887888412 unmodified lines"""def deploy_to_huggingface_space(code: str,language: str,3 unmodified linesdescription: Optional[str] = None,private: bool = False,existing_repo_id: Optional[str] = None,commit_message: Optional[str] = None) -> Tuple[bool, str, Optional[str]]:"""Deploy code to HuggingFace Spaces (create new or update existing)8 unmodified linesprivate: Whether to make the space private (only for new spaces)existing_repo_id: If provided (username/space-name), updates this space instead of creating new onecommit_message: Custom commit message (defaults to "Deploy from anycoder" or "Update from anycoder")Returns:Tuple of (success: bool, message: str, space_url: Optional[str])6 unmodified linestry:api = HfApi(token=token)# Determine if this is an update or new deploymentis_update = existing_repo_id is not None1 unmodified line# Use existing reporepo_id = existing_repo_idspace_name = existing_repo_id.split('/')[-1]username = existing_repo_id.split('/')[0] if '/' in existing_repo_id else usernameelse:# Get username if not providedif not username:419 unmodified linesspace_url = f"https://huggingface.co/spaces/{repo_id}"action = "Updated" if is_update else "Deployed"return True, f"✅ {action} successfully to {repo_id}!", space_urlexcept Exception as e:print(f"[Deploy] Top-level exception caught: {type(e).__name__}: {str(e)}")412 unmodified lines4134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654663 unmodified lines4704714724734744754764778 unmodified lines4864874884894904914926 unmodified lines4995005015025035045055065075085095105115125135145155165175185191 unmodified line521522523524525526527528529530531532533534535419 unmodified lines955956957958959960961962963964965966412 unmodified lines"""def extract_space_id_from_history(history: Optional[List[Dict]], username: Optional[str] = None) -> Optional[str]:"""Extract existing space ID from chat history (for updates after followups/imports)Args:history: Chat history (list of dicts with 'role' and 'content')username: Current username (to verify ownership of imported spaces)Returns:Space ID (username/space-name) if found, None otherwise"""if not history:return Noneimport reexisting_space = None# Look through history for previous deployments or importsfor msg in history:role = msg.get('role', '')content = msg.get('content', '')# Check assistant messages for deployment confirmationsif role == 'assistant':if "✅ Deployed!" in content or "✅ Updated!" in content:# Look for space URL patternmatch = re.search(r'huggingface\.co/spaces/([^/\s\)]+/[^/\s\)]+)', content)if match:existing_space = match.group(1)break# Check user messages for importselif role == 'user':if "import" in content.lower() and "space" in content.lower():# Extract space name from import messagematch = re.search(r'huggingface\.co/spaces/([^/\s\)]+/[^/\s\)]+)', content)if match:imported_space = match.group(1)# Only use imported space if user owns it (can update it)if username and imported_space.startswith(f"{username}/"):existing_space = imported_spacebreak# If user doesn't own the imported space, we'll create a new one# (existing_space remains None, triggering new deployment)return existing_spacedef deploy_to_huggingface_space(code: str,language: str,3 unmodified linesdescription: Optional[str] = None,private: bool = False,existing_repo_id: Optional[str] = None,commit_message: Optional[str] = None,history: Optional[List[Dict]] = None) -> Tuple[bool, str, Optional[str]]:"""Deploy code to HuggingFace Spaces (create new or update existing)8 unmodified linesprivate: Whether to make the space private (only for new spaces)existing_repo_id: If provided (username/space-name), updates this space instead of creating new onecommit_message: Custom commit message (defaults to "Deploy from anycoder" or "Update from anycoder")history: Chat history (list of dicts with 'role' and 'content') - used to detect followups/importsReturns:Tuple of (success: bool, message: str, space_url: Optional[str])6 unmodified linestry:api = HfApi(token=token)# Get username if not provided (needed for history tracking)if not username:try:user_info = api.whoami()username = user_info.get("name") or user_info.get("preferred_username") or "user"except Exception as e:pass # Will handle later if needed# Check history for existing space if not explicitly provided# This enables automatic updates for followup prompts and imported spacesif not existing_repo_id and history:existing_repo_id = extract_space_id_from_history(history, username)if existing_repo_id:print(f"[Deploy] Detected existing space from history: {existing_repo_id}")# Determine if this is an update or new deploymentis_update = existing_repo_id is not None1 unmodified line# Use existing reporepo_id = existing_repo_idspace_name = existing_repo_id.split('/')[-1]if '/' in existing_repo_id:username = existing_repo_id.split('/')[0]elif not username:# Get username if still not availabletry:user_info = api.whoami()username = user_info.get("name") or user_info.get("preferred_username") or "user"except Exception as e:return False, f"Failed to get user info: {str(e)}", Noneelse:# Get username if not providedif not username:419 unmodified linesspace_url = f"https://huggingface.co/spaces/{repo_id}"action = "Updated" if is_update else "Deployed"# Include the space URL in the message for history tracking# This allows future deployments to detect this as the existing spacesuccess_msg = f"✅ {action}! View your space at: {space_url}"return True, success_msg, space_urlexcept Exception as e:print(f"[Deploy] Top-level exception caught: {type(e).__name__}: {str(e)}")
backend_deploy.py+81-3