hamel commited on
Commit
484e78f
1 Parent(s): fecd672

Add application files

Browse files
MANIFEST.in DELETED
@@ -1,5 +0,0 @@
1
- include settings.ini
2
- include LICENSE
3
- include CONTRIBUTING.md
4
- include README.md
5
- recursive-exclude * __pycache__
 
 
 
 
 
 
README.md CHANGED
@@ -1,27 +1,188 @@
1
- nbdev-spaces-demo
2
- ================
 
 
 
 
 
 
 
 
 
3
 
4
- <!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
5
 
6
- This is a toy python library that lets you obtain the size of any
7
- Hugging Face dataset. For example, we can check the size of
8
- [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128)
9
- like so:
10
 
11
- ``` python
12
- from nbdev_spaces_demo import hfsize
13
- hfsize("tglcourse/CelebA-faces-cropped-128")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  ```
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  '5.49 GB'
17
 
18
- We deploy this function using Gradio and Hugging Face spaces.
19
 
20
- ## Using nbdev with Gradio
21
 
22
- Gradio and Hugging Face spaces is one of the easiest way to create and
23
- host apps. Gradio also allows you to prototype these apps in notebooks,
24
- which is excellent!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- We show you step-by-step instructions on how to deploy a Hugging Face
27
- gradio app from a notebook in this example.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Foo
3
+ emoji: 🐢
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.9
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
 
13
+ <!-- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference --># Hugging Face Spaces From A Notebook
14
 
15
+ > A demo of using nbdev with Hugging Face Spaces
 
 
 
16
 
