o |_h@sddlZddlZddlZddlmZddlmZddlZddlZddl m Z m Z ddl m Z ddlmZddlmZdZ dd ed edBfd d Z dd ed edBd dfddZd ed dfddZded ed dfddZdS)N)files)Path)Client handle_file) ReadTimeout)RepositoryNotFoundError) SQLiteStoragez(https://huggingface.co/spaces/{space_id}space_id dataset_idc Cs\tddkr dStd}t}d}d}z|}|ddddkr&d }Wn ty2d }Ynw|rCtd tjdd |}tj |d d d dt t |dd$}| }| dtj}t|d}|j|d|d dWdn1s{wYtj|j|d |dgdtj} | durt|d| |durt|d|dSdS)NZSYSTEMspacestrackioFauth accessTokenrolewriteTz2Need 'write' access token to create a Spaces repo.)Zadd_to_git_credentialgradiospace) space_sdk repo_typeexist_okz README.mdrz{GRADIO_VERSION}zutf-8)path_or_fileobj path_in_reporepo_idr)rr folder_pathignore_patternsHF_TOKENTRACKIO_DATASET_ID)osgetenvrhuggingface_hubHfApiwhoamiOSErrorprintlogin create_repoopenrreadreplacer __version__ioBytesIOencode upload_fileutilsdisable_progress_bars upload_folder get_tokenadd_space_secretadd_space_variable) r r Z trackio_pathhf_apir"r%fZreadme_contentZ readme_bufferhf_tokenr86/Users/nouamanetazi/projects/trackio/trackio/deploy.pydeploy_as_spacesb     r:returncCsd|vr td|d|durd|vrtd|dz tj|ddtd tj|d |dur:t|d |WdStyEYnwtd tj|d t||dS) z Creates a new Hugging Face Space if it does not exist. If a dataset_id is provided, it will be added as a space variable. Args: space_id: The ID of the Space to create. dataset_id: The ID of the Dataset to add to the Space. /zInvalid space ID: z?. Must be in the format: username/reponame or orgname/reponame.NzInvalid dataset ID: zE. Must be in the format: username/datasetname or orgname/datasetname.r)rz* Found existing space: )r rz* Creating new space: ) ValueErrorr repo_infor$ SPACE_URLformatr4rr:)r r r8r8r9create_space_if_not_existsLs*    rAc Cs|d}tttddD]-}zt|dd}|rWt dWq ty,tdYq ty9tdYq wt d)z Blocks the current thread until the space exists. May raise a TimeoutError if this takes quite a while. Args: space_id: The ID of the Space to wait for. NZTRACKIO_SPACE_WAIT_ATTEMPTSZ3000Fverbosez4Waiting for space to exist took longer than expected) rangeintrrrrtimesleepr= TimeoutError)r client_r8r8r9wait_until_space_existsns    rLprojectcCs6t|}t|dd}|jd|t|tjddS)z Uploads the database of a local Trackio project to a Hugging Face Space. Args: project: The name of the project to upload. space_id: The ID of the Space to upload to. FrBz/upload_db_to_space)api_namerMZ uploaded_dbr7N)rZget_project_db_pathrpredictrr r/r2)rMr Zdb_pathrJr8r8r9upload_db_to_spaces   rP)N)r+rrGimportlib.resourcesrpathlibrrr gradio_clientrrhttpxrZhuggingface_hub.errorsrtrackio.sqlite_storagerr?strr:rArLrPr8r8r8r9s@      < "