File size: 2,729 Bytes
ca59cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from transformers import CLIPTextModel, CLIPTokenizer
from diffusers import StableDiffusionPipeline

# Load the model and tokenizer
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
pipe = pipe.to("cpu")

def generate_image(prompt, negative_prompt, size):
    if not prompt:
        prompt = "a beautiful landscape"
    if not negative_prompt:
        negative_prompt = ""
        
    width, height = map(int, size.split('x'))
    generator = torch.Generator("cpu").manual_seed(42)
    
    # Generate the image
    result = pipe(prompt, height=height, width=width, negative_prompt=negative_prompt, generator=generator)
    
    if result is not None and 'images' in result:
        return result.images[0]
    else:
        return None

with gr.Blocks() as demo:
    gr.Markdown("## Text to Image SDXL")
    
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="Prompt", placeholder="Enter the prompt here...")
            negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Enter the negative prompt here...")
            size = gr.Dropdown(choices=["512x512", "768x768", "1024x1024"], value="1024x1024", label="Size")
            submit = gr.Button("Submit")
        with gr.Column():
            output = gr.Image(label="Output")

    submit.click(generate_image, inputs=[prompt, negative_prompt, size], outputs=output)

demo.launch()

# import gradio as gr
# import torch
# from transformers import CLIPTextModel, CLIPTokenizer
# from diffusers import StableDiffusionPipeline

# # Load the model and tokenizer
# model_id = "stabilityai/stable-diffusion-xl-base-1.0"
# pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
# pipe = pipe.to("cpu")

# def generate_image(prompt, negative_prompt, size):
#     width, height = map(int, size.split('x'))
#     generator = torch.Generator("cpu").manual_seed(42)
#     image = pipe(prompt, height=height, width=width, negative_prompt=negative_prompt, generator=generator).images[0]
#     return image

# with gr.Blocks() as demo:
#     gr.Markdown("## Text to Image SDXL")
    
#     with gr.Row():
#         with gr.Column():
#             prompt = gr.Textbox(label="Prompt")
#             negative_prompt = gr.Textbox(label="Negative Prompt")
#             size = gr.Dropdown(choices=["512x512", "768x768", "1024x1024"], value="1024x1024", label="Size")
#             submit = gr.Button("Submit")
#         with gr.Column():
#             output = gr.Image(label="Output")

#     submit.click(generate_image, inputs=[prompt, negative_prompt, size], outputs=output)

# demo.launch()