Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ MODEL_REPO = "ebraam1/interior-sd-models"
|
|
| 12 |
BASE_MODEL = "Interior.safetensors"
|
| 13 |
LORA_FILE = "Interior_lora.safetensors"
|
| 14 |
|
| 15 |
-
#
|
| 16 |
print("Downloading base model...")
|
| 17 |
base_path = hf_hub_download(repo_id=MODEL_REPO, filename=BASE_MODEL)
|
| 18 |
|
|
@@ -24,41 +24,42 @@ pipe = StableDiffusionPipeline.from_single_file(
|
|
| 24 |
requires_safety_checker=False
|
| 25 |
).to("cpu")
|
| 26 |
|
| 27 |
-
#
|
| 28 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
print("Downloading LoRA...")
|
| 32 |
lora_path = hf_hub_download(repo_id=MODEL_REPO, filename=LORA_FILE)
|
| 33 |
|
| 34 |
print("Loading LoRA...")
|
| 35 |
pipe.load_lora_weights(lora_path)
|
| 36 |
-
pipe.fuse_lora(lora_scale=1.0)
|
| 37 |
|
| 38 |
# تحسينات الذاكرة
|
| 39 |
pipe.enable_attention_slicing()
|
| 40 |
pipe.enable_vae_slicing()
|
| 41 |
|
| 42 |
-
print("Model ready 🔥 (
|
| 43 |
|
| 44 |
-
# ---------- دوال مساعدة ----------
|
| 45 |
def to_bytes(img: Image.Image) -> io.BytesIO:
|
| 46 |
buf = io.BytesIO()
|
| 47 |
img.save(buf, format="PNG")
|
| 48 |
buf.seek(0)
|
| 49 |
return buf
|
| 50 |
|
| 51 |
-
#
|
| 52 |
@app.get("/txt2img")
|
| 53 |
def txt2img(
|
| 54 |
-
prompt: str = Query(..., description="الوصف
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
):
|
| 60 |
image = pipe(
|
| 61 |
prompt=prompt,
|
|
|
|
| 62 |
num_inference_steps=steps,
|
| 63 |
guidance_scale=guidance,
|
| 64 |
height=height,
|
|
@@ -66,20 +67,22 @@ def txt2img(
|
|
| 66 |
).images[0]
|
| 67 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
@app.post("/img2img")
|
| 71 |
async def img2img(
|
| 72 |
file: UploadFile = File(...),
|
| 73 |
prompt: str = Query(""),
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
strength: float = Query(0.6, ge=0.0, le=1.0),
|
| 77 |
-
height: int = Query(
|
| 78 |
-
width: int = Query(
|
| 79 |
):
|
| 80 |
img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((width, height))
|
| 81 |
image = pipe(
|
| 82 |
prompt=prompt,
|
|
|
|
| 83 |
image=img,
|
| 84 |
strength=strength,
|
| 85 |
num_inference_steps=steps,
|
|
|
|
| 12 |
BASE_MODEL = "Interior.safetensors"
|
| 13 |
LORA_FILE = "Interior_lora.safetensors"
|
| 14 |
|
| 15 |
+
# تحميل الموديل الأساسي
|
| 16 |
print("Downloading base model...")
|
| 17 |
base_path = hf_hub_download(repo_id=MODEL_REPO, filename=BASE_MODEL)
|
| 18 |
|
|
|
|
| 24 |
requires_safety_checker=False
|
| 25 |
).to("cpu")
|
| 26 |
|
| 27 |
+
# أفضل Scheduler للجودة العالية
|
| 28 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 29 |
|
| 30 |
+
# تحميل LoRA ودمجه
|
| 31 |
print("Downloading LoRA...")
|
| 32 |
lora_path = hf_hub_download(repo_id=MODEL_REPO, filename=LORA_FILE)
|
| 33 |
|
| 34 |
print("Loading LoRA...")
|
| 35 |
pipe.load_lora_weights(lora_path)
|
| 36 |
+
pipe.fuse_lora(lora_scale=1.0)
|
| 37 |
|
| 38 |
# تحسينات الذاكرة
|
| 39 |
pipe.enable_attention_slicing()
|
| 40 |
pipe.enable_vae_slicing()
|
| 41 |
|
| 42 |
+
print("Model ready 🔥 (Max quality mode - Landscape)")
|
| 43 |
|
|
|
|
| 44 |
def to_bytes(img: Image.Image) -> io.BytesIO:
|
| 45 |
buf = io.BytesIO()
|
| 46 |
img.save(buf, format="PNG")
|
| 47 |
buf.seek(0)
|
| 48 |
return buf
|
| 49 |
|
| 50 |
+
# ================== Text-to-Image (GET) ==================
|
| 51 |
@app.get("/txt2img")
|
| 52 |
def txt2img(
|
| 53 |
+
prompt: str = Query(..., description="الوصف"),
|
| 54 |
+
negative_prompt: str = Query("", description="العناصر اللي عايز تتجنبها"),
|
| 55 |
+
steps: int = Query(20, ge=1, le=30),
|
| 56 |
+
guidance: float = Query(9.0, ge=1.0, le=20.0),
|
| 57 |
+
height: int = Query(512, ge=256, le=768), # أصبح أقل (عرضي)
|
| 58 |
+
width: int = Query(768, ge=256, le=768) # أصبح أكبر
|
| 59 |
):
|
| 60 |
image = pipe(
|
| 61 |
prompt=prompt,
|
| 62 |
+
negative_prompt=negative_prompt or None,
|
| 63 |
num_inference_steps=steps,
|
| 64 |
guidance_scale=guidance,
|
| 65 |
height=height,
|
|
|
|
| 67 |
).images[0]
|
| 68 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
| 69 |
|
| 70 |
+
# ================== Image-to-Image (POST) ==================
|
| 71 |
@app.post("/img2img")
|
| 72 |
async def img2img(
|
| 73 |
file: UploadFile = File(...),
|
| 74 |
prompt: str = Query(""),
|
| 75 |
+
negative_prompt: str = Query(""),
|
| 76 |
+
steps: int = Query(20, ge=1, le=30),
|
| 77 |
+
guidance: float = Query(9.0, ge=1.0, le=20.0),
|
| 78 |
strength: float = Query(0.6, ge=0.0, le=1.0),
|
| 79 |
+
height: int = Query(512, ge=256, le=768),
|
| 80 |
+
width: int = Query(768, ge=256, le=768)
|
| 81 |
):
|
| 82 |
img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((width, height))
|
| 83 |
image = pipe(
|
| 84 |
prompt=prompt,
|
| 85 |
+
negative_prompt=negative_prompt or None,
|
| 86 |
image=img,
|
| 87 |
strength=strength,
|
| 88 |
num_inference_steps=steps,
|