Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,30 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
-
import
|
|
|
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
import io
|
| 7 |
-
from fastapi.responses import StreamingResponse
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
safety_checker=None
|
| 23 |
).to("cpu")
|
| 24 |
|
|
@@ -27,16 +33,16 @@ print("Loading LoRA...")
|
|
| 27 |
pipe.load_lora_weights(LORA_PATH)
|
| 28 |
pipe.fuse_lora(lora_scale=0.8)
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
| 31 |
pipe.enable_attention_slicing()
|
| 32 |
pipe.enable_vae_slicing()
|
| 33 |
|
| 34 |
print("Model ready 🔥")
|
| 35 |
|
| 36 |
|
| 37 |
-
# ========================
|
| 38 |
-
# REQUEST MODEL
|
| 39 |
-
# ========================
|
| 40 |
class Prompt(BaseModel):
|
| 41 |
prompt: str
|
| 42 |
|
|
@@ -48,15 +54,15 @@ def to_bytes(img):
|
|
| 48 |
return buf
|
| 49 |
|
| 50 |
|
| 51 |
-
# ========================
|
| 52 |
# TXT2IMG
|
| 53 |
-
# ========================
|
| 54 |
@app.post("/txt2img")
|
| 55 |
def generate(data: Prompt):
|
| 56 |
|
| 57 |
image = pipe(
|
| 58 |
data.prompt,
|
| 59 |
-
num_inference_steps=6,
|
| 60 |
guidance_scale=5,
|
| 61 |
height=256,
|
| 62 |
width=256
|
|
@@ -65,14 +71,14 @@ def generate(data: Prompt):
|
|
| 65 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
| 66 |
|
| 67 |
|
| 68 |
-
# ========================
|
| 69 |
-
# IMG2IMG
|
| 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")
|
| 75 |
-
img = img.resize((256, 256))
|
| 76 |
|
| 77 |
image = pipe(
|
| 78 |
prompt=prompt,
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
from fastapi.responses import StreamingResponse
|
| 6 |
from PIL import Image
|
| 7 |
+
import torch
|
| 8 |
import io
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# =========================
|
| 13 |
+
# تحميل LoRA من HF
|
| 14 |
+
# =========================
|
| 15 |
+
LORA_PATH = hf_hub_download(
|
| 16 |
+
repo_id="ebraam1/interior-sd-models",
|
| 17 |
+
filename="Interior_lora.safetensors"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
print("Loading base model...")
|
| 21 |
+
|
| 22 |
+
# =========================
|
| 23 |
+
# ✔ FIX: استخدم pretrained model بدل single_file
|
| 24 |
+
# =========================
|
| 25 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 26 |
+
"runwayml/stable-diffusion-v1-5",
|
| 27 |
+
torch_dtype=torch.float32,
|
| 28 |
safety_checker=None
|
| 29 |
).to("cpu")
|
| 30 |
|
|
|
|
| 33 |
pipe.load_lora_weights(LORA_PATH)
|
| 34 |
pipe.fuse_lora(lora_scale=0.8)
|
| 35 |
|
| 36 |
+
# =========================
|
| 37 |
+
# ⚡ Speed optimizations
|
| 38 |
+
# =========================
|
| 39 |
pipe.enable_attention_slicing()
|
| 40 |
pipe.enable_vae_slicing()
|
| 41 |
|
| 42 |
print("Model ready 🔥")
|
| 43 |
|
| 44 |
|
| 45 |
+
# =========================
|
|
|
|
|
|
|
| 46 |
class Prompt(BaseModel):
|
| 47 |
prompt: str
|
| 48 |
|
|
|
|
| 54 |
return buf
|
| 55 |
|
| 56 |
|
| 57 |
+
# =========================
|
| 58 |
# TXT2IMG
|
| 59 |
+
# =========================
|
| 60 |
@app.post("/txt2img")
|
| 61 |
def generate(data: Prompt):
|
| 62 |
|
| 63 |
image = pipe(
|
| 64 |
data.prompt,
|
| 65 |
+
num_inference_steps=6,
|
| 66 |
guidance_scale=5,
|
| 67 |
height=256,
|
| 68 |
width=256
|
|
|
|
| 71 |
return StreamingResponse(to_bytes(image), media_type="image/png")
|
| 72 |
|
| 73 |
|
| 74 |
+
# =========================
|
| 75 |
+
# IMG2IMG (correct way)
|
| 76 |
+
# =========================
|
| 77 |
@app.post("/img2img")
|
| 78 |
async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
|
| 79 |
|
| 80 |
img = Image.open(io.BytesIO(await file.read())).convert("RGB")
|
| 81 |
+
img = img.resize((256, 256))
|
| 82 |
|
| 83 |
image = pipe(
|
| 84 |
prompt=prompt,
|