17
+ ## 1. Create a Gradio-enabled Space on Hugging Face
18
+
19
+ The first step is to create a space and select the appropriate sdk (which is Gradio in this example), per [these instructions](https://huggingface.co/docs/hub/spaces-overview#creating-a-new-space):
20
+
21
+ ![](./create_space.png)
22
+
23
+ After you are done creating the space, **clone the repo per the instructions provided in the app.** In this example, I ran the command `git clone https://huggingface.co/spaces/hamel/hfspace_demo`.
24
+
25
+ ## 2. Make an app with Gradio
26
+
27
+ Below, we will create a [gradio](https://gradio.app/) app in a notebook and show you how to deploy it to [Hugging Face Spaces](https://huggingface.co/docs/hub/spaces).
28
+
29
+ First, lets specify the libraries we need, which in this case are `gradio` and `fastcore`:
30
+
31
+
32
+ ```
33
+ #|export app
34
+ import gradio as gr
35
+ from fastcore.net import urljson, HTTPError
36
+ ```
37
+
38
+
39
+ ```
40
+ #|export
41
+ def size(repo:str):
42
+ "Returns the size in GB of a HuggingFace Dataset."
43
+ url = f'https://huggingface.co/api/datasets/{repo}'
44
+ try: resp = urljson(f'{url}/treesize/main')
45
+ except HTTPError: return f'Did not find repo: {url}'
46
+ gb = resp['size'] / 1e9
47
+ return f'{gb:.2f} GB'
48
  ```
49
 
50
+ `size` take as an input a [Hugging Face Dataset](https://huggingface.co/docs/datasets/index) repo and returns the total size in GB of the data.
51
+
52
+ For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:
53
+
54
+
55
+ ```
56
+ size("tglcourse/CelebA-faces-cropped-128")
57
+ ```
58
+
59
+
60
+
61
+
62
  '5.49 GB'
63
 
 
64
 
 
65
 
66
+ You can construct a simple UI with the `gradio.interface` and then call the `launch` method of that interface to display a preview in a notebook. This is a great way to test your app to see if it works
67
+
68
+
69
+ ```
70
+ #|export
71
+ iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
72
+ iface.launch(width=500)
73
+ ```
74
+
75
+ Running on local URL: http://127.0.0.1:7860
76
+
77
+ To create a public link, set `share=True` in `launch()`.
78
+
79
+
80
+
81
+ <div><iframe src="http://127.0.0.1:7860/" width="500" height="500" allow="autoplay; camera; microphone; clipboard-read; clipboard-write;" frameborder="0" allowfullscreen></iframe></div>
82
+
83
+
84
+
85
+
86
+
87
+ (<gradio.routes.App>, 'http://127.0.0.1:7860/', None)
88
+
89
+
90
+
91
+ Note how running the `launch()` method in a notebook runs a webserver in the background. Below, we call the `close()` method to close the webserver.
92
+
93
+
94
+ ```
95
+ # this is only necessary in a notebook
96
+ iface.close()
97
+ ```
98
+
99
+ Closing server running on port: 7860
100
 
101
+
102
+ ## 3. Converting This Notebook Into A Gradio App
103
+
104
+ In order to host this code on Hugging Faces spaces, you will export parts of this notebook to a script named `app.py`. That is what the special `#|export` comment that you have seen in cells above do! You can export code from this notebook like so:
105
+
106
+
107
+ ```
108
+ from nbdev.export import nb_export
109
+ nb_export('app.ipynb', lib_path='.', name='app')
110
+ ```
111
+
112
+ <div>
113
+ <link rel="stylesheet" href="https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.css">
114
+ <div id="target"></div>
115
+ <script src="https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.js"></script>
116
+ <script>
117
+ launchGradioFromSpaces("abidlabs/question-answering", "#target")
118
+ </script>
119
+ </div>
120
+
121
+ ### Understanding what is generated
122
+
123
+ Notice how the contents of app.py only contains the exported cells from this notebook:
124
+
125
+
126
+ ```
127
+ %pycat app.py
128
+ ```
129
+
130
+
131
+ # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
132
+ 
133
+ # %% auto 0
134
+ __all__ = ['iface', 'size']
135
+ 
136
+ # %% app.ipynb 7
137
+ def size(repo:str):
138
+  "Returns the size in GB of a HuggingFace Dataset."
139
+  url = f'https://huggingface.co/api/datasets/{repo}'
140
+  try: resp = urljson(f'{url}/treesize/main')
141
+  except HTTPError: return f'Did not find repo: {url}'
142
+  gb = resp['size'] / 1e9
143
+  return f'{gb:.2f} GB'
144
+ 
145
+ # %% app.ipynb 11
146
+ iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
147
+ iface.launch(width=500)
148
+
149
+
150
+
151
+ ### Fill out `requirements.txt`
152
+
153
+ You must supply a requirements.txt file so the gradio app knows how to build your dependencies. In this example, the only depdency other than gradio is `fastcore`. You don't need to specify gradio itself as a depdendency in `requirements.txt` so our `requirements.txt` file has only one dependency:
154
+
155
+
156
+ ```
157
+ !cat requirements.txt
158
+ ```
159
+
160
+ fastcore
161
+
162
+ ## 4. Launch Your Gradio App
163
+
164
+ To launch your gradio app, you need to commit the changes in the Hugging Face repo:
165
+
166
+ ```
167
+ git add -A; git commit -m "Add application files"; git push
168
+ ```
169
+
170
+ ## 5. Voilà! Enjoy your Gradio App
171
+
172
+ After a couple of minutes, you will see your app published!
173
+
174
+
175
+ ```
176
+ # this is only for hamel, you can ignore this.
177
+ !jupyter nbconvert --to markdown app.ipynb
178
+ !cat yaml.md app.md > README.md
179
+ ```
180
+
181
+ [NbConvertApp] Converting notebook app.ipynb to markdown
182
+ [NbConvertApp] Writing 6113 bytes to app.md
183
+
184
+
185
+
186
+ ```
187
+
188
+ ```
app.ipynb CHANGED
@@ -5,21 +5,9 @@
5
  "id": "8c68f03e-620c-46a9-a7ec-a6cde27043cd",
6
  "metadata": {},
7
  "source": [
8
- "# Hugging Face Spaces\n",
9
  "\n",
10
- "> A demo of using nbdev with Hugging Face Spaces\n",
11
- "\n",
12
- "Hugging Face spaces require that your python script is named `app.py`, so your first cell should be this, which will make sure code is exported to a file named `app.py`:"
13
- ]
14
- },
15
- {
16
- "cell_type": "code",
17
- "execution_count": null,
18
- "id": "c3463e8e-454a-48b8-ae21-8308703d2275",
19
- "metadata": {},
20
- "outputs": [],
21
- "source": [
22
- "#|default_exp app"
23
  ]
24
  },
25
  {
@@ -27,7 +15,7 @@
27
  "id": "96483373-4ae1-49b2-85ed-ceee8456df19",
28
  "metadata": {},
29
  "source": [
30
- "# Create a Gradio-enabled Space on Hugging Face\n",
31
  "\n",
32
  "The first step is to create a space and select the appropriate sdk (which is Gradio in this example), per [these instructions](https://huggingface.co/docs/hub/spaces-overview#creating-a-new-space):"
33
  ]
@@ -45,7 +33,7 @@
45
  "id": "c25e8e7a-52d9-4305-a107-ba03e3d6a5f3",
46
  "metadata": {},
47
  "source": [
48
- "After you are done creating the space, **clone the repo to the root of your nbdev project.** In this example, I ran the command `git clone https://huggingface.co/spaces/hamel/hfspace_demo` from the root of this repository."
49
  ]
50
  },
51
  {
@@ -53,7 +41,7 @@
53
  "id": "ff26114c-329b-4a97-98b5-c652554b0114",
54
  "metadata": {},
55
  "source": [
56
- "## Make an app with Gradio"
57
  ]
58
  },
59
  {
@@ -61,9 +49,9 @@
61
  "id": "14a884fc-36e2-43ec-8e42-ca2903aaa4de",
62
  "metadata": {},
63
  "source": [
64
- "Below, we will create a [gradio](https://gradio.app/) in a notebook and show you how to deploy it to [Hugging Face Spaces](https://huggingface.co/docs/hub/spaces).\n",
65
  "\n",
66
- "First, lets specify the libraries we need, which in this case are gradio, and the nbdev project which in this case is `nbdev_spaces_demo`:"
67
  ]
68
  },
69
  {
@@ -73,9 +61,26 @@
73
  "metadata": {},
74
  "outputs": [],
75
  "source": [
76
- "#|export\n",
77
  "import gradio as gr\n",
78
- "from nbdev_spaces_demo import hfsize "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  ]
80
  },
81
  {
@@ -83,7 +88,9 @@
83
  "id": "9ff9f84d-7744-46ad-80ed-2cf1fa6d0643",
84
  "metadata": {},
85
  "source": [
86
- "As a reminder, `hfsize` can be used to check the size of a Hugging Face Dataset. For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:"
 
 
87
  ]
88
  },
89
  {
@@ -104,7 +111,7 @@
104
  }
105
  ],
106
  "source": [
107
- "hfsize(\"tglcourse/CelebA-faces-cropped-128\")"
108
  ]
109
  },
110
  {
@@ -155,7 +162,7 @@
155
  ],
156
  "source": [
157
  "#|export\n",
158
- "iface = gr.Interface(fn=hfsize, inputs=gr.Text(value=\"tglcourse/CelebA-faces-cropped-128\"), outputs=\"text\")\n",
159
  "iface.launch(width=500)"
160
  ]
161
  },
@@ -191,7 +198,7 @@
191
  "id": "249b2cd7-3123-45bf-945f-882b8a964cf5",
192
  "metadata": {},
193
  "source": [
194
- "## Converting This Notebook Into A Gradio App"
195
  ]
196
  },
197
  {
@@ -199,18 +206,7 @@
199
  "id": "5c18ca6e-8de8-49e1-b95a-304070bbc171",
200
  "metadata": {},
201
  "source": [
202
- "In order to host this code on Hugging Faces spaces, we need to do the following:\n",
203
- "\n",
204
- "1. Export parts of this notebook to a script named `app.py`\n",
205
- "2. Create a `requirements.txt` file specifying all the dependencies of the gradio app which is inferred from `settings.ini`"
206
- ]
207
- },
208
- {
209
- "cell_type": "markdown",
210
- "id": "1971847f-8d70-429b-8dd2-292d3e329266",
211
- "metadata": {},
212
- "source": [
213
- "We can achieve this with the below code, note how we are exporting the code to the `hfspace_demo/` directory, which is the repo we cloned in the first step."
214
  ]
215
  },
