Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import threading
|
|
| 6 |
import time
|
| 7 |
from collections import OrderedDict
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -15,7 +16,7 @@ app = FastAPI()
|
|
| 15 |
torch.set_num_threads(2)
|
| 16 |
|
| 17 |
# =========================
|
| 18 |
-
# 🔥 MODEL (
|
| 19 |
# =========================
|
| 20 |
model_name = "Lykon/dreamshaper-7"
|
| 21 |
|
|
@@ -27,6 +28,10 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
| 27 |
|
| 28 |
pipe = pipe.to("cpu")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
pipe.enable_attention_slicing()
|
| 31 |
|
| 32 |
# =========================
|
|
@@ -37,11 +42,44 @@ queue = []
|
|
| 37 |
progress_db = {}
|
| 38 |
|
| 39 |
MAX_HISTORY = 40
|
| 40 |
-
NUM_WORKERS = 1
|
| 41 |
-
|
| 42 |
IMG_DIR = "images"
|
| 43 |
os.makedirs(IMG_DIR, exist_ok=True)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# =========================
|
| 46 |
# 🚀 GENERATION ENGINE
|
| 47 |
# =========================
|
|
@@ -49,33 +87,37 @@ def generate_ai_stream(message: str, mode="fast"):
|
|
| 49 |
|
| 50 |
try:
|
| 51 |
start = time.time()
|
|
|
|
| 52 |
|
| 53 |
-
# ⚡
|
| 54 |
if mode == "fast":
|
| 55 |
-
steps =
|
| 56 |
cfg = 1.5
|
|
|
|
| 57 |
else:
|
| 58 |
-
steps =
|
| 59 |
-
cfg =
|
| 60 |
-
|
| 61 |
-
progress_db[message] = 0
|
| 62 |
|
| 63 |
-
# 🔥 fake
|
| 64 |
for i in range(steps):
|
| 65 |
progress_db[message] = int((i / steps) * 100)
|
| 66 |
-
time.sleep(0.
|
| 67 |
|
| 68 |
image = pipe(
|
| 69 |
message,
|
| 70 |
num_inference_steps=steps,
|
| 71 |
guidance_scale=cfg,
|
| 72 |
-
height=
|
| 73 |
-
width=
|
| 74 |
).images[0]
|
| 75 |
|
| 76 |
filename = f"{IMG_DIR}/img_{int(time.time()*1000)}.png"
|
| 77 |
image.save(filename)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
progress_db[message] = 100
|
| 80 |
|
| 81 |
duration = round(time.time() - start, 2)
|
|
@@ -119,7 +161,7 @@ threading.Thread(target=worker, daemon=True).start()
|
|
| 119 |
|
| 120 |
@app.get("/")
|
| 121 |
async def root():
|
| 122 |
-
return PlainTextResponse("⚡
|
| 123 |
|
| 124 |
|
| 125 |
# 🚀 FAST MODE
|
|
@@ -150,7 +192,7 @@ async def quality(message: str):
|
|
| 150 |
return PlainTextResponse("accepted")
|
| 151 |
|
| 152 |
|
| 153 |
-
# 📡
|
| 154 |
@app.get("/get")
|
| 155 |
async def get(message: str):
|
| 156 |
|
|
@@ -159,7 +201,6 @@ async def get(message: str):
|
|
| 159 |
|
| 160 |
data = db[message]
|
| 161 |
|
| 162 |
-
# 🔥 если идёт генерация → показываем прогресс
|
| 163 |
if data["status"] == "pending":
|
| 164 |
progress = progress_db.get(message, 0)
|
| 165 |
return PlainTextResponse(f"generating... {progress}%")
|
|
@@ -167,9 +208,10 @@ async def get(message: str):
|
|
| 167 |
return PlainTextResponse(data["reply"])
|
| 168 |
|
| 169 |
|
| 170 |
-
# 🖼
|
| 171 |
@app.get("/image")
|
| 172 |
async def get_image(path: str):
|
|
|
|
| 173 |
if not os.path.exists(path):
|
| 174 |
return PlainTextResponse("file not found")
|
| 175 |
|
|
|
|
| 6 |
import time
|
| 7 |
from collections import OrderedDict
|
| 8 |
import os
|
| 9 |
+
from PIL import Image # 🔥 NEW
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
| 16 |
torch.set_num_threads(2)
|
| 17 |
|
| 18 |
# =========================
|
| 19 |
+
# 🔥 MODEL (ТВОЙ РАБОЧИЙ)
|
| 20 |
# =========================
|
| 21 |
model_name = "Lykon/dreamshaper-7"
|
| 22 |
|
|
|
|
| 28 |
|
| 29 |
pipe = pipe.to("cpu")
|
| 30 |
|
| 31 |
+
# 🔥 LCM LoRA (оставляем как у тебя)
|
| 32 |
+
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
|
| 33 |
+
pipe.fuse_lora()
|
| 34 |
+
|
| 35 |
pipe.enable_attention_slicing()
|
| 36 |
|
| 37 |
# =========================
|
|
|
|
| 42 |
progress_db = {}
|
| 43 |
|
| 44 |
MAX_HISTORY = 40
|
|
|
|
|
|
|
| 45 |
IMG_DIR = "images"
|
| 46 |
os.makedirs(IMG_DIR, exist_ok=True)
|
| 47 |
|
| 48 |
+
# =========================
|
| 49 |
+
# ✂️ SPLIT FUNCTION (12 частей)
|
| 50 |
+
# =========================
|
| 51 |
+
def split_image_into_12(img_path: str):
|
| 52 |
+
|
| 53 |
+
img = Image.open(img_path)
|
| 54 |
+
|
| 55 |
+
w, h = img.size
|
| 56 |
+
|
| 57 |
+
cols = 3
|
| 58 |
+
rows = 4
|
| 59 |
+
|
| 60 |
+
tile_w = w // cols
|
| 61 |
+
tile_h = h // rows
|
| 62 |
+
|
| 63 |
+
base = img_path.replace(".png", "")
|
| 64 |
+
|
| 65 |
+
index = 1
|
| 66 |
+
|
| 67 |
+
for r in range(rows):
|
| 68 |
+
for c in range(cols):
|
| 69 |
+
|
| 70 |
+
left = c * tile_w
|
| 71 |
+
top = r * tile_h
|
| 72 |
+
right = left + tile_w
|
| 73 |
+
bottom = top + tile_h
|
| 74 |
+
|
| 75 |
+
crop = img.crop((left, top, right, bottom))
|
| 76 |
+
|
| 77 |
+
out_path = f"{base}_{index}.png"
|
| 78 |
+
crop.save(out_path)
|
| 79 |
+
|
| 80 |
+
index += 1
|
| 81 |
+
|
| 82 |
+
|
| 83 |
# =========================
|
| 84 |
# 🚀 GENERATION ENGINE
|
| 85 |
# =========================
|
|
|
|
| 87 |
|
| 88 |
try:
|
| 89 |
start = time.time()
|
| 90 |
+
progress_db[message] = 0
|
| 91 |
|
| 92 |
+
# ⚡ FAST / QUALITY
|
| 93 |
if mode == "fast":
|
| 94 |
+
steps = 4
|
| 95 |
cfg = 1.5
|
| 96 |
+
h, w = 256, 256
|
| 97 |
else:
|
| 98 |
+
steps = 12
|
| 99 |
+
cfg = 6.5
|
| 100 |
+
h, w = 512, 512
|
|
|
|
| 101 |
|
| 102 |
+
# 🔥 fake progress
|
| 103 |
for i in range(steps):
|
| 104 |
progress_db[message] = int((i / steps) * 100)
|
| 105 |
+
time.sleep(0.08)
|
| 106 |
|
| 107 |
image = pipe(
|
| 108 |
message,
|
| 109 |
num_inference_steps=steps,
|
| 110 |
guidance_scale=cfg,
|
| 111 |
+
height=h,
|
| 112 |
+
width=w
|
| 113 |
).images[0]
|
| 114 |
|
| 115 |
filename = f"{IMG_DIR}/img_{int(time.time()*1000)}.png"
|
| 116 |
image.save(filename)
|
| 117 |
|
| 118 |
+
# 🔥 NEW: split into 12 parts
|
| 119 |
+
split_image_into_12(filename)
|
| 120 |
+
|
| 121 |
progress_db[message] = 100
|
| 122 |
|
| 123 |
duration = round(time.time() - start, 2)
|
|
|
|
| 161 |
|
| 162 |
@app.get("/")
|
| 163 |
async def root():
|
| 164 |
+
return PlainTextResponse("⚡ Dreamshaper + LCM + FAST/QUALITY + SPLIT 12 READY")
|
| 165 |
|
| 166 |
|
| 167 |
# 🚀 FAST MODE
|
|
|
|
| 192 |
return PlainTextResponse("accepted")
|
| 193 |
|
| 194 |
|
| 195 |
+
# 📡 GET + PROGRESS
|
| 196 |
@app.get("/get")
|
| 197 |
async def get(message: str):
|
| 198 |
|
|
|
|
| 201 |
|
| 202 |
data = db[message]
|
| 203 |
|
|
|
|
| 204 |
if data["status"] == "pending":
|
| 205 |
progress = progress_db.get(message, 0)
|
| 206 |
return PlainTextResponse(f"generating... {progress}%")
|
|
|
|
| 208 |
return PlainTextResponse(data["reply"])
|
| 209 |
|
| 210 |
|
| 211 |
+
# 🖼 FILE SERVE
|
| 212 |
@app.get("/image")
|
| 213 |
async def get_image(path: str):
|
| 214 |
+
|
| 215 |
if not os.path.exists(path):
|
| 216 |
return PlainTextResponse("file not found")
|
| 217 |
|