Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from pathlib import Path
|
|
| 4 |
import os
|
| 5 |
from huggingface_hub import list_models
|
| 6 |
|
| 7 |
-
#
|
| 8 |
try:
|
| 9 |
from huggingface_hub import list_spaces
|
| 10 |
except ImportError:
|
|
@@ -13,8 +13,8 @@ except ImportError:
|
|
| 13 |
# ===============================
|
| 14 |
# Configuration
|
| 15 |
# ===============================
|
| 16 |
-
|
| 17 |
-
|
| 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
|
| 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
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 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()
|
|
|