216
  {
@@ -221,30 +217,7 @@
221
  "outputs": [],
222
  "source": [
223
  "from nbdev.export import nb_export\n",
224
- "from nbdev.release import write_requirements\n",
225
- "\n",
226
- "app_dir = 'hfspace_demo/'\n",
227
- "nb_export('app.ipynb', app_dir)\n",
228
- "write_requirements(app_dir)"
229
- ]
230
- },
231
- {
232
- "cell_type": "markdown",
233
- "id": "662a58d6-a907-4a7f-962e-35859687a1e4",
234
- "metadata": {},
235
- "source": [
236
- "We can vendor in our python library generated with nbdev (named `nbdev_spaces_demo`) into the directory as well like so:"
237
- ]
238
- },
239
- {
240
- "cell_type": "code",
241
- "execution_count": null,
242
- "id": "128b042e-7a8a-4485-94af-46e7efee8557",
243
- "metadata": {},
244
- "outputs": [],
245
- "source": [
246
- "from nbdev.config import get_config\n",
247
- "!cp -r {str(get_config().lib_path)} {app_dir}"
248
  ]
249
  },
250
  {
@@ -270,32 +243,6 @@
270
  "### Understanding what is generated"
271
  ]
272
  },
273
- {
274
- "cell_type": "markdown",
275
- "id": "f0b783f0-cd5a-4092-b19c-8d05a978ce3c",
276
- "metadata": {},
277
- "source": [
278
- "The contents of the hfspace_demo/ folder will contain these assets:"
279
- ]
280
- },
281
- {
282
- "cell_type": "code",
283
- "execution_count": null,
284
- "id": "a8d6b05f-1d17-4000-b82a-7fd4eb3092c5",
285
- "metadata": {},
286
- "outputs": [
287
- {
288
- "name": "stdout",
289
- "output_type": "stream",
290
- "text": [
291
- "README.md app.py \u001b[1m\u001b[36mnbdev_spaces_demo\u001b[m\u001b[m requirements.txt\n"
292
- ]
293
- }
294
- ],
295
- "source": [
296
- "!ls hfspace_demo/"
297
- ]
298
- },
299
  {
300
  "cell_type": "markdown",
301
  "id": "9ea562e7-b67a-45df-b822-2f4528a307c2",
@@ -313,17 +260,22 @@
313
  {
314
  "data": {
315
  "text/plain": [
316
- "\u001b[0;31m# AUTOGENERATED! DO NOT EDIT! File to edit: ../app.ipynb.\u001b[0m\u001b[0;34m\u001b[0m\n",
317
  "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
318
  "\u001b[0;34m\u001b[0m\u001b[0;31m# %% auto 0\u001b[0m\u001b[0;34m\u001b[0m\n",
319
- "\u001b[0;34m\u001b[0m\u001b[0m__all__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'iface'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n",
320
  "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
321
- "\u001b[0;34m\u001b[0m\u001b[0;31m# %% ../app.ipynb 7\u001b[0m\u001b[0;34m\u001b[0m\n",
322
- "\u001b[0;34m\u001b[0m\u001b[0;32mimport\u001b[0m \u001b[0mgradio\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m\u001b[0m\n",
323
- "\u001b[0;34m\u001b[0m\u001b[0;32mfrom\u001b[0m \u001b[0mnbdev_spaces_demo\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mhfsize\u001b[0m \u001b[0;34m\u001b[0m\n",
 
 
 
 
 
324
  "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
325
- "\u001b[0;34m\u001b[0m\u001b[0;31m# %% ../app.ipynb 11\u001b[0m\u001b[0;34m\u001b[0m\n",
326
- "\u001b[0;34m\u001b[0m\u001b[0miface\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mInterface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhfsize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mText\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"tglcourse/CelebA-faces-cropped-128\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"text\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
327
  "\u001b[0;34m\u001b[0m\u001b[0miface\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m500\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n"
328
  ]
329
  },
@@ -332,33 +284,35 @@
332
  }
333
  ],
334
  "source": [
335
- "%pycat hfspace_demo/app.py"
336
  ]
337
  },
338
  {
339
  "cell_type": "markdown",
340
- "id": "aadb4817-0671-4d05-9abc-d16776e2bec7",
341
  "metadata": {},
342
  "source": [
343
- "Similarly, the contents of requirements.txt contain all dependencies listed in `settings.ini` from the `requirments` and `pip_requirements` fields (which in this case is just `fastcore`:"
 
 
344
  ]
345
  },
346
  {
347
  "cell_type": "code",
348
  "execution_count": null,
349
- "id": "831333c4-5e67-46fd-bd73-81a61cbcbd86",
350
  "metadata": {},
351
  "outputs": [
352
  {
353
  "name": "stdout",
354
  "output_type": "stream",
355
  "text": [
356
- "fastcore\n"
357
  ]
358
  }
359
  ],
360
  "source": [
361
- "!cat hfspace_demo/requirements.txt"
362
  ]
363
  },
