aliabd HF Staff commited on
Commit
d7b0b5f
·
verified ·
1 Parent(s): 9e6d16c

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. requirements.txt +2 -2
  3. run.ipynb +1 -1
  4. run.py +5 -6
README.md CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
- sdk_version: 4.36.1
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
 
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 4.37.0
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- gradio-client @ git+https://github.com/gradio-app/gradio@affce4cbd9e2df14175c79da27408ccce57a74e9#subdirectory=client/python
2
- https://gradio-builds.s3.amazonaws.com/affce4cbd9e2df14175c79da27408ccce57a74e9/gradio-4.36.1-py3-none-any.whl
 
1
+ gradio-client @ git+https://github.com/gradio-app/gradio@4cfb6eebf6697d0cfc7a68b734e58d3b53aa6ec7#subdirectory=client/python
2
+ https://gradio-builds.s3.amazonaws.com/4cfb6eebf6697d0cfc7a68b734e58d3b53aa6ec7/gradio-4.37.0-py3-none-any.whl
run.ipynb CHANGED
@@ -1 +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}
 
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.session_hash\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(delete_cache=(60, 3600)) 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()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py CHANGED
@@ -14,8 +14,8 @@ def generate_random_img(history: list[Image.Image], request: gr.Request):
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
 
@@ -30,7 +30,7 @@ def delete_directory(req: gr.Request):
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
  """)
@@ -44,10 +44,9 @@ with gr.Blocks() as demo:
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")
 
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.session_hash
19
  user_dir.mkdir(exist_ok=True)
20
  path = user_dir / f"{secrets.token_urlsafe(8)}.webp"
21
 
 
30
  user_dir: Path = current_dir / req.username
31
  shutil.rmtree(str(user_dir))
32
 
33
+ with gr.Blocks(delete_cache=(60, 3600)) 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
  """)
 
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()