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

device = "cpu"

print("Loading CPU model... this may take 20–40 seconds.")

pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    safety_checker=None,
    torch_dtype=torch.float32
).to(device)

def generate(img, prompt, strength, steps, guidance):
    if img is None:
        return "لطفاً عکس ورودی بده."

    result = pipe(
        prompt=prompt,
        image=img,
        strength=strength,
        num_inference_steps=steps,
        guidance_scale=guidance
    ).images[0]

    return result

with gr.Blocks(title="CPU Portrait Generator") as demo:

    gr.Markdown("## **نسخه CPU – بدون GPU – سازگار با Space رایگان**")

    with gr.Row():

        with gr.Column():
            input_img = gr.Image(type="pil", label="عکس ورودی")
            prompt = gr.Textbox(label="پرامپت")
            strength = gr.Slider(0.1, 1.0, value=0.6, label="قدرت ادیت")
            steps = gr.Slider(10, 40, value=25, label="Steps")
            guidance = gr.Slider(1, 12, value=7.5, label="Guidance Scale")
            btn = gr.Button("ساخت تصویر")

        with gr.Column():
            output = gr.Image(label="خروجی نهایی")

    btn.click(
        fn=generate,
        inputs=[input_img, prompt, strength, steps, guidance],
        outputs=output
    )

demo.launch()