Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,14 @@ import os
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
| 13 |
torch.set_num_threads(2)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
model_name = "Lykon/dreamshaper-7"
|
| 16 |
|
| 17 |
pipe = StableDiffusionPipeline.from_pretrained(
|
|
@@ -22,36 +27,48 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
| 22 |
|
| 23 |
pipe = pipe.to("cpu")
|
| 24 |
|
| 25 |
-
# 🔥 LCM LOADING (КЛЮЧЕВОЕ)
|
| 26 |
-
pipe.load_lora_weights(
|
| 27 |
-
"latent-consistency/lcm-lora-sdv1-5"
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
pipe.fuse_lora()
|
| 31 |
-
|
| 32 |
-
# ❌ никаких scheduler замен (важно!)
|
| 33 |
pipe.enable_attention_slicing()
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
db = OrderedDict()
|
| 39 |
queue = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
IMG_DIR = "images"
|
| 42 |
os.makedirs(IMG_DIR, exist_ok=True)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
try:
|
| 49 |
start = time.time()
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
image = pipe(
|
| 52 |
message,
|
| 53 |
-
num_inference_steps=
|
| 54 |
-
guidance_scale=
|
| 55 |
height=256,
|
| 56 |
width=256
|
| 57 |
).images[0]
|
|
@@ -59,9 +76,11 @@ def generate_ai_stream(message: str):
|
|
| 59 |
filename = f"{IMG_DIR}/img_{int(time.time()*1000)}.png"
|
| 60 |
image.save(filename)
|
| 61 |
|
|
|
|
|
|
|
| 62 |
duration = round(time.time() - start, 2)
|
| 63 |
|
| 64 |
-
result = f"{filename} | {duration}s"
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
result = f"error: {str(e)}"
|
|
@@ -70,19 +89,23 @@ def generate_ai_stream(message: str):
|
|
| 70 |
db[message]["reply"] = result
|
| 71 |
db[message]["status"] = "done"
|
| 72 |
|
|
|
|
|
|
|
| 73 |
return result
|
| 74 |
|
| 75 |
|
| 76 |
-
#
|
|
|
|
|
|
|
| 77 |
def worker():
|
| 78 |
while True:
|
| 79 |
if queue:
|
| 80 |
-
message = queue.pop(0)
|
| 81 |
|
| 82 |
if message in db and db[message]["status"] == "done":
|
| 83 |
continue
|
| 84 |
|
| 85 |
-
generate_ai_stream(message)
|
| 86 |
else:
|
| 87 |
time.sleep(0.03)
|
| 88 |
|
|
@@ -90,17 +113,36 @@ def worker():
|
|
| 90 |
threading.Thread(target=worker, daemon=True).start()
|
| 91 |
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
@app.get("/")
|
| 94 |
async def root():
|
| 95 |
-
return PlainTextResponse("⚡ LCM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
if message not in db:
|
| 102 |
db[message] = {"status": "pending", "reply": ""}
|
| 103 |
-
queue.append(message)
|
| 104 |
|
| 105 |
if len(db) > MAX_HISTORY:
|
| 106 |
db.popitem(last=False)
|
|
@@ -108,6 +150,7 @@ async def ask(message: str):
|
|
| 108 |
return PlainTextResponse("accepted")
|
| 109 |
|
| 110 |
|
|
|
|
| 111 |
@app.get("/get")
|
| 112 |
async def get(message: str):
|
| 113 |
|
|
@@ -116,17 +159,26 @@ async def get(message: str):
|
|
| 116 |
|
| 117 |
data = db[message]
|
| 118 |
|
|
|
|
| 119 |
if data["status"] == "pending":
|
| 120 |
-
|
|
|
|
| 121 |
|
| 122 |
return PlainTextResponse(data["reply"])
|
| 123 |
|
| 124 |
|
|
|
|
| 125 |
@app.get("/image")
|
| 126 |
async def get_image(path: str):
|
|
|
|
|
|
|
|
|
|
| 127 |
return FileResponse(path)
|
| 128 |
|
| 129 |
|
|
|
|
|
|
|
|
|
|
| 130 |
if __name__ == "__main__":
|
| 131 |
import uvicorn
|
| 132 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# =========================
|
| 13 |
+
# ⚡ CPU OPTIMIZATION
|
| 14 |
+
# =========================
|
| 15 |
torch.set_num_threads(2)
|
| 16 |
|
| 17 |
+
# =========================
|
| 18 |
+
# 🔥 MODEL (STABLE LCM-FRIENDLY)
|
| 19 |
+
# =========================
|
| 20 |
model_name = "Lykon/dreamshaper-7"
|
| 21 |
|
| 22 |
pipe = StableDiffusionPipeline.from_pretrained(
|
|
|
|
| 27 |
|
| 28 |
pipe = pipe.to("cpu")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
pipe.enable_attention_slicing()
|
| 31 |
|
| 32 |
+
# =========================
|
| 33 |
+
# 📦 STORAGE
|
| 34 |
+
# =========================
|
| 35 |
db = OrderedDict()
|
| 36 |
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 |
+
# =========================
|
| 48 |
+
def generate_ai_stream(message: str, mode="fast"):
|
| 49 |
|
| 50 |
try:
|
| 51 |
start = time.time()
|
| 52 |
|
| 53 |
+
# ⚡ режимы
|
| 54 |
+
if mode == "fast":
|
| 55 |
+
steps = 2
|
| 56 |
+
cfg = 1.5
|
| 57 |
+
else:
|
| 58 |
+
steps = 6
|
| 59 |
+
cfg = 3.0
|
| 60 |
+
|
| 61 |
+
progress_db[message] = 0
|
| 62 |
+
|
| 63 |
+
# 🔥 fake-progress (CPU-safe)
|
| 64 |
+
for i in range(steps):
|
| 65 |
+
progress_db[message] = int((i / steps) * 100)
|
| 66 |
+
time.sleep(0.12)
|
| 67 |
+
|
| 68 |
image = pipe(
|
| 69 |
message,
|
| 70 |
+
num_inference_steps=steps,
|
| 71 |
+
guidance_scale=cfg,
|
| 72 |
height=256,
|
| 73 |
width=256
|
| 74 |
).images[0]
|
|
|
|
| 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)
|
| 82 |
|
| 83 |
+
result = f"{filename} | {mode} | {duration}s"
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
result = f"error: {str(e)}"
|
|
|
|
| 89 |
db[message]["reply"] = result
|
| 90 |
db[message]["status"] = "done"
|
| 91 |
|
| 92 |
+
progress_db.pop(message, None)
|
| 93 |
+
|
| 94 |
return result
|
| 95 |
|
| 96 |
|
| 97 |
+
# =========================
|
| 98 |
+
# 🔄 WORKER
|
| 99 |
+
# =========================
|
| 100 |
def worker():
|
| 101 |
while True:
|
| 102 |
if queue:
|
| 103 |
+
message, mode = queue.pop(0)
|
| 104 |
|
| 105 |
if message in db and db[message]["status"] == "done":
|
| 106 |
continue
|
| 107 |
|
| 108 |
+
generate_ai_stream(message, mode)
|
| 109 |
else:
|
| 110 |
time.sleep(0.03)
|
| 111 |
|
|
|
|
| 113 |
threading.Thread(target=worker, daemon=True).start()
|
| 114 |
|
| 115 |
|
| 116 |
+
# =========================
|
| 117 |
+
# 🌐 API
|
| 118 |
+
# =========================
|
| 119 |
+
|
| 120 |
@app.get("/")
|
| 121 |
async def root():
|
| 122 |
+
return PlainTextResponse("⚡ CPU LCM Image Generator Running")
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# 🚀 FAST MODE
|
| 126 |
+
@app.get("/fast")
|
| 127 |
+
async def fast(message: str):
|
| 128 |
+
|
| 129 |
+
if message not in db:
|
| 130 |
+
db[message] = {"status": "pending", "reply": ""}
|
| 131 |
+
queue.append((message, "fast"))
|
| 132 |
|
| 133 |
+
if len(db) > MAX_HISTORY:
|
| 134 |
+
db.popitem(last=False)
|
| 135 |
|
| 136 |
+
return PlainTextResponse("accepted")
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# 🎨 QUALITY MODE
|
| 140 |
+
@app.get("/quality")
|
| 141 |
+
async def quality(message: str):
|
| 142 |
|
| 143 |
if message not in db:
|
| 144 |
db[message] = {"status": "pending", "reply": ""}
|
| 145 |
+
queue.append((message, "quality"))
|
| 146 |
|
| 147 |
if len(db) > MAX_HISTORY:
|
| 148 |
db.popitem(last=False)
|
|
|
|
| 150 |
return PlainTextResponse("accepted")
|
| 151 |
|
| 152 |
|
| 153 |
+
# 📡 MAIN STATUS + RESULT + PROGRESS
|
| 154 |
@app.get("/get")
|
| 155 |
async def get(message: str):
|
| 156 |
|
|
|
|
| 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}%")
|
| 166 |
|
| 167 |
return PlainTextResponse(data["reply"])
|
| 168 |
|
| 169 |
|
| 170 |
+
# 🖼 image serve
|
| 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 |
+
|
| 176 |
return FileResponse(path)
|
| 177 |
|
| 178 |
|
| 179 |
+
# =========================
|
| 180 |
+
# 🚀 RUN
|
| 181 |
+
# =========================
|
| 182 |
if __name__ == "__main__":
|
| 183 |
import uvicorn
|
| 184 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|