Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,76 +9,58 @@ import io
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
print("
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
).to("cpu")
|
| 22 |
|
|
|
|
| 23 |
pipe.enable_attention_slicing()
|
| 24 |
pipe.enable_vae_slicing()
|
| 25 |
-
|
| 26 |
-
# =========================
|
| 27 |
-
# FORCE enable PEFT backend
|
| 28 |
-
# =========================
|
| 29 |
-
import peft # 🔥 مهم جدًا
|
| 30 |
-
|
| 31 |
-
# =========================
|
| 32 |
-
# Load LoRA
|
| 33 |
-
# =========================
|
| 34 |
-
LORA_PATH = hf_hub_download(
|
| 35 |
-
repo_id="ebraam1/interior-sd-models",
|
| 36 |
-
filename="Interior_lora.safetensors"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
print("Loading LoRA...")
|
| 40 |
-
|
| 41 |
-
pipe.load_lora_weights(LORA_PATH)
|
| 42 |
-
pipe.fuse_lora(lora_scale=0.8)
|
| 43 |
|
| 44 |
print("Model ready 🔥")
|
| 45 |
|
| 46 |
-
# =========================
|
| 47 |
class Prompt(BaseModel):
|
| 48 |
prompt: str
|
| 49 |
|
| 50 |
-
def to_bytes(img):
|
| 51 |
buf = io.BytesIO()
|
| 52 |
img.save(buf, format="PNG")
|
| 53 |
buf.seek(0)
|
| 54 |
return buf
|
| 55 |
|
| 56 |
-
# =========================
|
| 57 |
@app.post("/txt2img")
|
| 58 |
def generate(data: Prompt):
|
| 59 |
-
|
| 60 |
image = pipe(
|
| 61 |
data.prompt,
|
| 62 |
-
num_inference_steps=
|
| 63 |
-
guidance_scale=5,
|
| 64 |
-
height=
|
| 65 |
-
width=
|
| 66 |
).images[0]
|
| 67 |
|
| 68 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
| 69 |
|
| 70 |
-
|
| 71 |
@app.post("/img2img")
|
| 72 |
async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
|
| 73 |
-
|
| 74 |
-
img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((256,256))
|
| 75 |
|
| 76 |
image = pipe(
|
| 77 |
prompt=prompt,
|
| 78 |
image=img,
|
| 79 |
strength=0.6,
|
| 80 |
-
num_inference_steps=
|
| 81 |
-
guidance_scale=5
|
| 82 |
).images[0]
|
| 83 |
|
| 84 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
MODEL_REPO = "ebraam1/interior-sd-models"
|
| 13 |
+
MODEL_FILE = "Interior.safetensors"
|
| 14 |
+
|
| 15 |
+
print("Downloading model file...")
|
| 16 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 17 |
+
|
| 18 |
+
print("Loading model on CPU (this may take a while)...")
|
| 19 |
+
pipe = StableDiffusionPipeline.from_single_file(
|
| 20 |
+
model_path,
|
| 21 |
+
torch_dtype=torch.float32, # ضروري لـ CPU
|
| 22 |
+
safety_checker=None, # تسريع مع تجاهل فلتر NSFW
|
| 23 |
+
requires_safety_checker=False
|
| 24 |
).to("cpu")
|
| 25 |
|
| 26 |
+
# تحسينات لتقليل استهلاك الميموري على الـ CPU
|
| 27 |
pipe.enable_attention_slicing()
|
| 28 |
pipe.enable_vae_slicing()
|
| 29 |
+
pipe.enable_sequential_cpu_offload() # ينقل أجزاء الموديل للـ CPU حسب الحاجة
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
print("Model ready 🔥")
|
| 32 |
|
|
|
|
| 33 |
class Prompt(BaseModel):
|
| 34 |
prompt: str
|
| 35 |
|
| 36 |
+
def to_bytes(img: Image.Image):
|
| 37 |
buf = io.BytesIO()
|
| 38 |
img.save(buf, format="PNG")
|
| 39 |
buf.seek(0)
|
| 40 |
return buf
|
| 41 |
|
|
|
|
| 42 |
@app.post("/txt2img")
|
| 43 |
def generate(data: Prompt):
|
|
|
|
| 44 |
image = pipe(
|
| 45 |
data.prompt,
|
| 46 |
+
num_inference_steps=10, # ممكن تقلل لـ 6 لو عايز سرعة
|
| 47 |
+
guidance_scale=7.5,
|
| 48 |
+
height=512,
|
| 49 |
+
width=512
|
| 50 |
).images[0]
|
| 51 |
|
| 52 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
| 53 |
|
|
|
|
| 54 |
@app.post("/img2img")
|
| 55 |
async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
|
| 56 |
+
img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((512, 512))
|
|
|
|
| 57 |
|
| 58 |
image = pipe(
|
| 59 |
prompt=prompt,
|
| 60 |
image=img,
|
| 61 |
strength=0.6,
|
| 62 |
+
num_inference_steps=10,
|
| 63 |
+
guidance_scale=7.5
|
| 64 |
).images[0]
|
| 65 |
|
| 66 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|