Skip to content

Commit 33cd86b

Browse files
authored
Merge pull request #1216 from fastai/space-blog
fix blog nits
2 parents 512f00c + 3438df6 commit 33cd86b

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

nbs/blog/posts/2022-11-07-spaces/index.qmd

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ skip_showdoc: true
1212
![Image created with Stable Diffusion from [this space](https://huggingface.co/spaces/stabilityai/stable-diffusion)](blog_cover.jpeg)
1313

1414

15-
[Hugging Face Spaces](https://huggingface.co/spaces/launch) provides an easy ways to deploy a web app with python. [Gradio](https://gradio.app/) is one of my favorite tools for building these web apps. For example, the cover-image for this blog post was generated with a [Gradio App](https://huggingface.co/spaces/stabilityai/stable-diffusion)![^1] Gradio also allows you to prototype your web apps in notebooks, which allow you to iterate fast. Unfortunately, Hugging Face Spaces requires you to package your web application code as a python script named `app.py`.
15+
[Hugging Face Spaces](https://huggingface.co/spaces/launch) provides an easy ways to deploy a web app with python. [Gradio](https://gradio.app/) is one of my favorite tools for building these web apps. For example, the cover-image for this blog post was generated with a [Gradio App](https://huggingface.co/spaces/stabilityai/stable-diffusion)![^1] Gradio also allows you to prototype your web apps in notebooks which allows you to iterate fast. Unfortunately, Hugging Face Spaces requires you to package your web application code as a python script named `app.py`.
1616

1717
However, **thanks to [nbdev](https://nbdev.fast.ai), you can deploy a Gradio app to Spaces from a notebook without having to refactor your code into a script!** When you finish this tutorial, you will have an app that looks like this:
1818

@@ -41,7 +41,21 @@ After you are done creating the space, clone the repo locally. In this example,
4141

4242
## 2. Create A Notebook
4343

44-
To follow along, create a notebook called `app.ipynb` in the root of your newly cloned repo. Alternatively, a minimal version of this notebook can also be [found here](https://gist.github.com/hamelsmu/35be07d242f3f19063c3a3839127dc67).
44+
Before getting started you will want to install the dependencies for this tutorial:
45+
46+
```python
47+
!pip install git+https://github.com/fastai/nbdev.git gradio fastcore
48+
```
49+
50+
Create a notebook called `app.ipynb` in the root of your newly cloned repo. Alternatively, download [the notebook](https://gist.github.com/hamelsmu/35be07d242f3f19063c3a3839127dc67) and follow along.
51+
52+
:::{.callout-tip}
53+
54+
### Download the notebook and follow along
55+
56+
This blog post is a verbose version of the "notebook" you can use to create a Gradio app. However, it can be useful to see just the code without any of the prose. A [concise version of this notebook is here](https://gist.github.com/hamelsmu/35be07d242f3f19063c3a3839127dc67). I recommend taking a look at this notebook during or after you read this blog post.
57+
58+
:::
4559

4660
## 3. Make an app with Gradio
4761

@@ -78,12 +92,11 @@ For example, I can check the size of [tglcourse/CelebA-faces-cropped-128](https:
7892
```python
7993
size("tglcourse/CelebA-faces-cropped-128")
8094
```
81-
82-
83-
95+
:::{.cell-output}
8496

8597
'5.49 GB'
8698

99+
:::
87100

88101

89102
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:
@@ -95,7 +108,7 @@ iface = gr.Interface(fn=size, inputs=gr.Text(value="tglcourse/CelebA-faces-cropp
95108
iface.launch(height=450, width=500)
96109
```
97110

98-
111+
:::{.cell-output}
99112
Running on local URL: http://127.0.0.1:7861
100113

101114
To create a public link, set `share=True` in `launch()`.
@@ -106,7 +119,8 @@ iface.launch(height=450, width=500)
106119
```
107120

108121
![](gradio_local.png)
109-
122+
123+
:::
110124

111125
Note how running the `launch()` method in a notebook runs a webserver in the background. Below, I call the `close()` method to close the webserver.
112126

@@ -116,8 +130,11 @@ Note how running the `launch()` method in a notebook runs a webserver in the bac
116130
iface.close()
117131
```
118132

133+
:::{.cell-output}
134+
119135
Closing server running on port: 7861
120136

137+
:::
121138

122139
## 4. Convert This Notebook Into A Gradio App
123140

@@ -189,7 +206,7 @@ _Note: you may want to add operating system dependencies in addition to python d
189206

190207
To launch your gradio app, you need to commit the changes to the Hugging Face repo:
191208

192-
```
209+
```bash
193210
git add -A; git commit -m "Add application files"; git push
194211
```
195212

0 commit comments

Comments
 (0)