Update app.py
Browse filesUpdate Interface
app.py
CHANGED
@@ -1,27 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
-
|
5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
6 |
from diffusers import DiffusionPipeline
|
7 |
import torch
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
model_repo_id = "
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
else:
|
15 |
-
torch_dtype = torch.float32
|
16 |
|
|
|
17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
18 |
pipe = pipe.to(device)
|
19 |
|
|
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
-
|
24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
25 |
def infer(
|
26 |
prompt,
|
27 |
negative_prompt,
|
@@ -31,13 +28,13 @@ def infer(
|
|
31 |
height,
|
32 |
guidance_scale,
|
33 |
num_inference_steps,
|
34 |
-
progress=gr.Progress(track_tqdm=True),
|
35 |
):
|
36 |
if randomize_seed:
|
37 |
seed = random.randint(0, MAX_SEED)
|
38 |
|
39 |
-
generator = torch.Generator().manual_seed(seed)
|
40 |
|
|
|
41 |
image = pipe(
|
42 |
prompt=prompt,
|
43 |
negative_prompt=negative_prompt,
|
@@ -50,92 +47,109 @@ def infer(
|
|
50 |
|
51 |
return image, seed
|
52 |
|
53 |
-
|
54 |
examples = [
|
55 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
56 |
"An astronaut riding a green horse",
|
57 |
"A delicious ceviche cheesecake slice",
|
58 |
]
|
59 |
|
|
|
60 |
css = """
|
61 |
-
#
|
62 |
margin: 0 auto;
|
63 |
-
max-width:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
}
|
65 |
"""
|
66 |
|
|
|
67 |
with gr.Blocks(css=css) as demo:
|
68 |
-
with gr.
|
69 |
-
gr.Markdown(
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
with gr.Row():
|
72 |
-
prompt = gr.
|
73 |
label="Prompt",
|
74 |
-
|
75 |
-
|
76 |
-
placeholder="Enter your prompt",
|
77 |
-
container=False,
|
78 |
)
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
result = gr.Image(label="Result", show_label=False)
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
placeholder="
|
89 |
-
visible=False,
|
90 |
)
|
91 |
-
|
92 |
-
seed = gr.
|
93 |
-
label="Seed",
|
94 |
-
minimum=0,
|
95 |
-
maximum=MAX_SEED,
|
96 |
-
step=1,
|
97 |
-
value=0,
|
98 |
-
)
|
99 |
-
|
100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
101 |
|
102 |
with gr.Row():
|
103 |
width = gr.Slider(
|
104 |
-
label="Width",
|
105 |
minimum=256,
|
106 |
maximum=MAX_IMAGE_SIZE,
|
107 |
-
step=
|
108 |
-
value=
|
109 |
)
|
110 |
-
|
111 |
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
minimum=256,
|
114 |
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=
|
116 |
-
value=
|
117 |
)
|
118 |
|
119 |
with gr.Row():
|
120 |
guidance_scale = gr.Slider(
|
121 |
-
label="Guidance
|
122 |
minimum=0.0,
|
123 |
-
maximum=
|
124 |
step=0.1,
|
125 |
-
value=
|
126 |
)
|
127 |
-
|
128 |
num_inference_steps = gr.Slider(
|
129 |
-
label="
|
130 |
-
minimum=
|
131 |
-
maximum=
|
132 |
-
step=
|
133 |
-
value=
|
134 |
)
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
fn=infer,
|
140 |
inputs=[
|
141 |
prompt,
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
|
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
model_repo_id = "Grandediw/lora_model" # Use the fine-tuned model
|
9 |
|
10 |
+
# Adjust torch data type based on device
|
11 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
12 |
|
13 |
+
# Load the model pipeline
|
14 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
15 |
pipe = pipe.to(device)
|
16 |
|
17 |
+
# Constants
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
MAX_IMAGE_SIZE = 1024
|
20 |
|
21 |
+
# Inference function
|
|
|
22 |
def infer(
|
23 |
prompt,
|
24 |
negative_prompt,
|
|
|
28 |
height,
|
29 |
guidance_scale,
|
30 |
num_inference_steps,
|
|
|
31 |
):
|
32 |
if randomize_seed:
|
33 |
seed = random.randint(0, MAX_SEED)
|
34 |
|
35 |
+
generator = torch.Generator(device).manual_seed(seed)
|
36 |
|
37 |
+
# Generate the image
|
38 |
image = pipe(
|
39 |
prompt=prompt,
|
40 |
negative_prompt=negative_prompt,
|
|
|
47 |
|
48 |
return image, seed
|
49 |
|
50 |
+
# Example prompts
|
51 |
examples = [
|
52 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
53 |
"An astronaut riding a green horse",
|
54 |
"A delicious ceviche cheesecake slice",
|
55 |
]
|
56 |
|
57 |
+
# Improved CSS for better styling
|
58 |
css = """
|
59 |
+
#interface-container {
|
60 |
margin: 0 auto;
|
61 |
+
max-width: 700px;
|
62 |
+
padding: 10px;
|
63 |
+
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
|
64 |
+
border-radius: 10px;
|
65 |
+
background-color: #f9f9f9;
|
66 |
+
}
|
67 |
+
#header {
|
68 |
+
text-align: center;
|
69 |
+
font-size: 1.5em;
|
70 |
+
margin-bottom: 20px;
|
71 |
+
color: #333;
|
72 |
+
}
|
73 |
+
#advanced-settings {
|
74 |
+
background-color: #f1f1f1;
|
75 |
+
padding: 10px;
|
76 |
+
border-radius: 8px;
|
77 |
}
|
78 |
"""
|
79 |
|
80 |
+
# Gradio interface
|
81 |
with gr.Blocks(css=css) as demo:
|
82 |
+
with gr.Box(elem_id="interface-container"):
|
83 |
+
gr.Markdown(
|
84 |
+
"""
|
85 |
+
<div id="header">🖼️ Text-to-Image Generator</div>
|
86 |
+
Generate high-quality images from your text prompts with the fine-tuned LoRA model.
|
87 |
+
"""
|
88 |
+
)
|
89 |
+
|
90 |
+
# Main input row
|
91 |
with gr.Row():
|
92 |
+
prompt = gr.Textbox(
|
93 |
label="Prompt",
|
94 |
+
placeholder="Describe the image you want to create...",
|
95 |
+
lines=2,
|
|
|
|
|
96 |
)
|
97 |
+
run_button = gr.Button("Generate Image", variant="primary")
|
98 |
|
99 |
+
# Output image display
|
100 |
+
result = gr.Image(label="Generated Image").style(height="512px")
|
|
|
101 |
|
102 |
+
# Advanced settings
|
103 |
+
with gr.Accordion("Advanced Settings", open=False, elem_id="advanced-settings"):
|
104 |
+
negative_prompt = gr.Textbox(
|
105 |
+
label="Negative Prompt",
|
106 |
+
placeholder="What to exclude from the image...",
|
|
|
107 |
)
|
108 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
109 |
+
seed = gr.Number(label="Seed", value=0, interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
with gr.Row():
|
112 |
width = gr.Slider(
|
113 |
+
label="Image Width",
|
114 |
minimum=256,
|
115 |
maximum=MAX_IMAGE_SIZE,
|
116 |
+
step=64,
|
117 |
+
value=512,
|
118 |
)
|
|
|
119 |
height = gr.Slider(
|
120 |
+
label="Image Height",
|
121 |
minimum=256,
|
122 |
maximum=MAX_IMAGE_SIZE,
|
123 |
+
step=64,
|
124 |
+
value=512,
|
125 |
)
|
126 |
|
127 |
with gr.Row():
|
128 |
guidance_scale = gr.Slider(
|
129 |
+
label="Guidance Scale",
|
130 |
minimum=0.0,
|
131 |
+
maximum=20.0,
|
132 |
step=0.1,
|
133 |
+
value=7.5,
|
134 |
)
|
|
|
135 |
num_inference_steps = gr.Slider(
|
136 |
+
label="Steps",
|
137 |
+
minimum=10,
|
138 |
+
maximum=100,
|
139 |
+
step=5,
|
140 |
+
value=50,
|
141 |
)
|
142 |
|
143 |
+
# Examples
|
144 |
+
gr.Examples(
|
145 |
+
examples=examples,
|
146 |
+
inputs=[prompt],
|
147 |
+
outputs=[result],
|
148 |
+
label="Try these prompts",
|
149 |
+
)
|
150 |
+
|
151 |
+
# Event handler
|
152 |
+
run_button.click(
|
153 |
fn=infer,
|
154 |
inputs=[
|
155 |
prompt,
|