File size: 2,526 Bytes
ffe06c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
import gradio as gr
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
import torch
import peft

# Initialize the pipelines

text2img_pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16")
text2img_pipe.to("cuda")
text2img_pipe.load_lora_weights("AbdalrhmanRi/SDXL-Turbo-With-AppleVisionPro", weight_name="pytorch_lora_weights.safetensors")

img2img_pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16")
img2img_pipe.to("cuda")
img2img_pipe.load_lora_weights("AbdalrhmanRi/SDXL-Turbo-With-AppleVisionPro", weight_name="pytorch_lora_weights.safetensors")


def generate_image(prompt, init_image):
    if init_image is None:

        # Text-to-Image generation
        output_image = text2img_pipe(
            prompt,
            num_inference_steps=40,
            guidance_scale=2.0,
            height=480
        ).images[0]

        yield output_image, None

        output_refiner_image = text2img_pipe(
            prompt=prompt,
            image=output_image,
            guidance_scale=1.0,
            height=480
        ).images[0]

        yield output_image, output_refiner_image


    else:

        # Image-to-Image generation
        init_image = init_image.resize((512, 512))

        output_image = img2img_pipe(
            prompt,
            image=init_image,
            num_inference_steps=40,
            strength=0.5,
            guidance_scale=2.0,
            height=480
        ).images[0]

        yield output_image, None

        output_refiner_image = img2img_pipe(
            prompt=prompt,
            image=output_image,
            strength=0.5,
            guidance_scale=1.0,
            height=480
        ).images[0]

        yield output_image, output_refiner_image

# Define the Gradio interface
interface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here..."),
        gr.Image(type="pil", label="Initial Image (Optional)", height=360)
    ],
    outputs=[gr.Image(type="pil", label="Generated Image"), gr.Image(type="pil", label="Refined Image")],
    title="Genrate Image Using Generative AI",
    theme=gr.themes.Default(primary_hue="green"),
    description="Text-to-Image or Image-to-Image Generation with SDXL-Turbo."
)

# Launch the interface
interface.launch()