Skip to content

Instantly share code, notes, and snippets.

@hamelsmu
Created November 7, 2022 20:20
Show Gist options
  • Select an option

  • Save hamelsmu/9207ab64da14254e0327709fa93abbdc to your computer and use it in GitHub Desktop.

Select an option

Save hamelsmu/9207ab64da14254e0327709fa93abbdc to your computer and use it in GitHub Desktop.
A demo of creating a gradio app with nbdev
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e5e5d597-19ad-46e5-81ad-8f646d8a1c21",
"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": "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`. As a reminder, this 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>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment