Spaces:
Sleeping
Sleeping
Initial commit
Browse files- app.py +73 -0
- cache/cached_inference_here +0 -0
- requirements.txt +11 -0
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
import io
|
3 |
+
import torch
|
4 |
+
from pathlib import Path
|
5 |
+
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, UniPCMultistepScheduler
|
6 |
+
from PIL import Image, ImageOps
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# ---- Model loading ----
|
10 |
+
CACHE_DIR = "./cache"
|
11 |
+
CNET_MODEL = "MrPio/Texture-Anything_CNet-SD15"
|
12 |
+
SD_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
|
13 |
+
|
14 |
+
controlnet = ControlNetModel.from_pretrained(
|
15 |
+
CNET_MODEL, cache_dir=CACHE_DIR, torch_dtype=torch.float16, local_files_only=True
|
16 |
+
)
|
17 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
18 |
+
SD_MODEL,
|
19 |
+
controlnet=controlnet,
|
20 |
+
cache_dir=CACHE_DIR,
|
21 |
+
torch_dtype=torch.float16,
|
22 |
+
safety_checker=None,
|
23 |
+
)
|
24 |
+
|
25 |
+
# speed & memory optimizations
|
26 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
27 |
+
pipe.enable_xformers_memory_efficient_attention() # if xformers installed
|
28 |
+
pipe.enable_model_cpu_offload()
|
29 |
+
|
30 |
+
|
31 |
+
def pil2hash(image: Image.Image) -> str:
|
32 |
+
buffer = io.BytesIO()
|
33 |
+
image.save(buffer, format="PNG")
|
34 |
+
image_bytes = buffer.getvalue()
|
35 |
+
return hashlib.sha256(image_bytes).hexdigest()
|
36 |
+
|
37 |
+
|
38 |
+
def caption2hash(caption: str) -> str:
|
39 |
+
return hashlib.sha256(caption.encode()).hexdigest()
|
40 |
+
|
41 |
+
|
42 |
+
# ---- Inference function ----
|
43 |
+
def infer(caption: str, condition_image: Image.Image, steps: int = 20, seed: int = 0, invert: bool = False):
|
44 |
+
img = condition_image.convert("RGB")
|
45 |
+
if invert:
|
46 |
+
img = ImageOps.invert(img)
|
47 |
+
cache_file = Path(f"inferences/{pil2hash(img)}_{caption2hash(caption)}.png")
|
48 |
+
if cache_file.exists():
|
49 |
+
return Image.open(cache_file)
|
50 |
+
|
51 |
+
generator = torch.manual_seed(seed)
|
52 |
+
output = pipe(prompt=caption, image=img, num_inference_steps=steps, generator=generator).images[0]
|
53 |
+
output.save(cache_file)
|
54 |
+
return output
|
55 |
+
|
56 |
+
|
57 |
+
# ---- Gradio UI + API ----
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("## ControlNet + Stable Diffusion 1.5")
|
60 |
+
with gr.Row():
|
61 |
+
txt = gr.Textbox(label="Prompt", placeholder="Describe the texture...")
|
62 |
+
cond = gr.Image(type="pil", label="Condition Image")
|
63 |
+
with gr.Row():
|
64 |
+
steps = gr.Slider(1, 50, value=20, label="Inference Steps")
|
65 |
+
seed = gr.Number(value=0, label="Seed (0 for random)")
|
66 |
+
inv = gr.Checkbox(label="Invert UV colors?")
|
67 |
+
btn = gr.Button("Generate")
|
68 |
+
out = gr.Image(label="Output")
|
69 |
+
|
70 |
+
btn.click(fn=infer, inputs=[txt, cond, steps, seed, inv], outputs=out)
|
71 |
+
|
72 |
+
# enable the standard gradio REST API (/run/predict)
|
73 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
cache/cached_inference_here
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
diffusers
|
3 |
+
huggingface-hub
|
4 |
+
numpy<2
|
5 |
+
requests
|
6 |
+
safetensors
|
7 |
+
torch
|
8 |
+
torchvision
|
9 |
+
tqdm
|
10 |
+
xformers
|
11 |
+
gradio
|