{ "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": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(, '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": [ "
\n", "\n", "
\n", "\n", "\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }