Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,226 @@
|
|
1 |
-
import
|
2 |
import os
|
3 |
-
import
|
4 |
-
import
|
5 |
import gradio as gr
|
6 |
-
|
|
|
|
|
|
|
7 |
from PIL import Image
|
8 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
|
9 |
-
|
10 |
-
# Model and processor initialization
|
11 |
-
model_name = "Qwen/QVQ-72B-Preview"
|
12 |
-
|
13 |
-
model = AutoModelForCausalLM.from_pretrained(
|
14 |
-
model_name,
|
15 |
-
trust_remote_code=True,
|
16 |
-
device_map="auto",
|
17 |
-
torch_dtype=torch.float16
|
18 |
-
)
|
19 |
-
|
20 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
21 |
-
model_name,
|
22 |
-
trust_remote_code=True
|
23 |
-
)
|
24 |
-
|
25 |
-
processor = AutoProcessor.from_pretrained(
|
26 |
-
model_name,
|
27 |
-
trust_remote_code=True
|
28 |
-
)
|
29 |
-
|
30 |
-
# Footer
|
31 |
-
footer = """
|
32 |
-
<div style="text-align: center; margin-top: 20px;">
|
33 |
-
<p>Powered by QVQ-72B Model</p>
|
34 |
-
</div>
|
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 |
css = """
|
72 |
footer {
|
73 |
visibility: hidden;
|
74 |
}
|
75 |
"""
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
with gr.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
demo.launch(
|
|
|
1 |
+
import random
|
2 |
import os
|
3 |
+
import uuid
|
4 |
+
from datetime import datetime
|
5 |
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import spaces
|
8 |
+
import torch
|
9 |
+
from diffusers import DiffusionPipeline
|
10 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Create permanent storage directory
|
13 |
+
SAVE_DIR = "saved_images" # Gradio will handle the persistence
|
14 |
+
if not os.path.exists(SAVE_DIR):
|
15 |
+
os.makedirs(SAVE_DIR, exist_ok=True)
|
16 |
+
|
17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
+
repo_id = "black-forest-labs/FLUX.1-dev"
|
19 |
+
adapter_id = "seawolf2357/flux-lora-military-artillery-k9"
|
20 |
+
|
21 |
+
pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16)
|
22 |
+
pipeline.load_lora_weights(adapter_id)
|
23 |
+
pipeline = pipeline.to(device)
|
24 |
+
|
25 |
+
MAX_SEED = np.iinfo(np.int32).max
|
26 |
+
MAX_IMAGE_SIZE = 1024
|
27 |
+
|
28 |
+
def save_generated_image(image, prompt):
|
29 |
+
# Generate unique filename with timestamp
|
30 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
31 |
+
unique_id = str(uuid.uuid4())[:8]
|
32 |
+
filename = f"{timestamp}_{unique_id}.png"
|
33 |
+
filepath = os.path.join(SAVE_DIR, filename)
|
34 |
+
|
35 |
+
# Save the image
|
36 |
+
image.save(filepath)
|
37 |
+
|
38 |
+
# Save metadata
|
39 |
+
metadata_file = os.path.join(SAVE_DIR, "metadata.txt")
|
40 |
+
with open(metadata_file, "a", encoding="utf-8") as f:
|
41 |
+
f.write(f"{filename}|{prompt}|{timestamp}\n")
|
42 |
+
|
43 |
+
return filepath
|
44 |
+
|
45 |
+
def load_generated_images():
|
46 |
+
if not os.path.exists(SAVE_DIR):
|
47 |
+
return []
|
48 |
+
|
49 |
+
# Load all images from the directory
|
50 |
+
image_files = [os.path.join(SAVE_DIR, f) for f in os.listdir(SAVE_DIR)
|
51 |
+
if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))]
|
52 |
+
# Sort by creation time (newest first)
|
53 |
+
image_files.sort(key=lambda x: os.path.getctime(x), reverse=True)
|
54 |
+
return image_files
|
55 |
+
|
56 |
+
def load_predefined_images():
|
57 |
+
# Return empty list since we're not using predefined images
|
58 |
+
return []
|
59 |
+
|
60 |
+
@spaces.GPU(duration=120)
|
61 |
+
def inference(
|
62 |
+
prompt: str,
|
63 |
+
seed: int,
|
64 |
+
randomize_seed: bool,
|
65 |
+
width: int,
|
66 |
+
height: int,
|
67 |
+
guidance_scale: float,
|
68 |
+
num_inference_steps: int,
|
69 |
+
lora_scale: float,
|
70 |
+
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
71 |
+
):
|
72 |
+
if randomize_seed:
|
73 |
+
seed = random.randint(0, MAX_SEED)
|
74 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
75 |
+
|
76 |
+
image = pipeline(
|
77 |
+
prompt=prompt,
|
78 |
+
guidance_scale=guidance_scale,
|
79 |
+
num_inference_steps=num_inference_steps,
|
80 |
+
width=width,
|
81 |
+
height=height,
|
82 |
+
generator=generator,
|
83 |
+
joint_attention_kwargs={"scale": lora_scale},
|
84 |
+
).images[0]
|
85 |
+
|
86 |
+
# Save the generated image
|
87 |
+
filepath = save_generated_image(image, prompt)
|
88 |
+
|
89 |
+
# Return the image, seed, and updated gallery
|
90 |
+
return image, seed, load_generated_images()
|
91 |
+
|
92 |
+
examples = [
|
93 |
+
"Ghibli style futuristic stormtrooper with glossy white armor and a sleek helmet, standing heroically on a lush alien planet, vibrant flowers blooming around, soft sunlight illuminating the scene, a gentle breeze rustling the leaves. The armor reflects the pink and purple hues of the alien sunset, creating an ethereal glow around the figure. [trigger]",
|
94 |
+
|
95 |
+
"Ghibli style young mechanic girl in a floating workshop, surrounded by hovering tools and glowing mechanical parts, her blue overalls covered in oil stains, tinkering with a semi-transparent robot companion. Magical sparks fly as she works, while floating islands with waterfalls drift past her open workshop window. [trigger]",
|
96 |
+
|
97 |
+
"Ghibli style ancient forest guardian robot, covered in moss and flowering vines, sitting peacefully in a crystal-clear lake. Its gentle eyes glow with soft blue light, while bioluminescent dragonflies dance around its weathered metal frame. Ancient tech symbols on its surface pulse with a gentle rhythm. [trigger]",
|
98 |
+
|
99 |
+
"Ghibli style sky whale transport ship, its metallic skin adorned with traditional Japanese patterns, gliding through cotton candy clouds at sunrise. Small floating gardens hang from its sides, where workers in futuristic kimonos tend to glowing plants. Rainbow auroras shimmer in the background. [trigger]",
|
100 |
+
|
101 |
+
"Ghibli style cyber-shrine maiden with flowing holographic robes, performing a ritual dance among floating lanterns and digital cherry blossoms. Her traditional headdress emits soft light patterns, while spirit-like AI constructs swirl around her in elegant patterns. The scene is set in a modern shrine with both ancient wood and sleek chrome elements. [trigger]",
|
102 |
+
|
103 |
+
"Ghibli style robot farmer tending to floating rice paddies in the sky, wearing a traditional straw hat with advanced sensors. Its gentle movements create ripples in the water as it plants glowing rice seedlings. Flying fish leap between the terraced fields, leaving trails of sparkles in their wake, while future Tokyo's spires gleam in the distance. [trigger]"
|
104 |
+
]
|
105 |
+
|
106 |
css = """
|
107 |
footer {
|
108 |
visibility: hidden;
|
109 |
}
|
110 |
"""
|
111 |
|
112 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, analytics_enabled=False) as demo:
|
113 |
+
gr.HTML('<div class="title"> FLUX Ghibli LoRA</div>')
|
114 |
+
gr.HTML('<div class="title">😄Image to Video Explore: <a href="https://huggingface.co/spaces/ginigen/theater" target="_blank">https://huggingface.co/spaces/ginigen/theater</a></div>')
|
115 |
+
|
116 |
+
with gr.Tabs() as tabs:
|
117 |
+
with gr.Tab("Generation"):
|
118 |
+
with gr.Column(elem_id="col-container"):
|
119 |
+
with gr.Row():
|
120 |
+
prompt = gr.Text(
|
121 |
+
label="Prompt",
|
122 |
+
show_label=False,
|
123 |
+
max_lines=1,
|
124 |
+
placeholder="Enter your prompt",
|
125 |
+
container=False,
|
126 |
+
)
|
127 |
+
run_button = gr.Button("Run", scale=0)
|
128 |
+
|
129 |
+
result = gr.Image(label="Result", show_label=False)
|
130 |
+
|
131 |
+
with gr.Accordion("Advanced Settings", open=False):
|
132 |
+
seed = gr.Slider(
|
133 |
+
label="Seed",
|
134 |
+
minimum=0,
|
135 |
+
maximum=MAX_SEED,
|
136 |
+
step=1,
|
137 |
+
value=42,
|
138 |
+
)
|
139 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
140 |
+
|
141 |
+
with gr.Row():
|
142 |
+
width = gr.Slider(
|
143 |
+
label="Width",
|
144 |
+
minimum=256,
|
145 |
+
maximum=MAX_IMAGE_SIZE,
|
146 |
+
step=32,
|
147 |
+
value=1024,
|
148 |
+
)
|
149 |
+
height = gr.Slider(
|
150 |
+
label="Height",
|
151 |
+
minimum=256,
|
152 |
+
maximum=MAX_IMAGE_SIZE,
|
153 |
+
step=32,
|
154 |
+
value=768,
|
155 |
+
)
|
156 |
+
|
157 |
+
with gr.Row():
|
158 |
+
guidance_scale = gr.Slider(
|
159 |
+
label="Guidance scale",
|
160 |
+
minimum=0.0,
|
161 |
+
maximum=10.0,
|
162 |
+
step=0.1,
|
163 |
+
value=3.5,
|
164 |
+
)
|
165 |
+
num_inference_steps = gr.Slider(
|
166 |
+
label="Number of inference steps",
|
167 |
+
minimum=1,
|
168 |
+
maximum=50,
|
169 |
+
step=1,
|
170 |
+
value=30,
|
171 |
+
)
|
172 |
+
lora_scale = gr.Slider(
|
173 |
+
label="LoRA scale",
|
174 |
+
minimum=0.0,
|
175 |
+
maximum=1.0,
|
176 |
+
step=0.1,
|
177 |
+
value=1.0,
|
178 |
+
)
|
179 |
+
|
180 |
+
gr.Examples(
|
181 |
+
examples=examples,
|
182 |
+
inputs=[prompt],
|
183 |
+
outputs=[result, seed],
|
184 |
+
)
|
185 |
+
|
186 |
+
with gr.Tab("Gallery"):
|
187 |
+
gallery_header = gr.Markdown("### Generated Images Gallery")
|
188 |
+
generated_gallery = gr.Gallery(
|
189 |
+
label="Generated Images",
|
190 |
+
columns=6,
|
191 |
+
show_label=False,
|
192 |
+
value=load_generated_images(),
|
193 |
+
elem_id="generated_gallery",
|
194 |
+
height="auto"
|
195 |
+
)
|
196 |
+
refresh_btn = gr.Button("🔄 Refresh Gallery")
|
197 |
+
|
198 |
+
|
199 |
+
# Event handlers
|
200 |
+
def refresh_gallery():
|
201 |
+
return load_generated_images()
|
202 |
+
|
203 |
+
refresh_btn.click(
|
204 |
+
fn=refresh_gallery,
|
205 |
+
inputs=None,
|
206 |
+
outputs=generated_gallery,
|
207 |
+
)
|
208 |
+
|
209 |
+
gr.on(
|
210 |
+
triggers=[run_button.click, prompt.submit],
|
211 |
+
fn=inference,
|
212 |
+
inputs=[
|
213 |
+
prompt,
|
214 |
+
seed,
|
215 |
+
randomize_seed,
|
216 |
+
width,
|
217 |
+
height,
|
218 |
+
guidance_scale,
|
219 |
+
num_inference_steps,
|
220 |
+
lora_scale,
|
221 |
+
],
|
222 |
+
outputs=[result, seed, generated_gallery],
|
223 |
+
)
|
224 |
|
225 |
+
demo.queue()
|
226 |
+
demo.launch()
|