aliabd HF Staff commited on
Commit
2df811f
·
verified ·
1 Parent(s): 8ad0eb0

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +7 -7
  2. requirements.txt +2 -0
  3. run.ipynb +1 -0
  4. run.py +33 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Unload Event Test Main
3
- emoji: 👁
4
- colorFrom: green
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 4.24.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: unload_event_test_main
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
  sdk_version: 4.24.0
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio-client @ git+https://github.com/gradio-app/gradio@6a4bf7abe29059dbdc6a342e0366fdaa2e4120ee#subdirectory=client/python
2
+ https://gradio-builds.s3.amazonaws.com/6a4bf7abe29059dbdc6a342e0366fdaa2e4120ee/gradio-4.24.0-py3-none-any.whl
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: unload_event_test"]}, {"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": ["\"\"\"This demo is only meant to test the unload event.\n", "It will write to a file when the unload event is triggered.\n", "May not work as expected if multiple people are using it.\n", "\"\"\"\n", "import gradio as gr\n", "from pathlib import Path\n", "\n", "log_file = (Path(__file__).parent / \"output_log.txt\").resolve()\n", "\n", "\n", "def test_fn(x):\n", " with open(log_file, \"a\") as f:\n", " f.write(f\"incremented {x}\\n\")\n", " return x + 1, x + 1\n", "\n", "def delete_fn(v):\n", " with log_file.open(\"a\") as f:\n", " f.write(f\"deleted {v}\\n\")\n", "\n", "def unload_fn():\n", " with log_file.open(\"a\") as f:\n", " f.write(f\"unloading\\n\")\n", "\n", "with gr.Blocks() as demo:\n", " n1 = gr.Number(value=0, label=\"Number\")\n", " state = gr.State(value=0, delete_callback=delete_fn)\n", " button = gr.Button(\"Increment\")\n", " button.click(test_fn, [state], [n1, state], api_name=\"increment\")\n", " demo.unload(unload_fn)\n", " demo.load(lambda: log_file.write_text(\"\"))\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """This demo is only meant to test the unload event.
2
+ It will write to a file when the unload event is triggered.
3
+ May not work as expected if multiple people are using it.
4
+ """
5
+ import gradio as gr
6
+ from pathlib import Path
7
+
8
+ log_file = (Path(__file__).parent / "output_log.txt").resolve()
9
+
10
+
11
+ def test_fn(x):
12
+ with open(log_file, "a") as f:
13
+ f.write(f"incremented {x}\n")
14
+ return x + 1, x + 1
15
+
16
+ def delete_fn(v):
17
+ with log_file.open("a") as f:
18
+ f.write(f"deleted {v}\n")
19
+
20
+ def unload_fn():
21
+ with log_file.open("a") as f:
22
+ f.write(f"unloading\n")
23
+
24
+ with gr.Blocks() as demo:
25
+ n1 = gr.Number(value=0, label="Number")
26
+ state = gr.State(value=0, delete_callback=delete_fn)
27
+ button = gr.Button("Increment")
28
+ button.click(test_fn, [state], [n1, state], api_name="increment")
29
+ demo.unload(unload_fn)
30
+ demo.load(lambda: log_file.write_text(""))
31
+
32
+ if __name__ == "__main__":
33
+ demo.launch()