RetroGazzaSpurs commited on
Commit
59aa1d8
Β·
verified Β·
1 Parent(s): 16e6e9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -26
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
  from huggingface_hub import list_models
3
 
4
  # list_spaces is not guaranteed to exist in all HF runtimes
@@ -8,6 +10,50 @@ except ImportError:
8
  list_spaces = None
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def browse_loras(limit=20):
12
  models = list(list_models(
13
  tags=["lora"],
@@ -28,34 +74,48 @@ def browse_loras(limit=20):
28
 
29
 
30
  def browse_workflows(limit=20):
31
- if list_spaces is None:
32
- return "Spaces API unavailable in this runtime."
33
 
34
- spaces = list(list_spaces(
35
- sort="likes",
36
- direction=-1,
37
- limit=limit,
38
- ))
39
-
40
- workflows = [
41
- s for s in spaces
42
- if any(
43
- tag in (s.tags or [])
44
- for tag in ["comfyui", "workflow", "pipeline", "diffusers"]
45
  )
46
- ]
47
 
48
- if not workflows:
49
- return "No workflows found."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- return "\n---\n".join(
52
- f"### {s.id}\n"
53
- f"⭐ {s.likes or 0}\n"
54
- f"https://huggingface.co/spaces/{s.id}"
55
- for s in workflows[:limit]
56
- )
57
 
58
 
 
 
 
59
  with gr.Blocks() as demo:
60
  gr.Markdown("# πŸ€— Workflow & LoRA Browser")
61
 
@@ -71,7 +131,3 @@ with gr.Blocks() as demo:
71
  lora_out = gr.Markdown()
72
  gr.Button("Browse LoRAs").click(
73
  browse_loras,
74
- outputs=lora_out,
75
- )
76
-
77
- demo.launch()
 
1
  import gradio as gr
2
+ import shutil
3
+ from pathlib import Path
4
  from huggingface_hub import list_models
5
 
6
  # list_spaces is not guaranteed to exist in all HF runtimes
 
10
  list_spaces = None
11
 
12
 
13
+ # ===============================
14
+ # Persistent storage
15
+ # ===============================
16
+ WORKFLOW_DIR = Path("/data/workflows")
17
+ WORKFLOW_DIR.mkdir(parents=True, exist_ok=True)
18
+
19
+ ALLOWED_EXTS = {".json", ".yaml", ".yml", ".png"}
20
+
21
+
22
+ # ===============================
23
+ # Workflow upload handlers
24
+ # ===============================
25
+ def upload_workflow(file):
26
+ if file is None:
27
+ return "No file uploaded."
28
+
29
+ src = Path(file.name)
30
+ ext = src.suffix.lower()
31
+
32
+ if ext not in ALLOWED_EXTS:
33
+ return f"❌ Unsupported file type: `{ext}`"
34
+
35
+ dest = WORKFLOW_DIR / src.name
36
+ shutil.copy(src, dest)
37
+
38
+ return f"βœ… Saved workflow: `{dest.name}`"
39
+
40
+
41
+ def list_uploaded_workflows():
42
+ files = sorted(WORKFLOW_DIR.glob("*"))
43
+
44
+ if not files:
45
+ return "No uploaded workflows yet."
46
+
47
+ return "\n---\n".join(
48
+ f"### {f.name}\n"
49
+ f"`{f.suffix}` | {f.stat().st_size // 1024} KB"
50
+ for f in files
51
+ )
52
+
53
+
54
+ # ===============================
55
+ # Hub browsing functions
56
+ # ===============================
57
  def browse_loras(limit=20):
58
  models = list(list_models(
59
  tags=["lora"],
 
74
 
75
 
76
  def browse_workflows(limit=20):
77
+ uploaded_files = sorted(WORKFLOW_DIR.glob("*"))
 
78
 
79
+ uploaded_md = ""
80
+ if uploaded_files:
81
+ uploaded_md = "## πŸ§‘β€πŸ’» Uploaded Workflows\n\n" + "\n---\n".join(
82
+ f"### {f.name}\nLocal upload"
83
+ for f in uploaded_files
 
 
 
 
 
 
84
  )
 
85
 
86
+ if list_spaces is None:
87
+ remote_md = "Spaces API unavailable."
88
+ else:
89
+ spaces = list(list_spaces(
90
+ sort="likes",
91
+ direction=-1,
92
+ limit=limit,
93
+ ))
94
+
95
+ workflows = [
96
+ s for s in spaces
97
+ if any(
98
+ tag in (s.tags or [])
99
+ for tag in ["comfyui", "workflow", "pipeline", "diffusers"]
100
+ )
101
+ ]
102
+
103
+ if workflows:
104
+ remote_md = "## 🌍 Community Workflows\n\n" + "\n---\n".join(
105
+ f"### {s.id}\n"
106
+ f"⭐ {s.likes or 0}\n"
107
+ f"https://huggingface.co/spaces/{s.id}"
108
+ for s in workflows[:limit]
109
+ )
110
+ else:
111
+ remote_md = "No community workflows found."
112
 
113
+ return uploaded_md + "\n\n" + remote_md
 
 
 
 
 
114
 
115
 
116
+ # ===============================
117
+ # Gradio UI
118
+ # ===============================
119
  with gr.Blocks() as demo:
120
  gr.Markdown("# πŸ€— Workflow & LoRA Browser")
121
 
 
131
  lora_out = gr.Markdown()
132
  gr.Button("Browse LoRAs").click(
133
  browse_loras,