o
    ;i-9                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z-m.Z. dd Z/dd Z0de1fddZ2defddZeee3dZ4eG d d! d!Z5ej6dCd#e7d$ee de8e5 fd%d&Z9ej6ddd'd(e7d#ee7 d$ee ddfd)d*Z:ej6ddd'd+e7d#ee7 d$ee de;e7e	ee f f fd,d-Z<ej6	.dDd/e	e7ef d0e=de&fd1d2Z>ej6			.dEd3e7d4ee) d5ed6 d0e=de&f
d7d8Z?de7fd9d:Z@ej6dd.d;d<ee7 d0e=de&fd=d>ZAej6dd?d@e7d$ee ddfdAdBZBdS )F    N)	dataclass)Path)LiteralOptionalUnion)api_pb2   )ClusterInfoget_cluster_info)	_Function)_get_environment_name)
_clustered)_ContainerIOManager)synchronize_apisynchronizer)_App)_Client)_Cls)InvalidError)DockerfileSpecImageBuilderVersion_Image_ImageRegistryConfig)_Secret   )flash_forwardflash_get_containersflash_prometheus_autoscalerhttp_serverc                   C   s   t   dS )zDon't fetch any more inputs from the server, after the current one.
    The container will exit gracefully after the current input is processed.N)r   stop_fetching_inputs r    r    O/home/ubuntu/.local/lib/python3.10/site-packages/modal/experimental/__init__.pyr      s   r   c                   C   s   t  S )zGet the container's local input concurrency.
    If recently reduced to particular value, it can return a larger number than
    set due to in-progress inputs.)r   get_input_concurrencyr    r    r    r!   get_local_input_concurrency$   s   r#   concurrencyc                 C   s   t |  dS )zSet the container's local input concurrency. Dynamic concurrency will be disabled.
    When setting to a smaller value, this method will not interrupt in-progress inputs.
    N)r   set_input_concurrency)r$   r    r    r!   set_local_input_concurrency+   s   r&   returnc                   C   s   t  S )N)_get_cluster_infor    r    r    r!   r
   2   s   r
   )target_modulec                   @   s&   e Zd ZU eed< eed< eed< dS )AppInfoapp_idname
containersN)__name__
__module____qualname__str__annotations__intr    r    r    r!   r*   9   s   
 r*    environment_nameclientc                    sl   |p	t  I dH }|jtjt| dI dH }g }|jD ]}|jtj	kr3|
t|j|j|jd q|S )zIList deployed Apps along with the number of containers currently running.Nr5   )r+   r,   r-   )r   from_envstubAppListr   AppListRequestr   appsstateAPP_STATE_DEPLOYEDappendr*   r+   descriptionn_running_tasks)r5   r6   resp	app_infos	app_statsr    r    r!   list_deployed_apps@   s"   


rE   r5   r6   r,   c                   sR   |p	t  I dH }tj| ||dI dH }tj|jtjd}|j	|I dH  dS )zStop a deployed App.

    This interface is experimental and may change in the future,
    although the functionality will continue to be supported.
    NrF   )r+   source)
r   r8   r   lookupr   AppStopRequestr+   APP_STOP_SOURCE_PYTHON_CLIENTr9   AppStop)r,   r5   r6   client_appreqr    r    r!   stop_app]   s
   rO   app_namec          	         s   |du rt  I dH }tj| ||dI dH }tj|jd}|j|I dH }i }|j	j
D ]}tj| ||d||< q-|j	jD ]}|drFq>tj| ||d||< q>|S )aw  Experimental interface for retrieving a dictionary of the Functions / Clses in an App.

    The return value is a dictionary mapping names to unhydrated Function or Cls objects.

    We plan to support this functionality through a stable API in the future. It's likely that
    the stable API will look different (it will probably be a method on the App object itself).

    NrF   )r+   r7   z.*)r   r8   r   rH   r   AppGetLayoutRequestr+   r9   AppGetLayout
app_layout	class_idsr   	from_namefunction_idsendswithr   )	rP   r5   r6   rM   rN   app_layout_respapp_objectscls_name	func_namer    r    r!   get_app_objectsj   s   
r\   Fpathforce_buildc                    s&   dt dtf fdd}tj||dS )a  
    Build a Modal Image from a local Dockerfile recipe without any changes.

    Unlike for `modal.Image.from_dockerfile`, the provided recipe will not be embellished with
    steps to install dependencies for the Modal client package. As a consequence, the resulting
    Image cannot be used with a modal Function unless those dependencies are already included
    as part of the base Dockerfile recipe or are added in a subsequent layer. The Image _can_ be
    directly used with a modal Sandbox, which does not need the Modal client.

    We expect to support this experimental function until the `2025.04` Modal Image Builder is
    stable, at which point Modal Image recipes will no longer install the client dependencies
    by default. At that point, users can upgrade their Image Builder Version and migrate to
    `modal.Image.from_dockerfile` for usecases supported by this function.

    versionr'   c                    sJ   t tj }| d}W d    n1 sw   Y  t|i dS )N
