ebraam1 commited on
Commit
6f01bd9
·
verified ·
1 Parent(s): e286fc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -12
app.py CHANGED
@@ -6,18 +6,22 @@ from fastapi.responses import StreamingResponse
6
  from PIL import Image
7
  import torch
8
  import io
 
9
 
10
  app = FastAPI()
11
 
12
- LORA_PATH = hf_hub_download(
 
 
 
13
  repo_id="ebraam1/interior-sd-models",
14
- filename="Interior_lora.safetensors"
15
  )
16
 
17
- print("Loading base model...")
18
 
19
- pipe = StableDiffusionPipeline.from_pretrained(
20
- "runwayml/stable-diffusion-v1-5",
21
  torch_dtype=torch.float32,
22
  safety_checker=None
23
  ).to("cpu")
@@ -25,14 +29,26 @@ pipe = StableDiffusionPipeline.from_pretrained(
25
  pipe.enable_attention_slicing()
26
  pipe.enable_vae_slicing()
27
 
28
- print("Loading LoRA (kohya format)...")
29
 
30
- # 🔥 الطريقة الصح للـ LoRA بتاعك
31
- pipe.load_lora_weights(LORA_PATH)
32
- pipe.fuse_lora(lora_scale=0.8)
 
 
 
 
33
 
34
- print("Model ready 🔥")
35
 
 
 
 
 
 
 
 
 
36
  class Prompt(BaseModel):
37
  prompt: str
38
 
@@ -42,11 +58,15 @@ def to_bytes(img):
42
  buf.seek(0)
43
  return buf
44
 
 
 
 
45
  @app.post("/txt2img")
46
  def generate(data: Prompt):
 
47
  image = pipe(
48
  data.prompt,
49
- num_inference_steps=6,
50
  guidance_scale=5,
51
  height=256,
52
  width=256
@@ -54,15 +74,19 @@ def generate(data: Prompt):
54
 
55
  return StreamingResponse(to_bytes(image), media_type="image/png")
56
 
 
 
 
57
  @app.post("/img2img")
58
  async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
 
59
  img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((256,256))
60
 
61
  image = pipe(
62
  prompt=prompt,
63
  image=img,
64
  strength=0.6,
65
- num_inference_steps=6,
66
  guidance_scale=5
67
  ).images[0]
68
 
 
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
  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):
53
  prompt: str
54
 
 
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
 
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 = ""):
82
+
83
  img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((256,256))
84
 
85
  image = pipe(
86
  prompt=prompt,
87
  image=img,
88
  strength=0.6,
89
+ num_inference_steps=8,
90
  guidance_scale=5
91
  ).images[0]
92