import gradio as gr from PIL import Image import torch from diffusers import StableDiffusionInpaintPipeline import traceback device = "cpu" try: MODEL_ID = "runwayml/stable-diffusion-inpainting" pipe = StableDiffusionInpaintPipeline.from_pretrained( MODEL_ID, torch_dtype=torch.float32 ) pipe = pipe.to(device) except Exception as e: print("Model yüklenirken hata oluştu:", e) traceback.print_exc() def tryon(person_img, cloth_img, prompt="A photo of the person wearing the selected clothing, realistic, photorealistic"): try: if person_img is None or cloth_img is None: return None person = person_img.convert("RGB") cloth = cloth_img.convert("RGBA") pw, ph = person.size cw, ch = cloth.size scale = 0.6 * pw / cw new_w = max(10, int(cw * scale)) new_h = max(10, int(ch * scale)) cloth_resized = cloth.resize((new_w, new_h), Image.LANCZOS) alpha = cloth_resized.split()[-1] mask = Image.new("L", (pw, ph), 0) x = int((pw - new_w) / 2) y = int(ph * 0.18) mask.paste(alpha, (x, y)) composite = person.copy() composite.paste(cloth_resized, (x, y), cloth_resized) result = pipe( prompt=prompt, image=composite, mask_image=mask, guidance_scale=7.5, num_inference_steps=30 ).images[0] return result except Exception as e: print("Giydirme sırasında hata oluştu:", e) traceback.print_exc() return None with gr.Blocks() as demo: gr.Markdown("## 👕 AI Kıyafet Giydirme\\nFotoğrafını ve kıyafet resmini yükle, sonucu gör!") with gr.Row(): with gr.Column(): person_in = gr.Image(type="pil", label="Kullanıcı Fotoğrafı") cloth_in = gr.Image(type="pil", label="Kıyafet Fotoğrafı (PNG, şeffaf arka plan)") prompt_in = gr.Textbox(label="Prompt (opsiyonel)", placeholder="Açıklama ekleyebilirsin...") btn = gr.Button("Giydir") with gr.Column(): out = gr.Image(type="pil", label="Sonuç") btn.click(fn=tryon, inputs=[person_in, cloth_in, prompt_in], outputs=out) if __name__ == "__main__": demo.launch()