File size: 1,258 Bytes
3276943
c9069cb
3276943
6ed9ca5
d3d35f8
c9069cb
6ed9ca5
c9069cb
 
03eab93
 
 
c9069cb
fa1f84c
c9069cb
 
34de85d
ba5cc93
c9069cb
3276943
6ed9ca5
 
7026765
 
36f25f8
6ed9ca5
 
112cb31
34de85d
 
112cb31
bc331b4
34de85d
bc331b4
 
c9069cb
34de85d
17192ca
34de85d
bc331b4
 
 
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
import gradio as gr
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
import torch
import tempfile, os

model_id = "nitrosocke/mo-di-diffusion"

pipe = StableDiffusionPipeline.from_pretrained(
    model_id,
    torch_dtype=torch.float32,
    safety_checker=None,
    use_safetensors=False
)

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cpu")
pipe.enable_attention_slicing()

def text_to_cartoon(prompt):
    try:
        image = pipe(
            prompt=prompt,
            guidance_scale=7.5,       # کیفیت رنگ بهتر
            num_inference_steps=30,   # کمی کندتر ولی خیلی بهتر
            height=512, width=512
        ).images[0]

        tmpdir = tempfile.mkdtemp()
        file_path = os.path.join(tmpdir, "cartoon_image.png")
        image.save(file_path, "PNG")  # PNG برای کیفیت بالاتر
        return file_path
    except Exception as e:
        return f"Error: {str(e)}"

iface = gr.Interface(
    fn=text_to_cartoon,
    inputs=gr.Textbox(label="Prompt", placeholder="مثلاً: a fantasy cartoon castle"),
    outputs=gr.File(label="Download Image"),
    title="✨ High Quality Cartoon Generator (CPU)"
)

iface.launch()