364
  {
@@ -366,15 +320,9 @@
366
  "id": "f15d9c78-1f55-449e-8058-9af1832367a0",
367
  "metadata": {},
368
  "source": [
369
- "## Launching Your Gradio App\n",
370
- "\n",
371
- "To launch your gradio app, you need to commit the changes in the Hugging Face repo. \n",
372
- "\n",
373
- "First, change directories to your huggingface repo (in this case its a directory called `hfspace_demo/`:\n",
374
  "\n",
375
- "`cd hfspace_demo`\n",
376
- "\n",
377
- "Then commit all changes\n",
378
  "\n",
379
  "```\n",
380
  "git add -A; git commit -m \"Add application files\"; git push\n",
@@ -386,7 +334,7 @@
386
  "id": "fa661f93-73b4-465a-9c22-cc38197505cb",
387
  "metadata": {},
388
  "source": [
389
- "## Voilà! Enjoy your Gradio App"
390
  ]
391
  },
392
  {
@@ -396,6 +344,35 @@
396
  "source": [
397
  "After a couple of minutes, you will see your app published! "
398
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  }
400
  ],
401
  "metadata": {
 
5
  "id": "8c68f03e-620c-46a9-a7ec-a6cde27043cd",
6
  "metadata": {},
7
  "source": [
8
+ "# Hugging Face Spaces From A Notebook\n",
9
  "\n",
10
+ "> A demo of using nbdev with Hugging Face Spaces"
 
 
 
 
 
 
 
 
 
 
 
 
11
  ]
12
  },
13
  {
 
15
  "id": "96483373-4ae1-49b2-85ed-ceee8456df19",
16
  "metadata": {},
17
  "source": [
18
+ "## 1. Create a Gradio-enabled Space on Hugging Face\n",
19
  "\n",
20
  "The first step is to create a space and select the appropriate sdk (which is Gradio in this example), per [these instructions](https://huggingface.co/docs/hub/spaces-overview#creating-a-new-space):"
21
  ]
 
33
  "id": "c25e8e7a-52d9-4305-a107-ba03e3d6a5f3",
34
  "metadata": {},
35
  "source": [
36
+ "After you are done creating the space, **clone the repo per the instructions provided in the app.** In this example, I ran the command `git clone https://huggingface.co/spaces/hamel/hfspace_demo`."
37
  ]
38
  },
39
  {
 
41
  "id": "ff26114c-329b-4a97-98b5-c652554b0114",
42
  "metadata": {},
43
  "source": [
44
+ "## 2. Make an app with Gradio"
45
  ]
46
  },
47
  {
 
49
  "id": "14a884fc-36e2-43ec-8e42-ca2903aaa4de",
50
  "metadata": {},
51
  "source": [
52
+ "Below, we will create a [gradio](https://gradio.app/) app in a notebook and show you how to deploy it to [Hugging Face Spaces](https://huggingface.co/docs/hub/spaces).\n",
53
  "\n",
54
+ "First, lets specify the libraries we need, which in this case are `gradio` and `fastcore`:"
55
  ]
56
  },
57
  {
 
61
  "metadata": {},
62
  "outputs": [],
63
  "source": [
64
+ "#|export app\n",
65
  "import gradio as gr\n",
66
+ "from fastcore.net import urljson, HTTPError"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": null,
72
+ "id": "38a4389f-ef53-4626-a6f5-a859354f854b",
73
+ "metadata": {},
74
+ "outputs": [],
75
+ "source": [
76
+ "#|export\n",
77
+ "def size(repo:str):\n",
78
+ " \"Returns the size in GB of a HuggingFace Dataset.\"\n",
79
+ " url = f'https://huggingface.co/api/datasets/{repo}'\n",
80
+ " try: resp = urljson(f'{url}/treesize/main')\n",
81
+ " except HTTPError: return f'Did not find repo: {url}'\n",
82
+ " gb = resp['size'] / 1e9\n",
83
+ " return f'{gb:.2f} GB'"
84
  ]
85
  },
86
  {
 
88
  "id": "9ff9f84d-7744-46ad-80ed-2cf1fa6d0643",
89
  "metadata": {},
90
  "source": [
91
+ "`size` take as an input a [Hugging Face Dataset](https://huggingface.co/docs/datasets/index) repo and returns the total size in GB of the data.\n",
92
+ "\n",
93
+ "For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:"
94
  ]
95
  },
96
  {
 
111
  }
112
  ],
113
  "source": [
114
+ "size(\"tglcourse/CelebA-faces-cropped-128\")"
115
  ]
116
  },
117
  {
 
162
  ],
163
  "source": [
164
  "#|export\n",
165
+ "iface = gr.Interface(fn=size, inputs=gr.Text(value=\"tglcourse/CelebA-faces-cropped-128\"), outputs=\"text\")\n",
166
  "iface.launch(width=500)"
167
  ]
168
  },
 
198
  "id": "249b2cd7-3123-45bf-945f-882b8a964cf5",
199
  "metadata": {},
200
  "source": [
201
+ "## 3. Converting This Notebook Into A Gradio App"
202
  ]
203
  },
204
  {
 
206
  "id": "5c18ca6e-8de8-49e1-b95a-304070bbc171",
207
  "metadata": {},
208
  "source": [
209
+ "In order to host this code on Hugging Faces spaces, you will export parts of this notebook to a script named `app.py`. That is what the special `#|export` comment that you have seen in cells above do! You can export code from this notebook like so:"
 
 
 
 
 
 
 
 
 
 
 
210
  ]
211
  },
212
  {
 
217
  "outputs": [],
218
  "source": [
219
  "from nbdev.export import nb_export\n",
220
+ "nb_export('app.ipynb', lib_path='.', name='app')"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  ]
222
  },
223
  {
 
243
  "### Understanding what is generated"
244
  ]
245
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  {
247
  "cell_type": "markdown",
248
  "id": "9ea562e7-b67a-45df-b822-2f4528a307c2",
 
260
  {
261
  "data": {
262
  "text/plain": [
263
+ "\u001b[0;31m# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.\u001b[0m\u001b[0;34m\u001b[0m\n",
264
  "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
265
  "\u001b[0;34m\u001b[0m\u001b[0;31m# %% auto 0\u001b[0m\u001b[0;34m\u001b[0m\n",
266
+ "\u001b[0;34m\u001b[0m\u001b[0m__all__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'iface'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'size'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\n",
267
  "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
268
+ "\u001b[0;34m\u001b[0m\u001b[0;31m# %% app.ipynb 7\u001b[0m\u001b[0;34m\u001b[0m\n",
269
+ "\u001b[0;34m\u001b[0m\u001b[0;32mdef\u001b[0m \u001b[0msize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrepo\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
270
+ "\u001b[0;34m\u001b[0m \u001b[0;34m\"Returns the size in GB of a HuggingFace Dataset.\"\u001b[0m\u001b[0;34m\u001b[0m\n",
271
+ "\u001b[0;34m\u001b[0m \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34mf'https://huggingface.co/api/datasets/{repo}'\u001b[0m\u001b[0;34m\u001b[0m\n",
272
+ "\u001b[0;34m\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murljson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf'{url}/treesize/main'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
273
+ "\u001b[0;34m\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mHTTPError\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34mf'Did not find repo: {url}'\u001b[0m\u001b[0;34m\u001b[0m\n",
274
+ "\u001b[0;34m\u001b[0m \u001b[0mgb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'size'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m1e9\u001b[0m\u001b[0;34m\u001b[0m\n",
275
+ "\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34mf'{gb:.2f} GB'\u001b[0m\u001b[0;34m\u001b[0m\n",
276
  "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
277
+ "\u001b[0;34m\u001b[0m\u001b[0;31m# %% app.ipynb 11\u001b[0m\u001b[0;34m\u001b[0m\n",
278
+ "\u001b[0;34m\u001b[0m\u001b[0miface\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mInterface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mText\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"tglcourse/CelebA-faces-cropped-128\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"text\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
279
  "\u001b[0;34m\u001b[0m\u001b[0miface\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m500\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n"
280
  ]
281
  },
 
284
  }
285
  ],
