RetroGazzaSpurs commited on
Commit
f724384
·
verified ·
1 Parent(s): 1598b2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -50
app.py CHANGED
@@ -4,7 +4,7 @@ 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
8
  try:
9
  from huggingface_hub import list_spaces
10
  except ImportError:
@@ -13,8 +13,8 @@ except ImportError:
13
  # ===============================
14
  # Configuration
15
  # ===============================
16
- OWNER_USERNAME = "RetroGazzaSpurs" # HF 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")
@@ -27,11 +27,14 @@ for d in [WORKFLOW_DIR, WORKFLOW_IMG_DIR, LORA_DIR, LORA_IMG_DIR]:
27
  ALLOWED_FILE_EXTS = {".json", ".yaml", ".yml", ".png", ".safetensors", ".ckpt"}
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)
@@ -40,7 +43,6 @@ def upload_workflow(file, image=None):
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:
@@ -48,7 +50,11 @@ def upload_workflow(file, image=None):
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)
@@ -63,9 +69,8 @@ def upload_lora(file, image=None):
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("*"))
@@ -94,7 +99,6 @@ def list_uploaded_loras():
94
  md += f"### {f.name}\n(no image)\n---\n"
95
  return md
96
 
97
-
98
  # ===============================
99
  # Hub browsing functions
100
  # ===============================
@@ -108,9 +112,7 @@ def browse_loras(limit=20):
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"
113
- f"https://huggingface.co/{m.modelId}"
114
  for m in models
115
  )
116
 
@@ -144,7 +146,6 @@ def browse_workflows(limit=20):
144
  remote_md = "No community workflows found."
145
  return uploaded_md + "\n\n" + remote_md
146
 
147
-
148
  # ===============================
149
  # Gradio UI
150
  # ===============================
@@ -168,41 +169,40 @@ with gr.Blocks() as demo:
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()
208
-
 
4
  import os
5
  from huggingface_hub import list_models
6
 
7
+ # Optional: Spaces API might not exist
8
  try:
9
  from huggingface_hub import list_spaces
10
  except ImportError:
 
13
  # ===============================
14
  # Configuration
15
  # ===============================
16
+ # Password is stored securely as a Hugging Face secret, not in code
17
+ UPLOAD_PASSWORD = os.environ.get("UPLOAD_PASSWORD") # must be set in Space secrets
18
 
19
  WORKFLOW_DIR = Path("/data/workflows")
20
  WORKFLOW_IMG_DIR = Path("/data/workflows_images")
 
27
  ALLOWED_FILE_EXTS = {".json", ".yaml", ".yml", ".png", ".safetensors", ".ckpt"}
28
  ALLOWED_IMG_EXTS = {".png", ".jpg", ".jpeg", ".webp"}
29
 
 
30
  # ===============================
31
  # Upload handlers
32
  # ===============================
33
+ def upload_workflow(file, image=None, password=""):
34
+ if UPLOAD_PASSWORD is None:
35
+ return "❌ Upload password not set in Space secrets."
36
+ if password != UPLOAD_PASSWORD:
37
+ return "❌ Unauthorized"
38
  if file is None:
39
  return "No file uploaded."
40
  src = Path(file.name)
 
43
  return f"❌ Unsupported file type: `{ext}`"
44
  dest = WORKFLOW_DIR / src.name
45
  shutil.copy(src, dest)
 
46
  if image:
47
  img_ext = Path(image.name).suffix.lower()
48
  if img_ext in ALLOWED_IMG_EXTS:
 
50
  return f"✅ Saved workflow: `{dest.name}`"
51
 
52
 
53
+ def upload_lora(file, image=None, password=""):
54
+ if UPLOAD_PASSWORD is None:
55
+ return "❌ Upload password not set in Space secrets."
56
+ if password != UPLOAD_PASSWORD:
57
+ return "❌ Unauthorized"
58
  if file is None:
59
  return "No file uploaded."
60
  src = Path(file.name)
 
69
  shutil.copy(image.name, LORA_IMG_DIR / (src.stem + img_ext))
70
  return f"✅ Saved LoRA: `{dest.name}`"
71
 
 
72
  # ===============================
73
+ # Listing functions
74
  # ===============================
75
  def list_uploaded_workflows():
76
  files = sorted(WORKFLOW_DIR.glob("*"))
 
99
  md += f"### {f.name}\n(no image)\n---\n"
100
  return md
101
 
 
102
  # ===============================
103
  # Hub browsing functions
104
  # ===============================
 
112
  if not models:
113
  return "No LoRAs found."
114
  return "\n---\n".join(
115
+ f"### {m.modelId}\n⬇️ {m.downloads or 0} | ⭐ {m.likes or 0}\nhttps://huggingface.co/{m.modelId}"
 
 
116
  for m in models
117
  )
118
 
 
146
  remote_md = "No community workflows found."
147
  return uploaded_md + "\n\n" + remote_md
148
 
 
149
  # ===============================
150
  # Gradio UI
151
  # ===============================
 
169
  outputs=lora_out,
170
  )
171
 
172
+ # Upload Workflow Tab (always visible, password-protected)
173
+ with gr.Tab("⬆️ Upload Workflow"):
174
+ gr.Markdown("Upload workflow files with optional reference image (.json/.yaml/.png). Owner only (password required).")
175
+ wf_file = gr.File(label="Workflow file")
176
+ wf_image = gr.File(label="Reference image (optional)")
177
+ wf_password = gr.Textbox(label="Password", type="password")
178
+ wf_status = gr.Markdown()
179
+ gr.Button("Upload").click(
180
+ upload_workflow,
181
+ inputs=[wf_file, wf_image, wf_password],
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 Tab (always visible, password-protected)
191
+ with gr.Tab("⬆️ Upload LoRA"):
192
+ gr.Markdown("Upload LoRA files with optional reference image (.safetensors/.ckpt/.png). Owner only (password required).")
193
+ lora_file = gr.File(label="LoRA file")
194
+ lora_image = gr.File(label="Reference image (optional)")
195
+ lora_password = gr.Textbox(label="Password", type="password")
196
+ lora_status = gr.Markdown()
197
+ gr.Button("Upload").click(
198
+ upload_lora,
199
+ inputs=[lora_file, lora_image, lora_password],
200
+ outputs=lora_status,
201
+ )
202
+ lora_list = gr.Markdown()
203
+ gr.Button("Refresh Uploaded LoRAs").click(
204
+ list_uploaded_loras,
205
+ outputs=lora_list,
206
+ )
207
 
208
  demo.launch()