File size: 13,009 Bytes
fecd672
 
 
 
 
 
 
484e78f
fecd672
484e78f
fecd672
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
484e78f
fecd672
484e78f
fecd672
 
 
 
 
 
 
 
 
484e78f
fecd672
484e78f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fecd672
 
 
 
 
 
 
484e78f
 
 
fecd672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
484e78f
fecd672
484e78f
 
 
 
 
 
 
 
fecd672
484e78f
 
fecd672
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
484e78f
fecd672
 
484e78f
 
 
fecd672
 
 
 
 
484e78f
fecd672
 
 
 
 
 
484e78f
fecd672
 
 
 
484e78f
fecd672
 
 
 
 
 
 
484e78f
fecd672
484e78f
fecd672
 
 
 
 
 
 
 
 
 
 
484e78f
fecd672
 
 
 
 
 
 
 
 
484e78f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fecd672
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c68f03e-620c-46a9-a7ec-a6cde27043cd",
   "metadata": {},
   "source": [
    "# Hugging Face Spaces From A Notebook\n",
    "\n",
    "> A demo of using nbdev with Hugging Face Spaces"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96483373-4ae1-49b2-85ed-ceee8456df19",
   "metadata": {},
   "source": [
    "## 1. Create a Gradio-enabled Space on Hugging Face\n",
    "\n",
    "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):"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b34d7ec6-69b8-48c4-a68b-fad6db3c2fab",
   "metadata": {},
   "source": [
    "![](./create_space.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c25e8e7a-52d9-4305-a107-ba03e3d6a5f3",
   "metadata": {},
   "source": [
    "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`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff26114c-329b-4a97-98b5-c652554b0114",
   "metadata": {},
   "source": [
    "## 2. Make an app with Gradio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14a884fc-36e2-43ec-8e42-ca2903aaa4de",
   "metadata": {},
   "source": [
    "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",
    "\n",
    "First, lets specify the libraries we need, which in this case are `gradio` and `fastcore`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5e5d597-19ad-46e5-81ad-8f646d8a1c21",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export app\n",
    "import gradio as gr\n",
    "from fastcore.net import urljson, HTTPError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38a4389f-ef53-4626-a6f5-a859354f854b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export\n",
    "def size(repo:str):\n",
    "    \"Returns the size in GB of a HuggingFace Dataset.\"\n",
    "    url = f'https://huggingface.co/api/datasets/{repo}'\n",
    "    try: resp = urljson(f'{url}/treesize/main')\n",
    "    except HTTPError: return f'Did not find repo: {url}'\n",
    "    gb = resp['size'] / 1e9\n",
    "    return f'{gb:.2f} GB'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ff9f84d-7744-46ad-80ed-2cf1fa6d0643",
   "metadata": {},
   "source": [
    "`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",
    "\n",
    "For example, we can check the size of [tglcourse/CelebA-faces-cropped-128](https://huggingface.co/datasets/tglcourse/CelebA-faces-cropped-128) like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95bc32b8-d8ff-4761-a2d7-0880c51d0a42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5.49 GB'"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "size(\"tglcourse/CelebA-faces-cropped-128\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb13747b-ea48-4146-846d-deb9e855d32d",
   "metadata": {},
   "source": [
    "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b20e2a1-b622-4970-9069-0202ce10a2ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<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>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(<gradio.routes.App>, 'http://127.0.0.1:7860/', None)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#|export\n",
    "iface = gr.Interface(fn=size, inputs=gr.Text(value=\"tglcourse/CelebA-faces-cropped-128\"), outputs=\"text\")\n",
    "iface.launch(width=500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59926b18-a9af-4387-9fcc-f88e588da577",
   "metadata": {},
   "source": [
    "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39d7be72-9389-42cf-91b1-78e8f4bbd083",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Closing server running on port: 7860\n"
     ]
    }
   ],
   "source": [
    "# this is only necessary in a notebook\n",
    "iface.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "249b2cd7-3123-45bf-945f-882b8a964cf5",
   "metadata": {},
   "source": [
    "## 3. Converting This Notebook Into A Gradio App"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c18ca6e-8de8-49e1-b95a-304070bbc171",
   "metadata": {},
   "source": [
    "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:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6706d92c-5785-4f09-9773-b9a944c493a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from nbdev.export import nb_export\n",
    "nb_export('app.ipynb', lib_path='.', name='app')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0182403f-d1d6-48c0-8e66-46aefb23a9ab",
   "metadata": {},
   "source": [
    "<div>\n",
    "<link rel=\"stylesheet\" href=\"https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.css\">\n",
    "<div id=\"target\"></div>\n",
    "<script src=\"https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.js\"></script>\n",
    "<script>\n",
    "launchGradioFromSpaces(\"abidlabs/question-answering\", \"#target\")\n",
    "</script>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84d5fd19-7880-459c-8382-b3574ed11141",
   "metadata": {},
   "source": [
    "### Understanding what is generated"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ea562e7-b67a-45df-b822-2f4528a307c2",
   "metadata": {},
   "source": [
    "Notice how the contents of app.py only contains the exported cells from this notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bae6a5c-58bc-4a0f-9aac-34c092150fdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31m# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;31m# %% auto 0\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\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",
       "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;31m# %% app.ipynb 7\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\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",
       "\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",
       "\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",
       "\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",
       "\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",
       "\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",
       "\u001b[0;34m\u001b[0m    \u001b[0;32mreturn\u001b[0m \u001b[0;34mf'{gb:.2f} GB'\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;31m# %% app.ipynb 11\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\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",
       "\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"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%pycat app.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a081bb0f-5cad-4b99-962b-4dd49cee61a2",
   "metadata": {},
   "source": [
    "### Fill out `requirements.txt`\n",
    "\n",
    "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:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b611d9c-d262-4124-9e9e-4fe754ac4378",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fastcore"
     ]
    }
   ],
   "source": [
    "!cat requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f15d9c78-1f55-449e-8058-9af1832367a0",
   "metadata": {},
   "source": [
    "## 4. Launch Your Gradio App\n",
    "\n",
    "To launch your gradio app, you need to commit the changes in the Hugging Face repo:\n",
    "\n",
    "```\n",
    "git add -A; git commit -m \"Add application files\"; git push\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa661f93-73b4-465a-9c22-cc38197505cb",
   "metadata": {},
   "source": [
    "## 5. Voilà!  Enjoy your Gradio App"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b20ff94-6842-4078-9ec1-be740944e721",
   "metadata": {},
   "source": [
    "After a couple of minutes, you will see your app published!  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a4f7c06-406a-4a7d-be6b-6cb606c35d8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[NbConvertApp] Converting notebook app.ipynb to markdown\n",
      "[NbConvertApp] Writing 6113 bytes to app.md\n"
     ]
    }
   ],
   "source": [
    "# this is only for hamel, you can ignore this.\n",
    "!jupyter nbconvert --to markdown app.ipynb \n",
    "!cat yaml.md app.md > README.md "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "958174fe-537e-4635-90b8-22ac11eae396",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}