286
  "source": [
287
+ "%pycat app.py"
288
  ]
289
  },
290
  {
291
  "cell_type": "markdown",
292
+ "id": "a081bb0f-5cad-4b99-962b-4dd49cee61a2",
293
  "metadata": {},
294
  "source": [
295
+ "### Fill out `requirements.txt`\n",
296
+ "\n",
297
+ "You must supply a requirements.txt file so the gradio app knows how to build your dependencies. In this example, the only depdency other than gradio is `fastcore`. You don't need to specify gradio itself as a depdendency in `requirements.txt` so our `requirements.txt` file has only one dependency:"
298
  ]
299
  },
300
  {
301
  "cell_type": "code",
302
  "execution_count": null,
303
+ "id": "0b611d9c-d262-4124-9e9e-4fe754ac4378",
304
  "metadata": {},
305
  "outputs": [
306
  {
307
  "name": "stdout",
308
  "output_type": "stream",
309
  "text": [
310
+ "fastcore"
311
  ]
312
  }
313
  ],
314
  "source": [
315
+ "!cat requirements.txt"
316
  ]
317
  },
318
  {
 
320
  "id": "f15d9c78-1f55-449e-8058-9af1832367a0",
321
  "metadata": {},
322
  "source": [
323
+ "## 4. Launch Your Gradio App\n",
 
 
 
 
324
  "\n",
325
+ "To launch your gradio app, you need to commit the changes in the Hugging Face repo:\n",
 
 
326
  "\n",
327
  "```\n",
328
  "git add -A; git commit -m \"Add application files\"; git push\n",
 
334
  "id": "fa661f93-73b4-465a-9c22-cc38197505cb",
335
  "metadata": {},
336
  "source": [
337
+ "## 5. Voilà! Enjoy your Gradio App"
338
  ]
339
  },
340
  {
 
344
  "source": [
345
  "After a couple of minutes, you will see your app published! "
346
  ]
347
+ },
348
+ {
349
+ "cell_type": "code",
350
+ "execution_count": null,
351
+ "id": "9a4f7c06-406a-4a7d-be6b-6cb606c35d8d",
352
+ "metadata": {},
353
+ "outputs": [
354
+ {
355
+ "name": "stdout",
356
+ "output_type": "stream",
357
+ "text": [
358
+ "[NbConvertApp] Converting notebook app.ipynb to markdown\n",
359
+ "[NbConvertApp] Writing 6113 bytes to app.md\n"
360
+ ]
361
+ }
362
+ ],
363
+ "source": [
364
+ "# this is only for hamel, you can ignore this.\n",
365
+ "!jupyter nbconvert --to markdown app.ipynb \n",
366
+ "!cat yaml.md app.md > README.md "
367
+ ]
368
+ },
369
+ {
370
+ "cell_type": "code",
371
+ "execution_count": null,
372
+ "id": "958174fe-537e-4635-90b8-22ac11eae396",
373
+ "metadata": {},
374
+ "outputs": [],
375
+ "source": []
376
  }
377
  ],
