Update app_demo.py
Browse files- app_demo.py +77 -4
app_demo.py
CHANGED
|
@@ -1,4 +1,76 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from __future__ import annotations
|
| 3 |
import os
|
| 4 |
import random
|
|
@@ -13,7 +85,7 @@ import uuid
|
|
| 13 |
|
| 14 |
model_id = "Lykon/dreamshaper-xl-v2-turbo"
|
| 15 |
DESCRIPTION = '''# Fast Stable Diffusion CPU with Latent Consistency Model
|
| 16 |
-
Distilled from [Dreamshaper v7](https://huggingface.co/Lykon/dreamshaper-7) fine‑tune of SD v1-5.
|
| 17 |
'''
|
| 18 |
if not torch.cuda.is_available():
|
| 19 |
DESCRIPTION += "\n<p>running on CPU.</p>"
|
|
@@ -114,8 +186,8 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 114 |
gallery = gr.Gallery(
|
| 115 |
label="Generated images",
|
| 116 |
show_label=False,
|
| 117 |
-
elem_id="gallery"
|
| 118 |
-
|
| 119 |
)
|
| 120 |
|
| 121 |
with gr.Accordion("Advanced options", open=False):
|
|
@@ -139,3 +211,4 @@ with gr.Blocks(css="style.css") as demo:
|
|
| 139 |
|
| 140 |
demo.queue()
|
| 141 |
demo.launch()
|
|
|
|
|
|
| 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 |
+
hfuser_input = gr.Textbox(label="Hugging Face Username")
|
| 47 |
+
hfuser_models = gr.Dropdown(label="Models from User", choices=[], multiselect=True)
|
| 48 |
+
|
| 49 |
+
user_model_dict = gr.Textbox(label="Dict Input (e.g., {'username': ['model1', 'model2']})")
|
| 50 |
+
|
| 51 |
+
parallel_toggle = gr.Checkbox(label="Load in Parallel", value=True)
|
| 52 |
+
output = gr.Textbox(label="Output", lines=10)
|
| 53 |
+
|
| 54 |
+
def update_models(hfuser):
|
| 55 |
+
return gr.update(choices=validate_and_list_models(hfuser))
|
| 56 |
+
|
| 57 |
+
def update_from_dict(dict_str):
|
| 58 |
+
parsed = parse_user_model_dict(dict_str)
|
| 59 |
+
if not parsed:
|
| 60 |
+
return gr.update(), gr.update()
|
| 61 |
+
hfuser = next(iter(parsed))
|
| 62 |
+
return gr.update(value=hfuser), gr.update(choices=parsed[hfuser], value=parsed[hfuser])
|
| 63 |
+
|
| 64 |
+
hfuser_input.change(update_models, hfuser_input, hfuser_models)
|
| 65 |
+
user_model_dict.change(update_from_dict, user_model_dict, [hfuser_input, hfuser_models])
|
| 66 |
+
|
| 67 |
+
run_btn = gr.Button("Load Models")
|
| 68 |
+
run_btn.click(run_models, [hfuser_models, parallel_toggle], output)
|
| 69 |
+
|
| 70 |
+
demo.launch()
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
'''#!/usr/bin/env python
|
| 74 |
from __future__ import annotations
|
| 75 |
import os
|
| 76 |
import random
|
|
|
|
| 85 |
|
| 86 |
model_id = "Lykon/dreamshaper-xl-v2-turbo"
|
| 87 |
DESCRIPTION = '''# Fast Stable Diffusion CPU with Latent Consistency Model
|
| 88 |
+
#Distilled from [Dreamshaper v7](https://huggingface.co/Lykon/dreamshaper-7) fine‑tune of SD v1-5.
|
| 89 |
'''
|
| 90 |
if not torch.cuda.is_available():
|
| 91 |
DESCRIPTION += "\n<p>running on CPU.</p>"
|
|
|
|
| 186 |
gallery = gr.Gallery(
|
| 187 |
label="Generated images",
|
| 188 |
show_label=False,
|
| 189 |
+
elem_id="gallery"
|
| 190 |
+
|
| 191 |
)
|
| 192 |
|
| 193 |
with gr.Accordion("Advanced options", open=False):
|
|
|
|
| 211 |
|
| 212 |
demo.queue()
|
| 213 |
demo.launch()
|
| 214 |
+
'''
|