Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,19 +3,27 @@ import argparse
|
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
from os import path
|
|
|
|
|
|
|
| 6 |
from safetensors.torch import load_file
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
import gradio as gr
|
| 9 |
import torch
|
| 10 |
from diffusers import FluxPipeline
|
|
|
|
| 11 |
|
| 12 |
# Setup and initialization code
|
| 13 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
|
|
|
| 14 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
| 15 |
os.environ["HF_HUB_CACHE"] = cache_path
|
| 16 |
os.environ["HF_HOME"] = cache_path
|
| 17 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
class timer:
|
| 20 |
def __init__(self, method_name="timed process"):
|
| 21 |
self.method = method_name
|
|
@@ -58,11 +66,50 @@ footer {display: none !important}
|
|
| 58 |
-webkit-background-clip: text;
|
| 59 |
-webkit-text-fill-color: transparent;
|
| 60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
"""
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# Create Gradio interface
|
| 64 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 65 |
-
gr.HTML('<div class="title">
|
| 66 |
gr.HTML('<div style="text-align: center; margin-bottom: 2em; color: #666;">Create stunning images from your descriptions</div>')
|
| 67 |
|
| 68 |
with gr.Row():
|
|
@@ -140,24 +187,32 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 140 |
<p style="font-weight: bold; margin: 0 0 0.5em 0;">🌿 Macro Nature</p>
|
| 141 |
<p style="margin: 0; font-style: italic;">"Extreme macro photography of a dewdrop on a butterfly wing, rainbow light refraction, crystalline clarity, intricate wing scales visible, natural bokeh background, professional studio lighting, shot with Canon MP-E 65mm lens"</p>
|
| 142 |
</div>
|
| 143 |
-
<h4 style="margin: 1em 0 0.5em 0;">Tips for best results:</h4>
|
| 144 |
-
<ul style="margin: 0; padding-left: 1.2em;">
|
| 145 |
-
<li>Be specific in your descriptions</li>
|
| 146 |
-
<li>Include details about style, lighting, and mood</li>
|
| 147 |
-
<li>Reference specific artists or techniques</li>
|
| 148 |
-
<li>Experiment with different guidance scales</li>
|
| 149 |
-
</ul>
|
| 150 |
</div>
|
| 151 |
""")
|
| 152 |
|
| 153 |
with gr.Column(scale=4):
|
|
|
|
| 154 |
output = gr.Image(label="Generated Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
@spaces.GPU
|
| 157 |
-
def
|
| 158 |
global pipe
|
| 159 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
| 160 |
-
|
| 161 |
prompt=[prompt],
|
| 162 |
generator=torch.Generator().manual_seed(int(seed)),
|
| 163 |
num_inference_steps=int(steps),
|
|
@@ -166,11 +221,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 166 |
width=int(width),
|
| 167 |
max_sequence_length=256
|
| 168 |
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
|
|
|
| 170 |
generate_btn.click(
|
| 171 |
-
|
| 172 |
inputs=[height, width, steps, scales, prompt, seed],
|
| 173 |
-
outputs=output
|
| 174 |
)
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
from os import path
|
| 6 |
+
import shutil
|
| 7 |
+
from datetime import datetime
|
| 8 |
from safetensors.torch import load_file
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
import gradio as gr
|
| 11 |
import torch
|
| 12 |
from diffusers import FluxPipeline
|
| 13 |
+
from PIL import Image
|
| 14 |
|
| 15 |
# Setup and initialization code
|
| 16 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
| 17 |
+
gallery_path = path.join(path.dirname(path.abspath(__file__)), "gallery")
|
| 18 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
| 19 |
os.environ["HF_HUB_CACHE"] = cache_path
|
| 20 |
os.environ["HF_HOME"] = cache_path
|
| 21 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 22 |
|
| 23 |
+
# Create gallery directory if it doesn't exist
|
| 24 |
+
if not path.exists(gallery_path):
|
| 25 |
+
os.makedirs(gallery_path, exist_ok=True)
|
| 26 |
+
|
| 27 |
class timer:
|
| 28 |
def __init__(self, method_name="timed process"):
|
| 29 |
self.method = method_name
|
|
|
|
| 66 |
-webkit-background-clip: text;
|
| 67 |
-webkit-text-fill-color: transparent;
|
| 68 |
}
|
| 69 |
+
.gallery-container {
|
| 70 |
+
display: grid;
|
| 71 |
+
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
|
| 72 |
+
gap: 10px;
|
| 73 |
+
padding: 10px;
|
| 74 |
+
background: rgba(255, 255, 255, 0.05);
|
| 75 |
+
border-radius: 8px;
|
| 76 |
+
margin-top: 10px;
|
| 77 |
+
}
|
| 78 |
+
.gallery-image {
|
| 79 |
+
width: 100%;
|
| 80 |
+
aspect-ratio: 1;
|
| 81 |
+
object-fit: cover;
|
| 82 |
+
border-radius: 4px;
|
| 83 |
+
transition: transform 0.2s;
|
| 84 |
+
}
|
| 85 |
+
.gallery-image:hover {
|
| 86 |
+
transform: scale(1.05);
|
| 87 |
+
}
|
| 88 |
"""
|
| 89 |
|
| 90 |
+
def save_image(image):
|
| 91 |
+
"""Save the generated image and return the path"""
|
| 92 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 93 |
+
filename = f"generated_{timestamp}.png"
|
| 94 |
+
filepath = os.path.join(gallery_path, filename)
|
| 95 |
+
|
| 96 |
+
if isinstance(image, Image.Image):
|
| 97 |
+
image.save(filepath)
|
| 98 |
+
else:
|
| 99 |
+
image = Image.fromarray(image)
|
| 100 |
+
image.save(filepath)
|
| 101 |
+
|
| 102 |
+
return filepath
|
| 103 |
+
|
| 104 |
+
def load_gallery():
|
| 105 |
+
"""Load all images from the gallery directory"""
|
| 106 |
+
image_files = [f for f in os.listdir(gallery_path) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
| 107 |
+
image_files.sort(reverse=True) # Most recent first
|
| 108 |
+
return [os.path.join(gallery_path, f) for f in image_files]
|
| 109 |
+
|
| 110 |
# Create Gradio interface
|
| 111 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 112 |
+
gr.HTML('<div class="title">AI Image Generator</div>')
|
| 113 |
gr.HTML('<div style="text-align: center; margin-bottom: 2em; color: #666;">Create stunning images from your descriptions</div>')
|
| 114 |
|
| 115 |
with gr.Row():
|
|
|
|
| 187 |
<p style="font-weight: bold; margin: 0 0 0.5em 0;">🌿 Macro Nature</p>
|
| 188 |
<p style="margin: 0; font-style: italic;">"Extreme macro photography of a dewdrop on a butterfly wing, rainbow light refraction, crystalline clarity, intricate wing scales visible, natural bokeh background, professional studio lighting, shot with Canon MP-E 65mm lens"</p>
|
| 189 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
</div>
|
| 191 |
""")
|
| 192 |
|
| 193 |
with gr.Column(scale=4):
|
| 194 |
+
# Current generated image
|
| 195 |
output = gr.Image(label="Generated Image")
|
| 196 |
+
|
| 197 |
+
# Gallery of generated images
|
| 198 |
+
gallery = gr.Gallery(
|
| 199 |
+
label="Generated Images Gallery",
|
| 200 |
+
show_label=True,
|
| 201 |
+
elem_id="gallery",
|
| 202 |
+
columns=[4],
|
| 203 |
+
rows=[2],
|
| 204 |
+
height="auto",
|
| 205 |
+
object_fit="contain"
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
# Load existing gallery images on startup
|
| 209 |
+
gallery.value = load_gallery()
|
| 210 |
|
| 211 |
@spaces.GPU
|
| 212 |
+
def process_and_save_image(height, width, steps, scales, prompt, seed):
|
| 213 |
global pipe
|
| 214 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
| 215 |
+
generated_image = pipe(
|
| 216 |
prompt=[prompt],
|
| 217 |
generator=torch.Generator().manual_seed(int(seed)),
|
| 218 |
num_inference_steps=int(steps),
|
|
|
|
| 221 |
width=int(width),
|
| 222 |
max_sequence_length=256
|
| 223 |
).images[0]
|
| 224 |
+
|
| 225 |
+
# Save the generated image
|
| 226 |
+
save_image(generated_image)
|
| 227 |
+
|
| 228 |
+
# Return both the generated image and updated gallery
|
| 229 |
+
return generated_image, load_gallery()
|
| 230 |
|
| 231 |
+
# Connect the generation button to both the image output and gallery update
|
| 232 |
generate_btn.click(
|
| 233 |
+
process_and_save_image,
|
| 234 |
inputs=[height, width, steps, scales, prompt, seed],
|
| 235 |
+
outputs=[output, gallery]
|
| 236 |
)
|
| 237 |
|
| 238 |
if __name__ == "__main__":
|