Spaces:
Running
Running
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") |