File size: 6,041 Bytes
22b1ad1
 
 
 
 
 
 
 
 
5968fa3
 
30a8e4b
45d1223
 
705af45
db6eeb9
77a2016
45d1223
b7abaaa
 
 
45d1223
 
057168a
45d1223
057168a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d1223
e060533
45d1223
057168a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d1223
057168a
45d1223
057168a
45d1223
 
057168a
 
 
 
 
 
 
 
45d1223
 
 
057168a
809c9c6
 
 
057168a
809c9c6
06ace8d
809c9c6
06ace8d
 
057168a
45d1223
057168a
06ace8d
 
 
 
 
057168a
 
 
 
06ace8d
 
057168a
06ace8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
title: README
emoji: 👀
colorFrom: blue
colorTo: red
sdk: static
pinned: false
---


<div style="background-color: rgba(255, 255, 255, 0.5); color: rgba(0, 0, 0, 0.87); border: 1px solid rgba(0, 0, 0, 0.5); border-radius: 8px; padding: 0.5rem 1rem;">
  <span style="font-weight: 600;">Spaces Dev Mode is currently in beta.</span> It's available for <a target="_blank" href="https://huggingface.co/subscribe/pro" style="color: inherit;">PRO</a> users (personnal Spaces) or <a target="_blank" href="https://huggingface.co/enterprise" style="color: inherit;">Enterprise</a> organizations (org Spaces).
</div>

<div style="background-color: rgba(255, 255, 255, 0.5); color: rgba(0, 0, 0, 0.87); border: 1px solid rgba(0, 0, 0, 0.5); border-radius: 8px; padding: 0.5rem 1rem; margin-top: 10px; margin-bottom: 30px;">
  <span style="font-weight: 600;">Please share your feedback about Spaces Dev Mode</span> in the <a target="_blank" href="https://huggingface.co/spaces/dev-mode-explorers/README/discussions" style="color: inherit;">Community tab</a>.
</div>

<img src="https://cdn-uploads.huggingface.co/production/uploads/61fd75b93c49561870461907/mLiaon921hu6TMHahf_ty.png" style="width: 100%; max-width:950px"/>


# Spaces Dev Mode (feature preview)

## About Spaces

Spaces offer a simple way to host ML demos and apps on your profile or your organization's profile.
In essence, a Space is a Docker container that runs your application.

Learn more about Spaces here: <a href="https://huggingface.co/docs/hub/spaces" target="_blank">https://huggingface.co/docs/hub/spaces</a>.

## Spaces Dev Mode


Whenever your commit some changes to your Space repo, the underlying Docker image gets rebuilt, and then a new virtual machine is provisioned to host the new container.

The Dev Mode allows you to update your Space much quicker by overriding the Docker image.

The Dev Mode Docker image starts your application as a sub-process, allowing you to restart it without stopping the Space container itself.
It also starts a VS Code server and a SSH server in the background for you to connect to the Space.

The ability to connect to the running Space unlocks several use cases:
- You can make changes to the app code without the Space rebuilding everytime
- You can debug a running application and monitor resources live

Overall it makes developing and experimenting with Spaces much faster by skipping the Docker image rebuild phase.

## Interface

Once the Dev Mode is enabled on your Space, you should see a modal like the following.

<img src="https://cdn-uploads.huggingface.co/production/uploads/5f17f0a0925b9863e28ad517/wSfCELm8WoY_EFhj8l1MM.png" style="max-width: 500px;">

The application does not restart automatically when you change the code. For your changes to appear in the Space, you need to use the `Refresh` button that will restart the app.

<div class="alert alert-warning">
  If you're using the Streamlit or Gradio SDK, or if your application is Pyhton-based, note that requirements are not installed automatically.
  You will need to manually run `pip install` from VS Code or SSH.
</div>

### Persisting changes

<div class="alert alert-warning">
The changes you make when the Dev Mode is enabled are not persisted to the Space repo automatically.
By default, they will be discarded when the Dev Mode is disabled or when the Space goes to sleep.
</div>
If you wish to persist changes made while the Dev Mode is enabled, you need to use `git` from inside the Space container (using VS Code or SSH). For example:

```shell
# Add changes and commit them
git add .
git commit -m "Persist changes from Dev Mode"

# Push the commit to persist them in the repo
git push
```

The modal will display a warning if you have uncommitted or unpushed changes in the Space:


![image/png](https://cdn-uploads.huggingface.co/production/uploads/5fcfb7c407408029ba3577e2/r6Uk1YyvE2-hzsKTSRvnR.png)

### How to enable the Dev Mode

Go to your Space's settings and click on "Enable Dev Mode".


![image/png](https://cdn-uploads.huggingface.co/production/uploads/5fcfb7c407408029ba3577e2/31fExSYIPyxgXm-B9uL4d.png)


You can also enable the Dev Mode from the quick actions dropdown.


![image/png](https://cdn-uploads.huggingface.co/production/uploads/5fcfb7c407408029ba3577e2/t2nQI_5kXY53QIVqBkIx6.png)


## Limitations

The Dev Mode is currently not available for static Spaces. Docker Spaces also have some additional requirements.

### Docker Spaces

Dev Mode is supported for Docker Spaces. However, your Space needs to comply with the following rules for the Dev Mode to work properly.

1. The following packages must be installed:

- `bash` (required to establish SSH connections)
- `curl`, `wget` and `procps` (required by the VS Code server process)
- `git` and `git-lfs` to be able to commit and push changes from your Dev Mode environment

2. Your application code must be located in the `/app` folder for the Dev Mode daemon to be able to detect changes.

3. The `/app` folder must be owned by the user with uid `1000` to allow you to make changes to the code.

4. The Dockerfile must contain a `CMD` instruction for startup. Checkout [Docker's documentation](https://docs.docker.com/reference/dockerfile/#cmd) about the `CMD` instruction for more details.

The Dev Mode works well when the base image is debian-based (eg, ubuntu).

More exotic linux distros (eg, alpine) are not tested and the Dev Mode is not guaranteed to work on them.

### Example of a compatible Dockerfile

This is an example of a Dockerfile compatible with Spaces Dev Mode.

It installs the required packages with `apt-get`, along with a couple more for developer convenience (namely: `top`, `vim` and `nano`).
It then starts a NodeJS application from `/app`.

```Dockerfile
FROM node:19-slim

RUN RUN apt-get update && \
    apt-get install -y \
      bash \
      git git-lfs \
      wget curl procps \
      htop vim nano && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY --link --chown=1000 ./ /app
RUN  npm ci 

CMD ["node", "index.mjs"]
```