File size: 4,688 Bytes
7cb8b0c
 
 
 
 
 
a534797
 
 
 
 
 
 
 
7cb8b0c
a534797
 
 
 
 
 
 
1f2775d
7cb8b0c
a534797
 
 
 
 
 
7cb8b0c
 
a534797
1f2775d
7cb8b0c
 
 
1f2775d
7cb8b0c
 
 
 
 
 
 
 
 
 
 
 
 
1f2775d
7cb8b0c
1f2775d
7cb8b0c
 
 
 
 
 
 
 
 
 
 
 
1f2775d
7cb8b0c
 
 
 
 
 
1f2775d
7cb8b0c
1f2775d
7cb8b0c
1f2775d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cb8b0c
 
 
1f2775d
7cb8b0c
1f2775d
 
 
 
 
 
 
 
 
7cb8b0c
1f2775d
7cb8b0c
1f2775d
 
7cb8b0c
1f2775d
7cb8b0c
1f2775d
 
7cb8b0c
1f2775d
 
 
 
 
7cb8b0c
1f2775d
 
7cb8b0c
 
 
1f2775d
7cb8b0c
 
1f2775d
 
7cb8b0c
 
1f2775d
7cb8b0c
 
1f2775d
 
7cb8b0c
 
 
 
1f2775d
7cb8b0c
1f2775d
7cb8b0c
1f2775d
7cb8b0c
 
1f2775d
 
 
 
 
7cb8b0c
 
1f2775d
 
 
 
 
 
 
 
 
 
7cb8b0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import gradio as gr
import numpy as np
import random
from diffusers import DiffusionPipeline
import torch

from diffusers import DiffusionPipeline
import torch
from huggingface_hub import login

import yaml

with open("config.yaml", "r") as f:
    config = yaml.safe_load(f)

token = config.get("huggingface_token")

# Login to Hugging Face Hub
login(token)

# Model details
device = "cuda" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

# Load model
pipe = DiffusionPipeline.from_pretrained(
    "Grandediw/lora_model",
    torch_dtype=torch_dtype,
    use_auth_token=True  # Enables private model access
)
pipe = pipe.to(device)


# Constants
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024

# Inference function
def infer(
    prompt,
    negative_prompt,
    seed,
    randomize_seed,
    width,
    height,
    guidance_scale,
    num_inference_steps,
):
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)

    generator = torch.Generator(device).manual_seed(seed)

    # Generate the image
    image = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        guidance_scale=guidance_scale,
        num_inference_steps=num_inference_steps,
        width=width,
        height=height,
        generator=generator,
    ).images[0]

    return image, seed

# Example prompts
examples = [
    "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
    "An astronaut riding a green horse",
    "A delicious ceviche cheesecake slice",
]

# Improved CSS for better styling
css = """
#interface-container {
    margin: 0 auto;
    max-width: 700px;
    padding: 10px;
    box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    background-color: #f9f9f9;
}
#header {
    text-align: center;
    font-size: 1.5em;
    margin-bottom: 20px;
    color: #333;
}
#advanced-settings {
    background-color: #f1f1f1;
    padding: 10px;
    border-radius: 8px;
}
"""

# Gradio interface
with gr.Blocks(css=css) as demo:
    with gr.Box(elem_id="interface-container"):
        gr.Markdown(
            """
            <div id="header">🖼️ Text-to-Image Generator</div>
            Generate high-quality images from your text prompts with the fine-tuned LoRA model.
            """
        )

        # Main input row
        with gr.Row():
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Describe the image you want to create...",
                lines=2,
            )
            run_button = gr.Button("Generate Image", variant="primary")

        # Output image display
        result = gr.Image(label="Generated Image").style(height="512px")

        # Advanced settings
        with gr.Accordion("Advanced Settings", open=False, elem_id="advanced-settings"):
            negative_prompt = gr.Textbox(
                label="Negative Prompt",
                placeholder="What to exclude from the image...",
            )
            randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
            seed = gr.Number(label="Seed", value=0, interactive=True)

            with gr.Row():
                width = gr.Slider(
                    label="Image Width",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=64,
                    value=512,
                )
                height = gr.Slider(
                    label="Image Height",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=64,
                    value=512,
                )

            with gr.Row():
                guidance_scale = gr.Slider(
                    label="Guidance Scale",
                    minimum=0.0,
                    maximum=20.0,
                    step=0.1,
                    value=7.5,
                )
                num_inference_steps = gr.Slider(
                    label="Steps",
                    minimum=10,
                    maximum=100,
                    step=5,
                    value=50,
                )

        # Examples
        gr.Examples(
            examples=examples,
            inputs=[prompt],
            outputs=[result],
            label="Try these prompts",
        )

    # Event handler
    run_button.click(
        fn=infer,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, seed],
    )

if __name__ == "__main__":
    demo.launch()