File size: 1,668 Bytes
eb04cf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from diffusers import StableDiffusionImg2ImgPipeline
import gradio as gr
from PIL import Image

# تحميل الموديل بصيغة مناسبة للـ CPU
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float32
)
pipe = pipe.to("cpu")

def generate_image(input_image, prompt, strength=0.7, guidance=7.5):
    if input_image is None or prompt.strip() == "":
        return None
    
    # تغيير حجم الصورة لتسريع المعالجة
    init_image = input_image.convert("RGB").resize((512, 512))
    result = pipe(
        prompt=prompt,
        image=init_image,
        strength=strength,
        guidance_scale=guidance
    ).images[0]
    return result

# واجهة Gradio
with gr.Blocks() as demo:
    gr.Markdown("## 🖼️ Stable Diffusion Img2Img - ديكور من صورة")
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(type="pil", label="ارفع الصورة")
            prompt = gr.Textbox(label="الوصف (Prompt)", placeholder="مثال: غرفة مع أثاث عصري وإضاءة دافئة")
            strength = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="درجة التغيير (Strength)")
            guidance = gr.Slider(1, 15, value=7.5, step=0.5, label="جودة التفاصيل (Guidance Scale)")
            btn = gr.Button("توليد الصورة")
        with gr.Column():
            output_image = gr.Image(label="النتيجة")
    
    btn.click(
        fn=generate_image,
        inputs=[input_image, prompt, strength, guidance],
        outputs=output_image
    )

demo.launch()