Spaces:
Runtime error
Runtime error
File size: 4,457 Bytes
0d8932f |
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 |
{
"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-10-spaces) on how to use this notebook."
]
},
{
"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": "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
}
|