commandscontext_files)openosr]   
expanduserreadsplitr   )r_   frb   r]   r    r!   build_dockerfile   s   z.raw_dockerfile_image.<locals>.build_dockerfile)dockerfile_functionr^   )r   r   r   
_from_args)r]   r^   rk   r    rj   r!   raw_dockerfile_image   s   rn   tagregistry_secretcredential_type)staticawsgcpNc                    s   dt dtf fdd}|r;|du rtd|dkrtj}n|dkr&tj}n|d	kr.tj}ntd
|t||}nd}tj	|||dS )a  
    Build a Modal Image from a public or private image registry without any changes.

    Unlike for `modal.Image.from_registry`, the provided recipe will not be embellished with
    steps to install dependencies for the Modal client package. As a consequence, the resulting
    Image cannot be used with a modal Function unless those dependencies are already included
    as part of the registry Image or are added in a subsequent layer. The Image _can_ be
    directly used with a modal Sandbox, which does not need the Modal client.

    We expect to support this experimental function until the `2025.04` Modal Image Builder is
    stable, at which point Modal Image recipes will no longer install the client dependencies
    by default. At that point, users can upgrade their Image Builder Version and migrate to
    `modal.Image.from_registry` for usecases supported by this function.

    r_   r'   c                    s   d  g}t |i dS )NzFROM ra   r   )r_   rb   ro   r    r!   rk      s   z,raw_registry_image.<locals>.build_dockerfileNz=credential_type must be provided when using a registry_secretrr   rs   rt   zInvalid credential_type: )rl   image_registry_configr^   )
r   r   r   r   REGISTRY_AUTH_TYPE_STATIC_CREDSREGISTRY_AUTH_TYPE_AWSREGISTRY_AUTH_TYPE_GCPr   r   rm   )ro   rp   rq   r^   rk   	auth_typeregistry_configr    rv   r!   raw_registry_image   s&   r}   c                  C   sf   d} d}d}d| d|  d| }d\}}| d| }d| d	d
| d	 d| d	 d|  S )z:Command to install CUDA Toolkit (nvcc) inside a container.x86_64debian12zcuda-keyring_1.1-1_all.debz9https://developer.download.nvidia.com/compute/cuda/repos//)      -zwget z && zdpkg -i zrm -f z/apt-get update && apt-get install -y cuda-nvcc-r    )archdistrofilenamecuda_keyring_urlmajorminormax_cuda_versionr    r    r!   _install_cuda_command   s   


r   )python_versionr^   r   c                    s   t j| d}g d}g d}ddt dddtt| dtt| g d	td
tf fdd}t jd|i||t	j
dS )a  Default image used for Modal notebook kernels, with common libraries.

    This can be used to bootstrap development workflows quickly. We don't
    recommend using this image for production Modal Functions though, as it may
    change at any time in the future.
    )r   )1
accelerateaiohttpaltair	anthropicasyncpgbeautifulsoup4bokehz
boto3[crt]clickzdiffusers[torch,flax]z	dm-sonnetflaxftfyh5pyurllib3httpxzhuggingface-hub
ipywidgetszjax[cuda12]keras
matplotlibnbformatnumbanumpyopenaioptaxpandaszplotly[express]polarspsycopg2requestssafetensorszscikit-imagezscikit-learnscipyseabornsentencepiece
sqlalchemystatsmodelssympytabulatetensorboardtomltransformerstritontyperzvega-datasets
watchfiles
websockets)zauthlib>=1.3zbasedpyright>=1.28zfastapi>=0.100zipykernel>=6zpydantic>=2z	pyzmq>=26z
ruff>=0.11zuvicorn>=0.32zapt-get updateznapt-get install -y libpq-dev pkg-config cmake git curl wget unzip zip libsqlite3-dev openssh-server vim ffmpegzpip install uvz[pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu129zuv pip install --system r_   r'   c                    s    t dgdd  D di dS )Nz	FROM basec                 s   s    | ]}d | V  qdS )zRUN Nr    ).0cmdr    r    r!   	<genexpr>Y  s    z@notebook_base_image.<locals>.build_dockerfile.<locals>.<genexpr>z"ENV PATH=/usr/local/cuda/bin:$PATHra   ru   )r_   rb   r    r!   rk   U  s   z-notebook_base_image.<locals>.build_dockerfilebase)base_imagesrl   r^   
_namespace)r   debian_slimr   shlexjoinsortedr   r   rm   r   DEPLOYMENT_NAMESPACE_GLOBAL)r   r^   
base_imageenvironment_packageskernelshim_packagesrk   r    r   r!   notebook_base_image   s&   5
r   )r6   image_idc                   s:   |du rt  I dH }tj| d}|j|I dH  dS )a  Delete an Image by its ID.

    Deletion is irreversible and will prevent Functions/Sandboxes from using
    the Image.

    This is an experimental interface for a feature that we will be adding to
    the main Image class. The stable form of this interface may look different.

    Note: When building an Image, each chained method call will create an
    intermediate Image layer, each with its own ID. Deleting an Image will not
    delete any of its intermediate layers, only the image identified by the
    provided ID.
    N)r   )r   r8   r   ImageDeleteRequestr9   ImageDelete)r   r6   rN   r    r    r!   image_deleteg  s
   r   )r4   N)F)NNF)Cre   r   dataclassesr   pathlibr   typingr   r   r   modal_protor   _clustered_functionsr	   r
   r(   
_functionsr   _objectr   _partial_functionr   _runtime.container_io_managerr   _utils.async_utilsr   r   rM   r   r6   r   clsr   	exceptionr   imager   r   r   r   secretr   flashr   r   r   r   r   r#   r3   r&   r.   	clusteredr*   create_blockingr1   listrE   rO   dictr\   boolrn   r}   r   r   r   r    r    r    r!   <module>   s   ",,
0$l