ebraam1 commited on
Commit
5605b35
·
verified ·
1 Parent(s): a5df33b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -29
app.py CHANGED
@@ -6,22 +6,22 @@ from fastapi.responses import StreamingResponse
6
  from PIL import Image
7
  import torch
8
  import io
9
- from safetensors.torch import load_file
10
 
11
  app = FastAPI()
12
 
13
  # =========================
14
- # Load FULL MODEL (CPU)
15
  # =========================
16
- MODEL_PATH = hf_hub_download(
17
  repo_id="ebraam1/interior-sd-models",
18
- filename="Interior.safetensors"
19
  )
20
 
21
- print("Loading SD checkpoint... (CPU mode)")
22
 
23
- pipe = StableDiffusionPipeline.from_single_file(
24
- MODEL_PATH,
 
25
  torch_dtype=torch.float32,
26
  safety_checker=None
27
  ).to("cpu")
@@ -29,24 +29,13 @@ pipe = StableDiffusionPipeline.from_single_file(
29
  pipe.enable_attention_slicing()
30
  pipe.enable_vae_slicing()
31
 
32
- print("Base model loaded 🔥")
33
 
34
- # =========================
35
- # Load LoRA manually (SAFE)
36
- # =========================
37
- LORA_PATH = hf_hub_download(
38
- repo_id="ebraam1/interior-sd-models",
39
- filename="Interior_lora.safetensors"
40
- )
41
-
42
- print("Loading LoRA manually...")
43
-
44
- lora_state_dict = load_file(LORA_PATH)
45
 
46
- # 👇 هذا هو السحر الحقيقي
47
- pipe.load_attn_procs(lora_state_dict)
48
-
49
- print("LoRA loaded 🔥")
50
 
51
  # =========================
52
  class Prompt(BaseModel):
@@ -58,15 +47,13 @@ def to_bytes(img):
58
  buf.seek(0)
59
  return buf
60
 
61
- # =========================
62
- # TXT2IMG
63
  # =========================
64
  @app.post("/txt2img")
65
  def generate(data: Prompt):
66
 
67
  image = pipe(
68
  data.prompt,
69
- num_inference_steps=8, # CPU → خليها منخفضة
70
  guidance_scale=5,
71
  height=256,
72
  width=256
@@ -74,8 +61,6 @@ def generate(data: Prompt):
74
 
75
  return StreamingResponse(to_bytes(image), media_type="image/png")
76
 
77
- # =========================
78
- # IMG2IMG
79
  # =========================
80
  @app.post("/img2img")
81
  async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
@@ -86,7 +71,7 @@ async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
86
  prompt=prompt,
87
  image=img,
88
  strength=0.6,
89
- num_inference_steps=8,
90
  guidance_scale=5
91
  ).images[0]
92
 
 
6
  from PIL import Image
7
  import torch
8
  import io
 
9
 
10
  app = FastAPI()
11
 
12
  # =========================
13
+ # Load LoRA ONLY (ignore checkpoint)
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 (CPU safe)...")
21
 
22
+ # ⚠️ IMPORTANT: base model only (no single_file)
23
+ pipe = StableDiffusionPipeline.from_pretrained(
24
+ "runwayml/stable-diffusion-v1-5",
25
  torch_dtype=torch.float32,
26
  safety_checker=None
27
  ).to("cpu")
 
29
  pipe.enable_attention_slicing()
30
  pipe.enable_vae_slicing()
31
 
32
+ print("Loading LoRA safely...")
33
 
34
+ # 🔥 SAFE LoRA loading (diffusers supported way)
35
+ pipe.load_lora_weights(LORA_PATH)
36
+ pipe.fuse_lora(lora_scale=0.8)
 
 
 
 
 
 
 
 
37
 
38
+ print("Model ready 🔥")
 
 
 
39
 
40
  # =========================
41
  class Prompt(BaseModel):
 
47
  buf.seek(0)
48
  return buf
49
 
 
 
50
  # =========================
51
  @app.post("/txt2img")
52
  def generate(data: Prompt):
53
 
54
  image = pipe(
55
  data.prompt,
56
+ num_inference_steps=6, # CPU optimized
57
  guidance_scale=5,
58
  height=256,
59
  width=256
 
61
 
62
  return StreamingResponse(to_bytes(image), media_type="image/png")
63
 
 
 
64
  # =========================
65
  @app.post("/img2img")
66
  async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
 
71
  prompt=prompt,
72
  image=img,
73
  strength=0.6,
74
+ num_inference_steps=6,
75
  guidance_scale=5
76
  ).images[0]
77