378
  "metadata": {
app.md ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Spaces From A Notebook
2
+
3
+ > A demo of using nbdev with Hugging Face Spaces
4
+
5
+ ## 1. Create a Gradio-enabled Space on Hugging Face
6
+
7
+ The first step is to create a space and select the appropriate sdk (which is Gradio in this example), per [these instructions](https://huggingface.co/docs/hub/spaces-overview#creating-a-new-space):
8
+
9
+ ![](./create_space.png)
10
+
11
+ After you are done creating the space, **clone the repo per the instructions provided in the app.** In this example, I ran the command `git clone https://huggingface.co/spaces/hamel/hfspace_demo`.
12
+
13
+ ## 2. Make an app with Gradio
14
+
15
+ Below, we will create a [gradio](https://gradio.app/) app in a notebook and show you how to deploy it to [Hugging Face Spaces](https://huggingface.co/docs/hub/spaces).
16
+
17
+ First, lets specify the libraries we need, which in this case are `gradio` and `fastcore`:
18
+
19
+
20
+ ```
21
+ #|export app
22
+ import gradio as gr
23
+ from fastcore.net import urljson, HTTPError
24
+ ```
25
+
26
+
27
+ ```
28
+ #|export
29
+ def size(repo:str):
30
+ "Returns the size in GB of a HuggingFace Dataset."
31
+ url = f'https://huggingface.co/api/datasets/{repo}'
32
+ try: resp = urljson(f'{url}/treesize/main')
33
+ except HTTPError: return f'Did not find repo: {url}'
34
+ gb = resp['size'] / 1e9
35
+ return f'{gb:.2f} GB'
36
+ ```
37
+
38
+ `size` take as an input a [Hugging Face Dataset](https://huggingface.co/docs/datasets/index) repo and returns the total size in GB of the data.
39
+
40
+ For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:
41
+
42
+
43
+ ```
44
+ size("tglcourse/CelebA-faces-cropped-128")
45
+ ```
46
+
47
+
48
+
49
+
50
+ '5.49 GB'
51
+
52
+
53
+
54
+ You can construct a simple UI with the `gradio.interface` and then call the `launch` method of that interface to display a preview in a notebook. This is a great way to test your app to see if it works
55
+
56
+
57
+ ```
58
+ #|export
59
+ iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
60
+ iface.launch(width=500)
61
+ ```
62
+
63
+ Running on local URL: http://127.0.0.1:7860
64
+
65
+ To create a public link, set `share=True` in `launch()`.
66
+
67
+
68
+
69
+ <div><iframe src="http://127.0.0.1:7860/" width="500" height="500" allow="autoplay; camera; microphone; clipboard-read; clipboard-write;" frameborder="0" allowfullscreen></iframe></div>
70
+
71
+
72
+
73
+
74
+
75
+ (<gradio.routes.App>, 'http://127.0.0.1:7860/', None)
76
+
77
+
78
+
79
+ Note how running the `launch()` method in a notebook runs a webserver in the background. Below, we call the `close()` method to close the webserver.
80
+
81
+
82
+ ```
83
+ # this is only necessary in a notebook
84
+ iface.close()
85
+ ```
86
+
87
+ Closing server running on port: 7860
88
+
89
+
90
+ ## 3. Converting This Notebook Into A Gradio App
91
+
92
+ In order to host this code on Hugging Faces spaces, you will export parts of this notebook to a script named `app.py`. That is what the special `#|export` comment that you have seen in cells above do! You can export code from this notebook like so:
93
+
94
+
95
+ ```
96
+ from nbdev.export import nb_export
97
+ nb_export('app.ipynb', lib_path='.', name='app')
98
+ ```
99
+
100
+ <div>
101
+ <link rel="stylesheet" href="https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.css">
102
+ <div id="target"></div>
103
+ <script src="https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.js"></script>
104
+ <script>
105
+ launchGradioFromSpaces("abidlabs/question-answering", "#target")
106
+ </script>
107
+ </div>
108
+
109
+ ### Understanding what is generated
110
+
111
+ Notice how the contents of app.py only contains the exported cells from this notebook:
112
+
113
+
114
+ ```
115
+ %pycat app.py
116
+ ```
117
+
118
+
119
+ # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
120
+ 
121
+ # %% auto 0
122
+ __all__ = ['iface', 'size']
123
+ 
124
+ # %% app.ipynb 7
125
+ def size(repo:str):
126
+  "Returns the size in GB of a HuggingFace Dataset."
127
+  url = f'https://huggingface.co/api/datasets/{repo}'
128
+  try: resp = urljson(f'{url}/treesize/main')
129
+  except HTTPError: return f'Did not find repo: {url}'
130
+  gb = resp['size'] / 1e9
131
+  return f'{gb:.2f} GB'
132
+ 
133
+ # %% app.ipynb 11
134
+ iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
135
+ iface.launch(width=500)
136
+
137
+
138
+
139
+ ### Fill out `requirements.txt`
140
+
141
+ You must supply a requirements.txt file so the gradio app knows how to build your dependencies. In this example, the only depdency other than gradio is `fastcore`. You don't need to specify gradio itself as a depdendency in `requirements.txt` so our `requirements.txt` file has only one dependency:
142
+
143
+
144
+ ```
145
+ !cat requirements.txt
146
+ ```
147
+
148
+ fastcore
149
+
150
+ ## 4. Launch Your Gradio App
151
+
152
+ To launch your gradio app, you need to commit the changes in the Hugging Face repo:
153
+
154
+ ```
155
+ git add -A; git commit -m "Add application files"; git push
156
+ ```
157
+
158
+ ## 5. Voilà! Enjoy your Gradio App
159
+
160
+ After a couple of minutes, you will see your app published!
161
+
162
+
163
+ ```
164
+ # this is only for hamel, you can ignore this.
165
+ !jupyter nbconvert --to markdown app.ipynb
166
+ !cat yaml.md app.md > README.md
167
+ ```
168
+
169
+ [NbConvertApp] Converting notebook app.ipynb to markdown
170
+ [NbConvertApp] Writing 6113 bytes to app.md
171
+
172
+
173
+
174
+ ```
175
+
176
+ ```
nbdev_spaces_demo/size.py → app.py RENAMED
@@ -1,15 +1,17 @@
1
- # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/size.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['hfsize']
5
 
6
- # %% ../nbs/size.ipynb 2
7
- from fastcore.net import urljson, HTTPError
8
-
9
- def hfsize(repo:str):
10
  "Returns the size in GB of a HuggingFace Dataset."
11
  url = f'https://huggingface.co/api/datasets/{repo}'
12
  try: resp = urljson(f'{url}/treesize/main')
13
  except HTTPError: return f'Did not find repo: {url}'
14
  gb = resp['size'] / 1e9
15
  return f'{gb:.2f} GB'
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['iface', 'size']
5
 
6
+ # %% app.ipynb 7
7
+ def size(repo:str):
 
 
8
  "Returns the size in GB of a HuggingFace Dataset."
9
  url = f'https://huggingface.co/api/datasets/{repo}'
10
  try: resp = urljson(f'{url}/treesize/main')
11
  except HTTPError: return f'Did not find repo: {url}'
12
  gb = resp['size'] / 1e9
13
  return f'{gb:.2f} GB'
14
+
15
+ # %% app.ipynb 11
16
+ iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
17
+ iface.launch(width=500)
hfspace_demo DELETED
@@ -1 +0,0 @@
1
- Subproject commit 4b24ff04a9705aaaf5a62a209ab654394e665b20
 
 
nbdev_spaces_demo/__init__.py DELETED
@@ -1,3 +0,0 @@
1
- __version__ = "0.0.1"
2
-
3
- from .size import hfsize
 
 
 
 
nbdev_spaces_demo/_modidx.py DELETED
@@ -1,8 +0,0 @@
1
- # Autogenerated by nbdev
2
-
3
- d = { 'settings': { 'branch': 'master',
4
- 'doc_baseurl': '/nbdev-spaces-demo',
5
- 'doc_host': 'https://fastai.github.io',
6
- 'git_url': 'https://github.com/fastai/nbdev-spaces-demo',
7
- 'lib_path': 'nbdev_spaces_demo'},
8
- 'syms': {'nbdev_spaces_demo.size': {'nbdev_spaces_demo.size.hfsize': ('size.html#hfsize', 'nbdev_spaces_demo/size.py')}}}
 
 
 
 
 
 
 
 
 
nbs/_quarto.yml DELETED
@@ -1,20 +0,0 @@
1
- project:
2
- type: website
3
-
4
- format:
5
- html:
6
- theme: cosmo
7
- css: styles.css
8
- toc: true
9
-
10
- website:
11
- twitter-card: true
12
- open-graph: true
13
- repo-actions: [issue]
14
- navbar:
15
- background: primary
16
- search: true
17
- sidebar:
18
- style: floating
19
-
20
- metadata-files: [nbdev.yml, sidebar.yml]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
nbs/index.ipynb DELETED
@@ -1,75 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "markdown",
5
- "metadata": {},
6
- "source": [
7
- "# nbdev-spaces-demo\n",
8
- "\n",
9
- "> A demo of how to create a Hugging Face Space with gradio within a nbdev project."
10
- ]
11
- },
12
- {
13
- "cell_type": "markdown",
14
- "metadata": {},
15
- "source": [
16
- "This is a toy python library that lets you obtain the size of any Hugging Face dataset. For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:"
17
- ]
18
- },
19
- {
20
- "cell_type": "code",
21
- "execution_count": null,
22
- "metadata": {},
23
- "outputs": [
24
- {
25
- "data": {
26
- "text/plain": [
27
- "'5.49 GB'"
28
- ]
29
- },
30
- "execution_count": null,
31
- "metadata": {},
32
- "output_type": "execute_result"
33
- }
34
- ],
35
- "source": [
36
- "from nbdev_spaces_demo import hfsize \n",
37
- "hfsize(\"tglcourse/CelebA-faces-cropped-128\")"
38
- ]
39
- },
40
- {
41
- "cell_type": "markdown",
42
- "metadata": {},
43
- "source": [
44
- "We deploy this function using Gradio and Hugging Face spaces."
45
- ]
46
- },
47
- {
48
- "cell_type": "markdown",
49
- "metadata": {},
50
- "source": [
51
- "## Using nbdev with Gradio\n",
52
- "\n",
53
- "Gradio and Hugging Face spaces is one of the easiest way to create and host apps. Gradio also allows you to prototype these apps in notebooks, which is excellent! \n",
54
- "\n",
55
- "We show you step-by-step instructions on how to deploy a Hugging Face gradio app from a notebook in this example."
56
- ]
57
- },
58
- {
59
- "cell_type": "code",
60
- "execution_count": null,
61
- "metadata": {},
62
- "outputs": [],
63
- "source": []
64
- }
65
- ],
66
- "metadata": {
67
- "kernelspec": {
68
- "display_name": "Python 3 (ipykernel)",
69
- "language": "python",
70
- "name": "python3"
71
- }
72
- },
73
- "nbformat": 4,
74
- "nbformat_minor": 4
75
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
nbs/nbdev.yml DELETED
@@ -1,9 +0,0 @@
1
- project:
2
- output-dir: _docs
3
-
4
- website:
5
- title: "nbdev-spaces-demo"
6
- site-url: "https://fastai.github.io/nbdev-spaces-demo"
7
- description: "A demo of how to create a Hugging Face Space with gradio within a nbdev project."
8
- repo-branch: master
9
- repo-url: "https://github.com/fastai/nbdev-spaces-demo"
 
 
 
 
 
 
 
 
 
 
nbs/size.ipynb DELETED
@@ -1,94 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "markdown",
5
- "metadata": {},
6
- "source": [
7
- "# size\n",
8
- "> Determine the size of a Hugging Face Dataset"
9
- ]
10
- },
11
- {
12
- "cell_type": "code",
13
- "execution_count": null,
14
- "metadata": {},
15
- "outputs": [],
16
- "source": [
17
- "#| default_exp size"
18
- ]
19
- },
20
- {
21
- "cell_type": "code",
22
- "execution_count": null,
23
- "metadata": {},
24
- "outputs": [],
25
- "source": [
26
- "#|export\n",
27
- "from fastcore.net import urljson, HTTPError\n",
28
- "\n",
29
- "def hfsize(repo:str):\n",
30
- " \"Returns the size in GB of a HuggingFace Dataset.\"\n",
31
- " url = f'https://huggingface.co/api/datasets/{repo}'\n",
32
- " try: resp = urljson(f'{url}/treesize/main')\n",
33
- " except HTTPError: return f'Did not find repo: {url}'\n",
34
- " gb = resp['size'] / 1e9\n",
35
- " return f'{gb:.2f} GB'"
36
- ]
37
- },
38
- {
39
- "cell_type": "markdown",
40
- "metadata": {},
41
- "source": [
42
- "`size` take as an input a [Hugging Face Dataset](https://huggingface.co/docs/datasets/index) repo and returns the total size in GB of the data.\n",
43
- "\n",
44
- "For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:"
45
- ]
46
- },
47
- {
48
- "cell_type": "code",
49
- "execution_count": null,
50
- "metadata": {},
51
- "outputs": [
52
- {
53
- "data": {
54
- "text/plain": [
55
- "'5.49 GB'"
56
- ]
57
- },
58
- "execution_count": null,
59
- "metadata": {},
60
- "output_type": "execute_result"
61
- }
62
- ],
63
- "source": [
64
- "hfsize(\"tglcourse/CelebA-faces-cropped-128\")"
65
- ]
66
- },
67
- {
68
- "cell_type": "code",
69
- "execution_count": null,
70
- "metadata": {},
71
- "outputs": [],
72
- "source": [
73
- "#| hide\n",
74
- "import nbdev; nbdev.nbdev_export()"
75
- ]
76
- },
77
- {
78
- "cell_type": "code",
79
- "execution_count": null,
80
- "metadata": {},
81
- "outputs": [],
82
- "source": []
83
- }
84
- ],
85
- "metadata": {
86
- "kernelspec": {
87
- "display_name": "Python 3 (ipykernel)",
88
- "language": "python",
89
- "name": "python3"
90
- }
91
- },
92
- "nbformat": 4,
93
- "nbformat_minor": 4
94
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
nbs/styles.css DELETED
@@ -1,37 +0,0 @@
1
- .cell {
2
- margin-bottom: 1rem;
3
- }
4
-
5
- .cell > .sourceCode {
6
- margin-bottom: 0;
7
- }
8
-
9
- .cell-output > pre {
10
- margin-bottom: 0;
11
- }
12
-
13
- .cell-output > pre, .cell-output > .sourceCode > pre, .cell-output-stdout > pre {
14
- margin-left: 0.8rem;
15
- margin-top: 0;
16
- background: none;
17
- border-left: 2px solid lightsalmon;
18
- border-top-left-radius: 0;
19
- border-top-right-radius: 0;
20
- }
21
-
22
- .cell-output > .sourceCode {
23
- border: none;
24
- }
25
-
26
- .cell-output > .sourceCode {
27
- background: none;
28
- margin-top: 0;
29
- }
30
-
31
- div.description {
32
- padding-left: 2px;
33
- padding-top: 5px;
34
- font-style: italic;
35
- font-size: 135%;
36
- opacity: 70%;
37
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fastcore
settings.ini DELETED
@@ -1,42 +0,0 @@
1
- [DEFAULT]
2
- # All sections below are required unless otherwise specified.
3
- # See https://github.com/fastai/nbdev/blob/master/settings.ini for examples.
4
-
5
- ### Python library ###
6
- repo = nbdev-spaces-demo
7
- lib_name = %(repo)s
8
- version = 0.0.1
9
- min_python = 3.7
10
- license = apache2
11
-
12
- ### nbdev ###
13
- doc_path = _docs
14
- lib_path = nbdev_spaces_demo
15
- nbs_path = nbs
16
- recursive = True
17
- tst_flags = notest
18
- put_version_in_init = True
19
-
20
- ### Docs ###
21
- branch = master
22
- custom_sidebar = False
23
- doc_host = https://%(user)s.github.io
24
- doc_baseurl = /%(repo)s
25
- git_url = https://github.com/%(user)s/%(repo)s
26
- title = %(lib_name)s
27
-
28
- ### PyPI ###
29
- audience = Developers
30
- author = Hamel Husain
31
- author_email = hamel.husain@gmail.com
32
- copyright = 2022 onwards, %(author)s
33
- description = A demo of how to create a Hugging Face Space with gradio within a nbdev project.
34
- keywords = nbdev jupyter notebook python
35
- language = English
36
- status = 3
37
- user = fastai
38
-
39
- ### Optional ###
40
- requirements = fastcore
41
- dev_requirements = gradio
42
- # console_scripts =
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
setup.py DELETED
@@ -1,57 +0,0 @@
1
- from pkg_resources import parse_version
2
- from configparser import ConfigParser
3
- import setuptools
4
- assert parse_version(setuptools.__version__)>=parse_version('36.2')
5
-
6
- # note: all settings are in settings.ini; edit there, not here
7
- config = ConfigParser(delimiters=['='])
8
- config.read('settings.ini')
9
- cfg = config['DEFAULT']
10
-
11
- cfg_keys = 'version description keywords author author_email'.split()
12
- expected = cfg_keys + "lib_name user branch license status min_python audience language".split()
13
- for o in expected: assert o in cfg, "missing expected setting: {}".format(o)
14
- setup_cfg = {o:cfg[o] for o in cfg_keys}
15
-
16
- licenses = {
17
- 'apache2': ('Apache Software License 2.0','OSI Approved :: Apache Software License'),
18
- 'mit': ('MIT License', 'OSI Approved :: MIT License'),
19
- 'gpl2': ('GNU General Public License v2', 'OSI Approved :: GNU General Public License v2 (GPLv2)'),
20
- 'gpl3': ('GNU General Public License v3', 'OSI Approved :: GNU General Public License v3 (GPLv3)'),
21
- 'bsd3': ('BSD License', 'OSI Approved :: BSD License'),
22
- }
23
- statuses = [ '1 - Planning', '2 - Pre-Alpha', '3 - Alpha',
24
- '4 - Beta', '5 - Production/Stable', '6 - Mature', '7 - Inactive' ]
25
- py_versions = '3.6 3.7 3.8 3.9 3.10'.split()
26
-
27
- requirements = cfg.get('requirements','').split()
28
- if cfg.get('pip_requirements'): requirements += cfg.get('pip_requirements','').split()
29
- min_python = cfg['min_python']
30
- lic = licenses.get(cfg['license'].lower(), (cfg['license'], None))
31
- dev_requirements = (cfg.get('dev_requirements') or '').split()
32
-
33
- setuptools.setup(
34
- name = cfg['lib_name'],
35
- license = lic[0],
36
- classifiers = [
37
- 'Development Status :: ' + statuses[int(cfg['status'])],
38
- 'Intended Audience :: ' + cfg['audience'].title(),
39
- 'Natural Language :: ' + cfg['language'].title(),
40
- ] + ['Programming Language :: Python :: '+o for o in py_versions[py_versions.index(min_python):]] + (['License :: ' + lic[1] ] if lic[1] else []),
41
- url = cfg['git_url'],
42
- packages = setuptools.find_packages(),
43
- include_package_data = True,
44
- install_requires = requirements,
45
- extras_require={ 'dev': dev_requirements },
46
- dependency_links = cfg.get('dep_links','').split(),
47
- python_requires = '>=' + cfg['min_python'],
48
- long_description = open('README.md').read(),
49
- long_description_content_type = 'text/markdown',
50
- zip_safe = False,
51
- entry_points = {
52
- 'console_scripts': cfg.get('console_scripts','').split(),
53
- 'nbdev': [f'{cfg.get("lib_path")}={cfg.get("lib_path")}._modidx:d']
54
- },
55
- **setup_cfg)
56
-
57
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
yaml.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Foo
3
+ emoji: 🐢
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.9
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ <!-- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference -->