RetroGazzaSpurs commited on
Commit
236548e
Β·
verified Β·
1 Parent(s): c7362e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -52
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -9,46 +10,89 @@ try:
9
  except ImportError:
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
  # ===============================
@@ -61,10 +105,8 @@ def browse_loras(limit=20):
61
  direction=-1,
62
  limit=limit,
63
  ))
64
-
65
  if not models:
66
  return "No LoRAs found."
67
-
68
  return "\n---\n".join(
69
  f"### {m.modelId}\n"
70
  f"⬇️ {m.downloads or 0} | ⭐ {m.likes or 0}\n"
@@ -75,14 +117,12 @@ def browse_loras(limit=20):
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:
@@ -91,25 +131,17 @@ def browse_workflows(limit=20):
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
 
@@ -136,26 +168,40 @@ with gr.Blocks() as demo:
136
  outputs=lora_out,
137
  )
138
 
139
- # Upload Tab
140
- with gr.Tab("⬆️ Upload Workflow"):
141
- gr.Markdown(
142
- "Upload workflow files (`.json`, `.yaml`, `.png`). "
143
- "Files are saved persistently."
144
- )
145
-
146
- wf_file = gr.File(label="Workflow file")
147
- upload_status = gr.Markdown()
148
-
149
- gr.Button("Upload").click(
150
- upload_workflow,
151
- inputs=wf_file,
152
- outputs=upload_status,
153
- )
154
-
155
- uploaded_list = gr.Markdown()
156
- gr.Button("Refresh Uploaded Workflows").click(
157
- list_uploaded_workflows,
158
- outputs=uploaded_list,
159
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  demo.launch()
 
1
  import gradio as gr
2
  import shutil
3
  from pathlib import Path
4
+ import os
5
  from huggingface_hub import list_models
6
 
7
  # list_spaces is not guaranteed to exist in all HF runtimes
 
10
  except ImportError:
11
  list_spaces = None
12
 
 
13
  # ===============================
14
+ # Configuration
15
  # ===============================
16
+ OWNER_USERNAME = "your_hf_username" # <-- replace with your username
17
+ current_user = os.environ.get("HUGGINGFACE_HUB_USER", "")
18
+
19
  WORKFLOW_DIR = Path("/data/workflows")
20
+ WORKFLOW_IMG_DIR = Path("/data/workflows_images")
21
+ LORA_DIR = Path("/data/loras")
22
+ LORA_IMG_DIR = Path("/data/loras_images")
23
+
24
+ for d in [WORKFLOW_DIR, WORKFLOW_IMG_DIR, LORA_DIR, LORA_IMG_DIR]:
25
+ d.mkdir(parents=True, exist_ok=True)
26
 
27
+ ALLOWED_FILE_EXTS = {".json", ".yaml", ".yml", ".png", ".safetensors"}
28
+ ALLOWED_IMG_EXTS = {".png", ".jpg", ".jpeg", ".webp"}
29
 
30
 
31
  # ===============================
32
+ # Upload handlers
33
  # ===============================
34
+ def upload_workflow(file, image=None):
35
  if file is None:
36
  return "No file uploaded."
 
37
  src = Path(file.name)
38
  ext = src.suffix.lower()
39
+ if ext not in ALLOWED_FILE_EXTS:
 
40
  return f"❌ Unsupported file type: `{ext}`"
 
41
  dest = WORKFLOW_DIR / src.name
42
  shutil.copy(src, dest)
43
+ # handle optional image
44
+ if image:
45
+ img_ext = Path(image.name).suffix.lower()
46
+ if img_ext in ALLOWED_IMG_EXTS:
47
+ shutil.copy(image.name, WORKFLOW_IMG_DIR / (src.stem + img_ext))
48
  return f"βœ… Saved workflow: `{dest.name}`"
49
 
50
 
51
+ def upload_lora(file, image=None):
52
+ if file is None:
53
+ return "No file uploaded."
54
+ src = Path(file.name)
55
+ ext = src.suffix.lower()
56
+ if ext not in ALLOWED_FILE_EXTS:
57
+ return f"❌ Unsupported file type: `{ext}`"
58
+ dest = LORA_DIR / src.name
59
+ shutil.copy(src, dest)
60
+ if image:
61
+ img_ext = Path(image.name).suffix.lower()
62
+ if img_ext in ALLOWED_IMG_EXTS:
63
+ shutil.copy(image.name, LORA_IMG_DIR / (src.stem + img_ext))
64
+ return f"βœ… Saved LoRA: `{dest.name}`"
65
+
66
+
67
+ # ===============================
68
+ # Listing functions with images
69
+ # ===============================
70
  def list_uploaded_workflows():
71
  files = sorted(WORKFLOW_DIR.glob("*"))
 
72
  if not files:
73
  return "No uploaded workflows yet."
74
+ md = ""
75
+ for f in files:
76
+ img_path = WORKFLOW_IMG_DIR / f"{f.stem}.png"
77
+ if img_path.exists():
78
+ md += f"### {f.name}\n![ref image]({img_path})\n---\n"
79
+ else:
80
+ md += f"### {f.name}\n(no image)\n---\n"
81
+ return md
82
 
83
+
84
+ def list_uploaded_loras():
85
+ files = sorted(LORA_DIR.glob("*"))
86
+ if not files:
87
+ return "No uploaded LoRAs yet."
88
+ md = ""
89
+ for f in files:
90
+ img_path = LORA_IMG_DIR / f"{f.stem}.png"
91
+ if img_path.exists():
92
+ md += f"### {f.name}\n![ref image]({img_path})\n---\n"
93
+ else:
94
+ md += f"### {f.name}\n(no image)\n---\n"
95
+ return md
96
 
97
 
98
  # ===============================
 
105
  direction=-1,
106
  limit=limit,
107
  ))
 
108
  if not models:
109
  return "No LoRAs found."
 
110
  return "\n---\n".join(
111
  f"### {m.modelId}\n"
112
  f"⬇️ {m.downloads or 0} | ⭐ {m.likes or 0}\n"
 
117
 
118
  def browse_workflows(limit=20):
119
  uploaded_files = sorted(WORKFLOW_DIR.glob("*"))
 
120
  uploaded_md = ""
121
  if uploaded_files:
122
  uploaded_md = "## πŸ§‘β€πŸ’» Uploaded Workflows\n\n" + "\n---\n".join(
123
  f"### {f.name}\nLocal upload"
124
  for f in uploaded_files
125
  )
 
126
  if list_spaces is None:
127
  remote_md = "Spaces API unavailable."
128
  else:
 
131
  direction=-1,
132
  limit=limit,
133
  ))
 
134
  workflows = [
135
  s for s in spaces
136
+ if any(tag in (s.tags or []) for tag in ["comfyui", "workflow", "pipeline", "diffusers"])
 
 
 
137
  ]
 
138
  if workflows:
139
  remote_md = "## 🌍 Community Workflows\n\n" + "\n---\n".join(
140
+ f"### {s.id}\n⭐ {s.likes or 0}\nhttps://huggingface.co/spaces/{s.id}"
 
 
141
  for s in workflows[:limit]
142
  )
143
  else:
144
  remote_md = "No community workflows found."
 
145
  return uploaded_md + "\n\n" + remote_md
146
 
147
 
 
168
  outputs=lora_out,
169
  )
170
 
171
+ # Upload tabs visible only to owner
172
+ if current_user == OWNER_USERNAME:
173
+ # Upload Workflow
174
+ with gr.Tab("⬆️ Upload Workflow"):
175
+ gr.Markdown("Upload workflow files with optional reference image (.json/.yaml/.png).")
176
+ wf_file = gr.File(label="Workflow file")
177
+ wf_image = gr.File(label="Reference image (optional)")
178
+ wf_status = gr.Markdown()
179
+ gr.Button("Upload").click(
180
+ upload_workflow,
181
+ inputs=[wf_file, wf_image],
182
+ outputs=wf_status,
183
+ )
184
+ wf_list = gr.Markdown()
185
+ gr.Button("Refresh Uploaded Workflows").click(
186
+ list_uploaded_workflows,
187
+ outputs=wf_list,
188
+ )
189
+
190
+ # Upload LoRA
191
+ with gr.Tab("⬆️ Upload LoRA"):
192
+ gr.Markdown("Upload LoRA files with optional reference image (.safetensors/.ckpt/.png).")
193
+ lora_file = gr.File(label="LoRA file")
194
+ lora_image = gr.File(label="Reference image (optional)")
195
+ lora_status = gr.Markdown()
196
+ gr.Button("Upload").click(
197
+ upload_lora,
198
+ inputs=[lora_file, lora_image],
199
+ outputs=lora_status,
200
+ )
201
+ lora_list = gr.Markdown()
202
+ gr.Button("Refresh Uploaded LoRAs").click(
203
+ list_uploaded_loras,
204
+ outputs=lora_list,
205
+ )
206
 
207
  demo.launch()