Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: indigo
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
|
| 2 |
---
|
| 3 |
+
title: state_cleanup
|
| 4 |
+
emoji: 🔥
|
| 5 |
colorFrom: indigo
|
| 6 |
+
colorTo: indigo
|
| 7 |
sdk: gradio
|
| 8 |
+
sdk_version: 4.25.0
|
| 9 |
+
app_file: run.py
|
| 10 |
pinned: false
|
| 11 |
+
hf_oauth: true
|
| 12 |
---
|
|
|
|
|
|
run.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: state_cleanup"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["from __future__ import annotations\n", "import gradio as gr\n", "import numpy as np\n", "from PIL import Image\n", "from pathlib import Path\n", "import secrets\n", "import shutil\n", "\n", "current_dir = Path(__file__).parent\n", "\n", "\n", "def generate_random_img(history: list[Image.Image], request: gr.Request):\n", " \"\"\"Generate a random red, green, blue, orange, yellor or purple image.\"\"\"\n", " colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 165, 0), (255, 255, 0), (128, 0, 128)]\n", " color = colors[np.random.randint(0, len(colors))]\n", " img = Image.new('RGB', (100, 100), color)\n", " \n", " user_dir: Path = current_dir / request.username # type: ignore\n", " user_dir.mkdir(exist_ok=True)\n", " path = user_dir / f\"{secrets.token_urlsafe(8)}.webp\"\n", "\n", " img.save(path)\n", " history.append(img)\n", "\n", " return img, history, history\n", "\n", "def delete_directory(req: gr.Request):\n", " if not req.username:\n", " return\n", " user_dir: Path = current_dir / req.username\n", " shutil.rmtree(str(user_dir))\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"\"\"# State Cleanup Demo\n", " \ud83d\uddbc\ufe0f Images are saved in a user-specific directory and deleted when the users closes the page via demo.unload.\n", " \"\"\")\n", " with gr.Row():\n", " with gr.Column(scale=1):\n", " with gr.Row():\n", " img = gr.Image(label=\"Generated Image\", height=300, width=300)\n", " with gr.Row():\n", " gen = gr.Button(value=\"Generate\")\n", " with gr.Row():\n", " history = gr.Gallery(label=\"Previous Generations\", height=500, columns=10)\n", " state = gr.State(value=[], delete_callback=lambda v: print(\"STATE DELETED\"))\n", "\n", " demo.load(generate_random_img, [state], [img, state, history]) \n", " gen.click(generate_random_img, [state], [img, state, history])\n", " demo.unload(delete_directory)\n", "\n", "\n", "demo.launch(auth=lambda user,pwd: True,\n", " auth_message=\"Enter any username and password to continue\")"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import secrets
|
| 7 |
+
import shutil
|
| 8 |
+
|
| 9 |
+
current_dir = Path(__file__).parent
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def generate_random_img(history: list[Image.Image], request: gr.Request):
|
| 13 |
+
"""Generate a random red, green, blue, orange, yellor or purple image."""
|
| 14 |
+
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 165, 0), (255, 255, 0), (128, 0, 128)]
|
| 15 |
+
color = colors[np.random.randint(0, len(colors))]
|
| 16 |
+
img = Image.new('RGB', (100, 100), color)
|
| 17 |
+
|
| 18 |
+
user_dir: Path = current_dir / request.username # type: ignore
|
| 19 |
+
user_dir.mkdir(exist_ok=True)
|
| 20 |
+
path = user_dir / f"{secrets.token_urlsafe(8)}.webp"
|
| 21 |
+
|
| 22 |
+
img.save(path)
|
| 23 |
+
history.append(img)
|
| 24 |
+
|
| 25 |
+
return img, history, history
|
| 26 |
+
|
| 27 |
+
def delete_directory(req: gr.Request):
|
| 28 |
+
if not req.username:
|
| 29 |
+
return
|
| 30 |
+
user_dir: Path = current_dir / req.username
|
| 31 |
+
shutil.rmtree(str(user_dir))
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("""# State Cleanup Demo
|
| 35 |
+
🖼️ Images are saved in a user-specific directory and deleted when the users closes the page via demo.unload.
|
| 36 |
+
""")
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column(scale=1):
|
| 39 |
+
with gr.Row():
|
| 40 |
+
img = gr.Image(label="Generated Image", height=300, width=300)
|
| 41 |
+
with gr.Row():
|
| 42 |
+
gen = gr.Button(value="Generate")
|
| 43 |
+
with gr.Row():
|
| 44 |
+
history = gr.Gallery(label="Previous Generations", height=500, columns=10)
|
| 45 |
+
state = gr.State(value=[], delete_callback=lambda v: print("STATE DELETED"))
|
| 46 |
+
|
| 47 |
+
demo.load(generate_random_img, [state], [img, state, history])
|
| 48 |
+
gen.click(generate_random_img, [state], [img, state, history])
|
| 49 |
+
demo.unload(delete_directory)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
demo.launch(auth=lambda user,pwd: True,
|
| 53 |
+
auth_message="Enter any username and password to continue")
|