hamel commited on
Commit
0d8932f
1 Parent(s): 9703f29
Files changed (3) hide show
  1. app.ipynb +10 -5
  2. app.py +2 -2
  3. demo.ipynb +184 -0
app.ipynb CHANGED
@@ -40,7 +40,12 @@
40
  "id": "c25e8e7a-52d9-4305-a107-ba03e3d6a5f3",
41
  "metadata": {},
42
  "source": [
43
- "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`."
 
 
 
 
 
44
  ]
45
  },
46
  {
@@ -48,7 +53,7 @@
48
  "id": "ff26114c-329b-4a97-98b5-c652554b0114",
49
  "metadata": {},
50
  "source": [
51
- "## 2. Make an app with Gradio"
52
  ]
53
  },
54
  {
@@ -213,7 +218,7 @@
213
  "id": "249b2cd7-3123-45bf-945f-882b8a964cf5",
214
  "metadata": {},
215
  "source": [
216
- "## 3. Converting This Notebook Into A Gradio App"
217
  ]
218
  },
219
  {
@@ -341,7 +346,7 @@
341
  "source": [
342
  "_Note: you may want to add operating system dependencies in addition to python dependencies. You can do this via a `packages.txt` file as [documented here](https://huggingface.co/docs/hub/spaces-dependencies#adding-your-own-dependencies)._\n",
343
  "\n",
344
- "## 4. Launch Your Gradio App\n",
345
  "\n",
346
  "To launch your gradio app, you need to commit the changes to the Hugging Face repo:\n",
347
  "\n",
@@ -355,7 +360,7 @@
355
  "id": "fa661f93-73b4-465a-9c22-cc38197505cb",
356
  "metadata": {},
357
  "source": [
358
- "## 5. Voilà! Enjoy your Gradio App"
359
  ]
360
  },
361
  {
40
  "id": "c25e8e7a-52d9-4305-a107-ba03e3d6a5f3",
41
  "metadata": {},
42
  "source": [
43
+ "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`.\n",
44
+ "\n",
45
+ "\n",
46
+ "## 2. Create A Notebook\n",
47
+ "\n",
48
+ "To follow along, create a notebook called `app.ipynb` in the root of your newly cloned repo. You can write code as described below in your notebook. Alternatively, a minimal version of this notebook can also be [found here](https://gist.github.com/hamelsmu/35be07d242f3f19063c3a3839127dc67)."
49
  ]
50
  },
51
  {
53
  "id": "ff26114c-329b-4a97-98b5-c652554b0114",
54
  "metadata": {},
55
  "source": [
56
+ "## 3. Make an app with Gradio"
57
  ]
58
  },
59
  {
218
  "id": "249b2cd7-3123-45bf-945f-882b8a964cf5",
219
  "metadata": {},
220
  "source": [
221
+ "## 4. Convert This Notebook Into A Gradio App"
222
  ]
223
  },
224
  {
346
  "source": [
347
  "_Note: you may want to add operating system dependencies in addition to python dependencies. You can do this via a `packages.txt` file as [documented here](https://huggingface.co/docs/hub/spaces-dependencies#adding-your-own-dependencies)._\n",
348
  "\n",
349
+ "## 5. Launch Your Gradio App\n",
350
  "\n",
351
  "To launch your gradio app, you need to commit the changes to the Hugging Face repo:\n",
352
  "\n",
360
  "id": "fa661f93-73b4-465a-9c22-cc38197505cb",
361
  "metadata": {},
362
  "source": [
363
+ "# Voilà! Enjoy your Gradio App"
364
  ]
365
  },
366
  {
app.py CHANGED
@@ -7,7 +7,7 @@ __all__ = ['iface', 'size']
7
  import gradio as gr
8
  from fastcore.net import urljson, HTTPError
9
 
10
- # %% app.ipynb 7
11
  def size(repo:str):
12
  "Returns the size in GB of a HuggingFace Dataset."
13
  url = f'https://huggingface.co/api/datasets/{repo}'
@@ -16,6 +16,6 @@ def size(repo:str):
16
  gb = resp['size'] / 1e9
17
  return f'{gb:.2f} GB'
18
 
19
- # %% app.ipynb 11
20
  iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
21
  iface.launch(width=500)
7
  import gradio as gr
8
  from fastcore.net import urljson, HTTPError
9
 
10
+ # %% app.ipynb 8
11
  def size(repo:str):
12
  "Returns the size in GB of a HuggingFace Dataset."
13
  url = f'https://huggingface.co/api/datasets/{repo}'
16
  gb = resp['size'] / 1e9
17
  return f'{gb:.2f} GB'
18
 
19
+ # %% app.ipynb 12
20
  iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropped-128"), outputs="text")
21
  iface.launch(width=500)
demo.ipynb ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "eed6bcd0-6cf8-4aa9-bedf-5eca5a6b7a9c",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Hugging Face Spaces From A Notebook"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "id": "15fe30a9-cfeb-4ec0-ae40-334072046464",
14
+ "metadata": {},
15
+ "source": [
16
+ "Please reference [this blog post](https://nbdev.fast.ai/blog/posts/2022-11-10-spaces) on how to use this notebook."
17
+ ]
18
+ },
19
+ {
20
+ "cell_type": "markdown",
21
+ "id": "5ca22d1e-1bd0-49c0-9b89-c480ad1a29c4",
22
+ "metadata": {},
23
+ "source": [
24
+ "# Make an app with Gradio"
25
+ ]
26
+ },
27
+ {
28
+ "cell_type": "code",
29
+ "execution_count": null,
30
+ "id": "00cf4fad-a920-41dc-be42-3992c7fcefac",
31
+ "metadata": {},
32
+ "outputs": [],
33
+ "source": [
34
+ "#|export\n",
35
+ "import gradio as gr\n",
36
+ "from fastcore.net import urljson, HTTPError"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "code",
41
+ "execution_count": null,
42
+ "id": "38a4389f-ef53-4626-a6f5-a859354f854b",
43
+ "metadata": {},
44
+ "outputs": [],
45
+ "source": [
46
+ "#|export\n",
47
+ "def size(repo:str):\n",
48
+ " \"Returns the size in GB of a HuggingFace Dataset.\"\n",
49
+ " url = f'https://huggingface.co/api/datasets/{repo}'\n",
50
+ " try: resp = urljson(f'{url}/treesize/main')\n",
51
+ " except HTTPError: return f'Did not find repo: {url}'\n",
52
+ " gb = resp['size'] / 1e9\n",
53
+ " return f'{gb:.2f} GB'"
54
+ ]
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": null,
59
+ "id": "95bc32b8-d8ff-4761-a2d7-0880c51d0a42",
60
+ "metadata": {},
61
+ "outputs": [
62
+ {
63
+ "data": {
64
+ "text/plain": [
65
+ "'5.49 GB'"
66
+ ]
67
+ },
68
+ "execution_count": null,
69
+ "metadata": {},
70
+ "output_type": "execute_result"
71
+ }
72
+ ],
73
+ "source": [
74
+ "size(\"tglcourse/CelebA-faces-cropped-128\")"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "7b20e2a1-b622-4970-9069-0202ce10a2ce",
81
+ "metadata": {},
82
+ "outputs": [
83
+ {
84
+ "name": "stdout",
85
+ "output_type": "stream",
86
+ "text": [
87
+ "Running on local URL: http://127.0.0.1:7860\n",
88
+ "\n",
89
+ "To create a public link, set `share=True` in `launch()`.\n"
90
+ ]
91
+ },
92
+ {
93
+ "data": {
94
+ "text/html": [
95
+ "<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>"
96
+ ],
97
+ "text/plain": [
98
+ "<IPython.core.display.HTML object>"
99
+ ]
100
+ },
101
+ "metadata": {},
102
+ "output_type": "display_data"
103
+ },
104
+ {
105
+ "data": {
106
+ "text/plain": [
107
+ "(<gradio.routes.App>, 'http://127.0.0.1:7860/', None)"
108
+ ]
109
+ },
110
+ "execution_count": null,
111
+ "metadata": {},
112
+ "output_type": "execute_result"
113
+ }
114
+ ],
115
+ "source": [
116
+ "#|export\n",
117
+ "iface = gr.Interface(fn=size, inputs=gr.Text(value=\"tglcourse/CelebA-faces-cropped-128\"), outputs=\"text\")\n",
118
+ "iface.launch(width=500)"
119
+ ]
120
+ },
121
+ {
122
+ "cell_type": "code",
123
+ "execution_count": null,
124
+ "id": "39d7be72-9389-42cf-91b1-78e8f4bbd083",
125
+ "metadata": {},
126
+ "outputs": [
127
+ {
128
+ "name": "stdout",
129
+ "output_type": "stream",
130
+ "text": [
131
+ "Closing server running on port: 7860\n"
132
+ ]
133
+ }
134
+ ],
135
+ "source": [
136
+ "# this is only necessary in a notebook\n",
137
+ "iface.close()"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "markdown",
142
+ "id": "249b2cd7-3123-45bf-945f-882b8a964cf5",
143
+ "metadata": {},
144
+ "source": [
145
+ "## Convert This Notebook Into A Gradio App"
146
+ ]
147
+ },
148
+ {
149
+ "cell_type": "code",
150
+ "execution_count": null,
151
+ "id": "6706d92c-5785-4f09-9773-b9a944c493a5",
152
+ "metadata": {},
153
+ "outputs": [],
154
+ "source": [
155
+ "from nbdev.export import nb_export\n",
156
+ "nb_export('app.ipynb', lib_path='.', name='app')"
157
+ ]
158
+ },
159
+ {
160
+ "cell_type": "markdown",
161
+ "id": "0182403f-d1d6-48c0-8e66-46aefb23a9ab",
162
+ "metadata": {},
163
+ "source": [
164
+ "<div>\n",
165
+ "<link rel=\"stylesheet\" href=\"https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.css\">\n",
166
+ "<div id=\"target\"></div>\n",
167
+ "<script src=\"https://gradio.s3-us-west-2.amazonaws.com/2.6.5/static/bundle.js\"></script>\n",
168
+ "<script>\n",
169
+ "launchGradioFromSpaces(\"abidlabs/question-answering\", \"#target\")\n",
170
+ "</script>\n",
171
+ "</div>"
172
+ ]
173
+ }
174
+ ],
175
+ "metadata": {
176
+ "kernelspec": {
177
+ "display_name": "Python 3 (ipykernel)",
178
+ "language": "python",
179
+ "name": "python3"
180
+ }
181
+ },
182
+ "nbformat": 4,
183
+ "nbformat_minor": 5
184
+ }