File size: 2,230 Bytes
764f1e8
 
 
0ef5f85
22e9227
764f1e8
990c5ce
764f1e8
22e9227
990c5ce
 
0ef5f85
 
 
 
fc45919
764f1e8
 
fc45919
0ef5f85
 
 
990c5ce
0ef5f85
 
764f1e8
0ef5f85
 
764f1e8
0ef5f85
764f1e8
 
 
 
 
0ef5f85
764f1e8
 
 
b5e3251
2b3a7bb
0ef5f85
 
764f1e8
 
 
6f01bd9
764f1e8
6f01bd9
764f1e8
 
 
 
990c5ce
 
 
 
 
764f1e8
 
 
 
 
 
 
990c5ce
 
 
764f1e8
 
 
c098b55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from fastapi import FastAPI, UploadFile, File
from pydantic import BaseModel
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
from huggingface_hub import hf_hub_download   # <-- ุฌุฏูŠุฏ
import torch
from PIL import Image
import io
from fastapi.responses import StreamingResponse

app = FastAPI()

# --- ุฑูŠุจูˆ ุงู„ู…ู„ูุงุช ุนู„ู‰ Hugging Face ---
MODEL_REPO = "ebraam1/interior-sd-models"
BASE_MODEL_FILE = "Interior.safetensors"
LORA_FILE = "Interior_lora.safetensors"

device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32

# --- ุชุญู…ูŠู„ ุงู„ู…ู„ูุงุช ู…ู† ุงู„ู€ Hub ---
print("Downloading base model...")
base_path = hf_hub_download(repo_id=MODEL_REPO, filename=BASE_MODEL_FILE)

print("Downloading LoRA...")
lora_path = hf_hub_download(repo_id=MODEL_REPO, filename=LORA_FILE)

# --- ุชุญู…ูŠู„ ุงู„ุจุงูŠุจู„ุงูŠู†ุฒ ู…ู† ุงู„ู…ู„ู ุงู„ู…ุญู„ูŠ (ู†ูุณ ูƒูˆุฏ ูƒูˆู„ุงุจ) ---
print("Loading base model...")
txt2img = StableDiffusionPipeline.from_single_file(
    base_path,                # <-- ุงู„ู…ุณุงุฑ ุงู„ู…ุญู„ูŠ ุจุนุฏ ุงู„ุชุญู…ูŠู„
    torch_dtype=dtype,
    safety_checker=None
).to(device)

img2img = StableDiffusionImg2ImgPipeline.from_single_file(
    base_path,
    torch_dtype=dtype,
    safety_checker=None
).to(device)

print("Loading LoRA...")
txt2img.load_lora_weights(lora_path)
img2img.load_lora_weights(lora_path)

txt2img.fuse_lora(lora_scale=0.8)
img2img.fuse_lora(lora_scale=0.8)

print("LoRA loaded ๐Ÿ”ฅ")

class Prompt(BaseModel):
    prompt: str

def to_bytes(img):
    buf = io.BytesIO()
    img.save(buf, format="PNG")
    buf.seek(0)
    return buf

@app.get("/")
def home():
    return {"status": "API is running ๐Ÿš€"}

@app.post("/txt2img")
def generate(data: Prompt):
    image = txt2img(data.prompt).images[0]
    return StreamingResponse(to_bytes(image), media_type="image/png")

@app.post("/img2img")
async def img2img_api(file: UploadFile = File(...), prompt: str = ""):
    img = Image.open(io.BytesIO(await file.read())).convert("RGB").resize((512, 512))
    image = img2img(prompt=prompt, image=img).images[0]
    return StreamingResponse(to_bytes(image), media_type="image/png")