-
-
Save hamelsmu/35be07d242f3f19063c3a3839127dc67 to your computer and use it in GitHub Desktop.
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "eed6bcd0-6cf8-4aa9-bedf-5eca5a6b7a9c", | |
"metadata": {}, | |
"source": [ | |
"# Hugging Face Spaces From A Notebook" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "15fe30a9-cfeb-4ec0-ae40-334072046464", | |
"metadata": {}, | |
"source": [ | |
"Please reference [this blog post](https://nbdev.fast.ai/blog/posts/2022-11-07-spaces) on how to use this notebook." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "acf7644f-42b3-4d8d-a1d0-f8c3a2358e27", | |
"metadata": {}, | |
"source": [ | |
"## Install dependencies" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "a3a57baf-bb8a-4b32-904a-820fb85be1a7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"!pip install git+https://github.com/fastai/nbdev.git gradio fastcore" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5ca22d1e-1bd0-49c0-9b89-c480ad1a29c4", | |
"metadata": {}, | |
"source": [ | |
"## Make an app with Gradio" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "00cf4fad-a920-41dc-be42-3992c7fcefac", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#|export\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": "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": "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": "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": "88424f53-cd78-41fe-9e06-8a6209001064", | |
"metadata": {}, | |
"source": [ | |
"## Create a `requirements.txt` file" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "db4a30aa-9090-460e-acf9-4eb359161125", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%%writefile requirements.txt\n", | |
"fastcore" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "249b2cd7-3123-45bf-945f-882b8a964cf5", | |
"metadata": {}, | |
"source": [ | |
"## Convert this notebook into a Gradio app" | |
] | |
}, | |
{ | |
"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>" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Tried also installing nbdev with conda or pip, since this PR#1024 seems to be included in release 2.3.9 but still no success. These were in place of first input of the notebook, which installs nbdev 2.3.10. But none of them worked. Also inserted debug=Tru
e before name
but still silent. What else could I try? Thanks.
Don't use the CLI, use the method I suggested in the blog post π , I'm not certain that the CLI nbdev_export
supports this.
Tried also installing nbdev with conda or pip, since this AnswerDotAI/nbdev#1204 seems to be included in release 2.3.9 but still no success. These were in place of first input of the notebook, which installs nbdev 2.3.10. But none of them worked. Also inserted debug=True before name but still silent. What else could I try? Thanks.
Per the blog post, install from github pip install git+https://github.com/fastai/nbdev.git
Thanks, CLI was a fallback, the method in your blog post runs in 0.3 secs, but no app.py in the folder (or nowhere else). Relative or absolute paths like lib_path='/Users/cer/vc/projects/hfds-size/'
did not matter. Can't really figure out why it fails. In any case, thanks for the idea and blog post.
from nbdev.export import nb_export
nb_export('app.ipynb', lib_path='.', name='app')
the method in your blog post runs in 0.3 secs, but no app.py in the folder
It's hard to debug this way, if you would like to provide a minimally reproducible example it might be easier. The code in a repo format is on HFhub incase that helps. https://huggingface.co/spaces/hamel/hfspace_demo/tree/main
The last step "Convert this notebook into a Gradio app" silently fails, no app.py is created. Tried on VSCode, jupyter, juypter lab, colab and kaggle. When I try the CLI with
nbdev_export
I get the following error. Is there another way to auto-generate the app.py without manual editing? Thanks.