Update app_demo.py
Browse files- app_demo.py +133 -95
app_demo.py
CHANGED
|
@@ -1,101 +1,9 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from huggingface_hub import HfApi, snapshot_download
|
| 3 |
from concurrent.futures import ThreadPoolExecutor
|
| 4 |
import asyncio
|
| 5 |
import ast
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
-
api = HfApi()
|
| 9 |
-
executor = ThreadPoolExecutor()
|
| 10 |
-
model_cache = {}
|
| 11 |
-
|
| 12 |
-
def validate_and_list_models(hfuser):
|
| 13 |
-
try:
|
| 14 |
-
models = api.list_models(author=hfuser)
|
| 15 |
-
return [model.modelId for model in models if model.pipeline_tag == "text-to-image"]
|
| 16 |
-
except Exception:
|
| 17 |
-
return []
|
| 18 |
-
|
| 19 |
-
def parse_user_model_dict(user_model_dict_str):
|
| 20 |
-
try:
|
| 21 |
-
data = ast.literal_eval(user_model_dict_str)
|
| 22 |
-
if isinstance(data, dict) and all(isinstance(v, list) for v in data.values()):
|
| 23 |
-
return data
|
| 24 |
-
return {}
|
| 25 |
-
except Exception:
|
| 26 |
-
return {}
|
| 27 |
-
|
| 28 |
-
def load_model(model_id):
|
| 29 |
-
if model_id in model_cache:
|
| 30 |
-
return f"{model_id} loaded from cache"
|
| 31 |
-
try:
|
| 32 |
-
path = snapshot_download(repo_id=model_id, cache_dir="model_cache", token=os.getenv("HF_TOKEN"))
|
| 33 |
-
model_cache[model_id] = path
|
| 34 |
-
return f"{model_id} loaded successfully"
|
| 35 |
-
except Exception as e:
|
| 36 |
-
return f"{model_id} failed to load: {str(e)}"
|
| 37 |
-
|
| 38 |
-
def run_models(models, parallel):
|
| 39 |
-
if parallel:
|
| 40 |
-
futures = [executor.submit(load_model, m) for m in models]
|
| 41 |
-
return [f.result() for f in futures]
|
| 42 |
-
else:
|
| 43 |
-
return [load_model(m) for m in models]
|
| 44 |
-
|
| 45 |
-
with gr.Blocks() as demo:
|
| 46 |
-
with gr.Row():
|
| 47 |
-
with gr.Column(scale=1):
|
| 48 |
-
with gr.Row():
|
| 49 |
-
hfuser_input = gr.Textbox(label="Hugging Face Username")
|
| 50 |
-
hfuser_models = gr.Dropdown(label="Models from User", choices=[], multiselect=True)
|
| 51 |
-
user_model_dict = gr.Textbox(visible=False, label="Dict Input (e.g., {'username': ['model1', 'model2']})")
|
| 52 |
-
with gr.Row():
|
| 53 |
-
run_btn = gr.Button("Load Models")
|
| 54 |
-
with gr.Column(scale=3):
|
| 55 |
-
with gr.Row():
|
| 56 |
-
parallel_toggle = gr.Checkbox(label="Load in Parallel", value=True)
|
| 57 |
-
with gr.Row():
|
| 58 |
-
output = gr.Textbox(label="Output", lines=3)
|
| 59 |
-
with gr.Row():
|
| 60 |
-
gr.HTML(
|
| 61 |
-
f"""
|
| 62 |
-
<p id="project-links" align="center">
|
| 63 |
-
<a href='https://huggingface.co/spaces/charliebaby2023/Fast_Stable_diffusion_CPU/edit/main/app_demo.py'>Edit this app_demo py file</a>
|
| 64 |
-
<p> this is currently running the following model array </p>
|
| 65 |
-
|
| 66 |
-
</p>
|
| 67 |
-
"""
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
def update_models(hfuser):
|
| 71 |
-
if hfuser:
|
| 72 |
-
models = validate_and_list_models(hfuser)
|
| 73 |
-
label = f"Models found: {len(models)}"
|
| 74 |
-
else:
|
| 75 |
-
models = ''
|
| 76 |
-
label = ''
|
| 77 |
-
return gr.update(choices=models, label=label)
|
| 78 |
-
|
| 79 |
-
def update_from_dict(dict_str):
|
| 80 |
-
parsed = parse_user_model_dict(dict_str)
|
| 81 |
-
if not parsed:
|
| 82 |
-
return gr.update(), gr.update()
|
| 83 |
-
hfuser = next(iter(parsed))
|
| 84 |
-
models = parsed[hfuser]
|
| 85 |
-
label = f"Models found: {len(models)}"
|
| 86 |
-
return gr.update(value=hfuser), gr.update(choices=models, value=models, label=label)
|
| 87 |
-
#return gr.update(value=hfuser), gr.update(choices=parsed[hfuser], value=parsed[hfuser])
|
| 88 |
-
|
| 89 |
-
hfuser_input.change(update_models, hfuser_input, hfuser_models)
|
| 90 |
-
user_model_dict.change(update_from_dict, user_model_dict, [hfuser_input, hfuser_models])
|
| 91 |
-
run_btn.click(run_models, [hfuser_models, parallel_toggle], output)
|
| 92 |
-
|
| 93 |
-
demo.launch()
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
'''#!/usr/bin/env python
|
| 97 |
from __future__ import annotations
|
| 98 |
-
import os
|
| 99 |
import random
|
| 100 |
import time
|
| 101 |
import gradio as gr
|
|
@@ -103,7 +11,6 @@ import numpy as np
|
|
| 103 |
import PIL.Image
|
| 104 |
import torch
|
| 105 |
from diffusers import StableDiffusionPipeline
|
| 106 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 107 |
import uuid
|
| 108 |
|
| 109 |
model_id = "Lykon/dreamshaper-xl-v2-turbo"
|
|
@@ -112,11 +19,28 @@ DESCRIPTION = '''# Fast Stable Diffusion CPU with Latent Consistency Model
|
|
| 112 |
'''
|
| 113 |
if not torch.cuda.is_available():
|
| 114 |
DESCRIPTION += "\n<p>running on CPU.</p>"
|
| 115 |
-
|
| 116 |
MAX_SEED = np.iinfo(np.int32).max
|
| 117 |
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
|
| 118 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "768"))
|
| 119 |
DTYPE = torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Load pipeline once, disabling NSFW filter at construction time
|
| 122 |
pipe = StableDiffusionPipeline.from_pretrained(
|
|
@@ -196,7 +120,112 @@ examples = [
|
|
| 196 |
"Portrait of a cyborg queen, hyper‑detailed",
|
| 197 |
]
|
| 198 |
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
gr.Markdown(DESCRIPTION)
|
| 201 |
with gr.Group():
|
| 202 |
with gr.Row():
|
|
@@ -232,6 +261,15 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 232 |
cache_examples=CACHE_EXAMPLES,
|
| 233 |
)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
demo.queue()
|
| 236 |
demo.launch()
|
| 237 |
'''
|
|
|
|
|
|
|
| 1 |
from huggingface_hub import HfApi, snapshot_download
|
| 2 |
from concurrent.futures import ThreadPoolExecutor
|
| 3 |
import asyncio
|
| 4 |
import ast
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from __future__ import annotations
|
|
|
|
| 7 |
import random
|
| 8 |
import time
|
| 9 |
import gradio as gr
|
|
|
|
| 11 |
import PIL.Image
|
| 12 |
import torch
|
| 13 |
from diffusers import StableDiffusionPipeline
|
|
|
|
| 14 |
import uuid
|
| 15 |
|
| 16 |
model_id = "Lykon/dreamshaper-xl-v2-turbo"
|
|
|
|
| 19 |
'''
|
| 20 |
if not torch.cuda.is_available():
|
| 21 |
DESCRIPTION += "\n<p>running on CPU.</p>"
|
|
|
|
| 22 |
MAX_SEED = np.iinfo(np.int32).max
|
| 23 |
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
|
| 24 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "768"))
|
| 25 |
DTYPE = torch.float32
|
| 26 |
+
api = HfApi()
|
| 27 |
+
executor = ThreadPoolExecutor()
|
| 28 |
+
model_cache = {}
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
|
| 45 |
# Load pipeline once, disabling NSFW filter at construction time
|
| 46 |
pipe = StableDiffusionPipeline.from_pretrained(
|
|
|
|
| 120 |
"Portrait of a cyborg queen, hyper‑detailed",
|
| 121 |
]
|
| 122 |
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def validate_and_list_models(hfuser):
|
| 148 |
+
try:
|
| 149 |
+
models = api.list_models(author=hfuser)
|
| 150 |
+
return [model.modelId for model in models if model.pipeline_tag == "text-to-image"]
|
| 151 |
+
except Exception:
|
| 152 |
+
return []
|
| 153 |
+
|
| 154 |
+
def parse_user_model_dict(user_model_dict_str):
|
| 155 |
+
try:
|
| 156 |
+
data = ast.literal_eval(user_model_dict_str)
|
| 157 |
+
if isinstance(data, dict) and all(isinstance(v, list) for v in data.values()):
|
| 158 |
+
return data
|
| 159 |
+
return {}
|
| 160 |
+
except Exception:
|
| 161 |
+
return {}
|
| 162 |
+
|
| 163 |
+
def load_model(model_id):
|
| 164 |
+
if model_id in model_cache:
|
| 165 |
+
return f"{model_id} loaded from cache"
|
| 166 |
+
try:
|
| 167 |
+
path = snapshot_download(repo_id=model_id, cache_dir="model_cache", token=os.getenv("HF_TOKEN"))
|
| 168 |
+
model_cache[model_id] = path
|
| 169 |
+
return f"{model_id} loaded successfully"
|
| 170 |
+
except Exception as e:
|
| 171 |
+
return f"{model_id} failed to load: {str(e)}"
|
| 172 |
+
|
| 173 |
+
def run_models(models, parallel):
|
| 174 |
+
if parallel:
|
| 175 |
+
futures = [executor.submit(load_model, m) for m in models]
|
| 176 |
+
return [f.result() for f in futures]
|
| 177 |
+
else:
|
| 178 |
+
return [load_model(m) for m in models]
|
| 179 |
+
#with gr.Blocks(css="style.css") as demo:
|
| 180 |
+
with gr.Blocks() as demo:
|
| 181 |
+
with gr.Row():
|
| 182 |
+
with gr.Column(scale=1):
|
| 183 |
+
with gr.Row():
|
| 184 |
+
hfuser_input = gr.Textbox(label="Hugging Face Username")
|
| 185 |
+
hfuser_models = gr.Dropdown(label="Models from User", choices=[], multiselect=True)
|
| 186 |
+
user_model_dict = gr.Textbox(visible=False, label="Dict Input (e.g., {'username': ['model1', 'model2']})")
|
| 187 |
+
with gr.Row():
|
| 188 |
+
run_btn = gr.Button("Load Models")
|
| 189 |
+
with gr.Column(scale=3):
|
| 190 |
+
with gr.Row():
|
| 191 |
+
parallel_toggle = gr.Checkbox(label="Load in Parallel", value=True)
|
| 192 |
+
with gr.Row():
|
| 193 |
+
output = gr.Textbox(label="Output", lines=3)
|
| 194 |
+
with gr.Row():
|
| 195 |
+
gr.HTML(
|
| 196 |
+
f"""
|
| 197 |
+
<p id="project-links" align="center">
|
| 198 |
+
<a href='https://huggingface.co/spaces/charliebaby2023/Fast_Stable_diffusion_CPU/edit/main/app_demo.py'>Edit this app_demo py file</a>
|
| 199 |
+
<p> this is currently running the following model array </p>
|
| 200 |
+
|
| 201 |
+
</p>
|
| 202 |
+
"""
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
def update_models(hfuser):
|
| 206 |
+
if hfuser:
|
| 207 |
+
models = validate_and_list_models(hfuser)
|
| 208 |
+
label = f"Models found: {len(models)}"
|
| 209 |
+
else:
|
| 210 |
+
models = ''
|
| 211 |
+
label = ''
|
| 212 |
+
return gr.update(choices=models, label=label)
|
| 213 |
+
|
| 214 |
+
def update_from_dict(dict_str):
|
| 215 |
+
parsed = parse_user_model_dict(dict_str)
|
| 216 |
+
if not parsed:
|
| 217 |
+
return gr.update(), gr.update()
|
| 218 |
+
hfuser = next(iter(parsed))
|
| 219 |
+
models = parsed[hfuser]
|
| 220 |
+
label = f"Models found: {len(models)}"
|
| 221 |
+
return gr.update(value=hfuser), gr.update(choices=models, value=models, label=label)
|
| 222 |
+
#return gr.update(value=hfuser), gr.update(choices=parsed[hfuser], value=parsed[hfuser])
|
| 223 |
+
|
| 224 |
+
hfuser_input.change(update_models, hfuser_input, hfuser_models)
|
| 225 |
+
user_model_dict.change(update_from_dict, user_model_dict, [hfuser_input, hfuser_models])
|
| 226 |
+
run_btn.click(run_models, [hfuser_models, parallel_toggle], output)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
gr.Markdown(DESCRIPTION)
|
| 230 |
with gr.Group():
|
| 231 |
with gr.Row():
|
|
|
|
| 261 |
cache_examples=CACHE_EXAMPLES,
|
| 262 |
)
|
| 263 |
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
demo.launch()
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
'''#!/usr/bin/env python
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
|
| 273 |
demo.queue()
|
| 274 |
demo.launch()
|
| 275 |
'''
|