vorstcavry commited on
Commit
8f4d36c
·
verified ·
1 Parent(s): 38da9ca

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pathlib import Path
3
+ import os
4
+ import shutil
5
+ from huggingface_hub import HfApi, snapshot_download
6
+
7
+
8
+ HF_TOKEN = os.environ["HF_TOKEN"]
9
+
10
+ if not HF_TOKEN:
11
+ raise ValueError("HF_TOKEN not set")
12
+
13
+ api = HfApi()
14
+ username = api.whoami(token=HF_TOKEN)["name"]
15
+ repo = "sd_out"
16
+ user_repo = f"{username}/{repo}"
17
+
18
+
19
+ def refresh_images():
20
+ """
21
+ Refreshes the images by deleting the existing image directory and retrieving new images.
22
+
23
+ Returns:
24
+ A list of image files.
25
+ """
26
+ try:
27
+ shutil.rmtree(os.environ["IMAGE_DIR"])
28
+ except:
29
+ pass
30
+
31
+ image_dir = Path(
32
+ snapshot_download(repo_id=user_repo, repo_type="dataset", token=HF_TOKEN)
33
+ )
34
+
35
+ os.environ["IMAGE_DIR"] = str(image_dir)
36
+
37
+ image_files = list(Path(image_dir).rglob("*.[pjw]*[npjg]*[ge]*"))
38
+
39
+ return image_files
40
+
41
+
42
+ with gr.Blocks(
43
+ analytics_enabled=False, title="Image Gallery", theme="NoCrypt/miku"
44
+ ) as demo:
45
+ gr.HTML("""<center><h1>Image Gallery</h1></center>""")
46
+ submit = gr.Button("Refresh", variant="primary")
47
+ gallery = gr.Gallery(
48
+ value="", columns=4, show_label=False, height=800, object_fit="contain"
49
+ )
50
+
51
+ submit.click(refresh_images, outputs=[gallery])
52
+
53
+
54
+ demo.launch(debug=True)