Spaces:
Running
Running
Upload 15 files
Browse files- README.md +8 -7
- app.py +175 -0
- app_backup.py +165 -0
- custom_pipeline.py +168 -0
- gradio_cached_examples/.DS_Store +0 -0
- gradio_cached_examples/25/Hasil Gambar/357e0c8d8a0e75b63595/image.webp +0 -0
- gradio_cached_examples/25/Hasil Gambar/727c385c02cbde1f1576/image.webp +0 -0
- gradio_cached_examples/25/Hasil Gambar/827754ef7725b469cc3b/image.webp +0 -0
- gradio_cached_examples/25/indices.csv +3 -0
- gradio_cached_examples/25/log.csv +4 -0
- gradio_cached_examples/29/Generated Image/c9de53a0541e2186d948/image.webp +0 -0
- gradio_cached_examples/29/indices.csv +1 -0
- gradio_cached_examples/29/log.csv +2 -0
- requirements.txt +7 -0
- themes.py +55 -0
README.md
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
---
|
2 |
-
title: FLUX
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.44.0
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
10 |
-
license:
|
|
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: FLUX Realtime
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.44.0
|
8 |
app_file: app.py
|
9 |
+
pinned: true
|
10 |
+
license: mit
|
11 |
+
short_description: High quality Images in Realtime
|
12 |
---
|
13 |
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
from gradio_client import Client
|
4 |
+
import os
|
5 |
+
from themes import IndonesiaTheme # Impor tema custom dari themes.py
|
6 |
+
|
7 |
+
# Constants
|
8 |
+
MAX_SEED = 999999
|
9 |
+
MAX_IMAGE_SIZE = 2048
|
10 |
+
DEFAULT_WIDTH = 1024
|
11 |
+
DEFAULT_HEIGHT = 1024
|
12 |
+
DEFAULT_INFERENCE_STEPS = 1
|
13 |
+
|
14 |
+
# Siapkan URL untuk permintaan API RT FLUX
|
15 |
+
# url_api = os.environ['url_api']
|
16 |
+
|
17 |
+
client = Client("KingNish/Realtime-FLUX")
|
18 |
+
# client = Client(url_api)
|
19 |
+
|
20 |
+
# Inference function using RealtimeFlux API
|
21 |
+
def generate_image(prompt, seed=42, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False, num_inference_steps=1):
|
22 |
+
result = client.predict(
|
23 |
+
prompt=prompt,
|
24 |
+
seed=seed if not randomize_seed else random.randint(0, MAX_SEED),
|
25 |
+
width=width,
|
26 |
+
height=height,
|
27 |
+
randomize_seed=randomize_seed,
|
28 |
+
num_inference_steps=num_inference_steps,
|
29 |
+
api_name="/RealtimeFlux"
|
30 |
+
)
|
31 |
+
return result[0], result[1], result[2] # Image, Seed, Latency
|
32 |
+
|
33 |
+
# Enhance function using Enhance API
|
34 |
+
def enhance_image(prompt, seed, width, height):
|
35 |
+
result = client.predict(
|
36 |
+
param_0=prompt,
|
37 |
+
param_1=seed,
|
38 |
+
param_2=width,
|
39 |
+
param_3=height,
|
40 |
+
api_name="/Enhance"
|
41 |
+
)
|
42 |
+
return result[0], result[1], result[2] # Image, Seed, Latency
|
43 |
+
|
44 |
+
# CSS untuk styling antarmuka
|
45 |
+
css = """
|
46 |
+
#col-left, #col-mid, #col-right {
|
47 |
+
margin: 0 auto;
|
48 |
+
max-width: 400px;
|
49 |
+
padding: 10px;
|
50 |
+
border-radius: 15px;
|
51 |
+
background-color: #f9f9f9;
|
52 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
53 |
+
}
|
54 |
+
|
55 |
+
#col-right {
|
56 |
+
margin: 0 auto;
|
57 |
+
max-width: 800px;
|
58 |
+
padding: 10px;
|
59 |
+
border-radius: 15px;
|
60 |
+
background-color: #f9f9f9;
|
61 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
62 |
+
}
|
63 |
+
|
64 |
+
#banner {
|
65 |
+
width: 100%;
|
66 |
+
text-align: center;
|
67 |
+
margin-bottom: 20px;
|
68 |
+
}
|
69 |
+
#run-button {
|
70 |
+
background-color: #ff4b5c;
|
71 |
+
color: white;
|
72 |
+
font-weight: bold;
|
73 |
+
padding: 10px;
|
74 |
+
border-radius: 10px;
|
75 |
+
cursor: pointer;
|
76 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
77 |
+
}
|
78 |
+
#footer {
|
79 |
+
text-align: center;
|
80 |
+
margin-top: 20px;
|
81 |
+
color: silver;
|
82 |
+
}
|
83 |
+
|
84 |
+
#whitey {
|
85 |
+
text-align: center;
|
86 |
+
margin-top: 10px;
|
87 |
+
color: white;
|
88 |
+
}
|
89 |
+
"""
|
90 |
+
|
91 |
+
# Membuat antarmuka Gradio dengan tema IndonesiaTheme
|
92 |
+
with gr.Blocks(css=css, theme=IndonesiaTheme()) as RealtimeFluxAPP:
|
93 |
+
# Tambahkan banner dan header
|
94 |
+
gr.HTML("""
|
95 |
+
<div style='text-align: center;'>
|
96 |
+
<h1>🌟 Realtime FLUX Image Generator 🌟</h1>
|
97 |
+
<p>Selamat datang! Buat gambar memukau secara realtime dengan FLUX Pipeline.</p>
|
98 |
+
<img src='https://i.ibb.co.com/M2Sd185/banner-rtf.jpg' alt='Banner' style='width: 100%; height: auto;'/>
|
99 |
+
</div>
|
100 |
+
""")
|
101 |
+
|
102 |
+
# Layout utama
|
103 |
+
with gr.Row():
|
104 |
+
with gr.Column(elem_id="col-left"):
|
105 |
+
gr.Markdown("### Deskripsi Gambar")
|
106 |
+
prompt = gr.Textbox(
|
107 |
+
label="Prompt",
|
108 |
+
placeholder="Deskripsikan gambar yang ingin Anda buat...",
|
109 |
+
lines=3,
|
110 |
+
show_label=False,
|
111 |
+
container=False,
|
112 |
+
)
|
113 |
+
generateBtn = gr.Button("🖼️ Buat Gambar", elem_id="run-button")
|
114 |
+
enhanceBtn = gr.Button("🚀 Tingkatkan Gambar", elem_id="run-button")
|
115 |
+
|
116 |
+
# Advanced Options
|
117 |
+
with gr.Accordion("Advanced Options"):
|
118 |
+
with gr.Row():
|
119 |
+
realtime = gr.Checkbox(label="Realtime Toggler", info="Gunakan lebih banyak GPU untuk realtime.")
|
120 |
+
latency = gr.Textbox(label="Latency")
|
121 |
+
with gr.Row():
|
122 |
+
seed = gr.Number(label="Seed", value=42)
|
123 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
|
124 |
+
with gr.Row():
|
125 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
|
126 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
|
127 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=4, step=1, value=DEFAULT_INFERENCE_STEPS)
|
128 |
+
|
129 |
+
# Output di sebelah kanan
|
130 |
+
with gr.Column(elem_id="col-right"):
|
131 |
+
result = gr.Image(label="Hasil Gambar", show_label=False, interactive=False)
|
132 |
+
|
133 |
+
# Example Gallery
|
134 |
+
gr.Markdown("### 🌟 Inspirasi Gallery")
|
135 |
+
gr.Examples(elem_id="whitey",
|
136 |
+
examples=[
|
137 |
+
"A beautiful sunset over the rice fields in Bali",
|
138 |
+
"A traditional Indonesian fisherman sailing in a wooden boat",
|
139 |
+
"Mount Bromo erupting at dawn with the sky full of stars",
|
140 |
+
"A street food vendor selling nasi goreng in Jakarta",
|
141 |
+
"The majestic Komodo dragon walking through the forest",
|
142 |
+
"An Indonesian traditional dancer performing in a colorful costume",
|
143 |
+
"A futuristic cityscape of Jakarta with skyscrapers and advanced technology",
|
144 |
+
],
|
145 |
+
fn=generate_image,
|
146 |
+
inputs=[prompt],
|
147 |
+
outputs=[result, seed, latency],
|
148 |
+
cache_examples="lazy"
|
149 |
+
)
|
150 |
+
|
151 |
+
# Tombol untuk memulai proses pembuatan dan peningkatan gambar
|
152 |
+
generateBtn.click(
|
153 |
+
fn=generate_image,
|
154 |
+
inputs=[prompt, seed, width, height, randomize_seed, num_inference_steps],
|
155 |
+
outputs=[result, seed, latency],
|
156 |
+
show_progress=True
|
157 |
+
)
|
158 |
+
|
159 |
+
enhanceBtn.click(
|
160 |
+
fn=enhance_image,
|
161 |
+
inputs=[prompt, seed, width, height],
|
162 |
+
outputs=[result, seed, latency],
|
163 |
+
show_progress=True
|
164 |
+
)
|
165 |
+
|
166 |
+
# Tambahkan footer di bagian bawah
|
167 |
+
gr.HTML("""
|
168 |
+
<footer id="footer">
|
169 |
+
<p>Transfer Energi Semesta Digital © 2024 __drat. | 🇮🇩 Untuk Indonesia Jaya!</p>
|
170 |
+
</footer>
|
171 |
+
""")
|
172 |
+
|
173 |
+
# Menjalankan aplikasi
|
174 |
+
if __name__ == "__main__":
|
175 |
+
RealtimeFluxAPP.queue(api_open=False).launch(show_api=False)
|
app_backup.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import spaces
|
5 |
+
import torch
|
6 |
+
import time
|
7 |
+
from diffusers import DiffusionPipeline
|
8 |
+
from custom_pipeline import FLUXPipelineWithIntermediateOutputs
|
9 |
+
|
10 |
+
# Constants
|
11 |
+
MAX_SEED = np.iinfo(np.int32).max
|
12 |
+
MAX_IMAGE_SIZE = 2048
|
13 |
+
DEFAULT_WIDTH = 1024
|
14 |
+
DEFAULT_HEIGHT = 1024
|
15 |
+
DEFAULT_INFERENCE_STEPS = 1
|
16 |
+
|
17 |
+
# Device and model setup
|
18 |
+
dtype = torch.float16
|
19 |
+
pipe = FLUXPipelineWithIntermediateOutputs.from_pretrained(
|
20 |
+
"black-forest-labs/FLUX.1-schnell", torch_dtype=dtype
|
21 |
+
).to("cuda")
|
22 |
+
torch.cuda.empty_cache()
|
23 |
+
|
24 |
+
# Inference function
|
25 |
+
@spaces.GPU(duration=25)
|
26 |
+
def generate_image(prompt, seed=42, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False, num_inference_steps=2, progress=gr.Progress(track_tqdm=True)):
|
27 |
+
if randomize_seed:
|
28 |
+
seed = random.randint(0, MAX_SEED)
|
29 |
+
generator = torch.Generator().manual_seed(int(float(seed)))
|
30 |
+
|
31 |
+
start_time = time.time()
|
32 |
+
|
33 |
+
# Only generate the last image in the sequence
|
34 |
+
for img in pipe.generate_images(
|
35 |
+
prompt=prompt,
|
36 |
+
guidance_scale=0, # as Flux schnell is guidance free
|
37 |
+
num_inference_steps=num_inference_steps,
|
38 |
+
width=width,
|
39 |
+
height=height,
|
40 |
+
generator=generator
|
41 |
+
):
|
42 |
+
latency = f"Latency: {(time.time()-start_time):.2f} seconds"
|
43 |
+
yield img, seed, latency
|
44 |
+
|
45 |
+
# Example prompts
|
46 |
+
examples = [
|
47 |
+
"a tiny astronaut hatching from an egg on the moon",
|
48 |
+
"a cute white cat holding a sign that says hello world",
|
49 |
+
"an anime illustration of a wiener schnitzel",
|
50 |
+
"Create mage of Modern house in minecraft style",
|
51 |
+
"Imagine steve jobs as Star Wars movie character",
|
52 |
+
"Lion",
|
53 |
+
"Photo of a young woman with long, wavy brown hair tied in a bun and glasses. She has a fair complexion and is wearing subtle makeup, emphasizing her eyes and lips. She is dressed in a black top. The background appears to be an urban setting with a building facade, and the sunlight casts a warm glow on her face.",
|
54 |
+
]
|
55 |
+
|
56 |
+
# --- Gradio UI ---
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
with gr.Column(elem_id="app-container"):
|
59 |
+
gr.Markdown("# 🎨 Realtime FLUX Image Generator")
|
60 |
+
gr.Markdown("Generate stunning images in real-time with Modified Flux.Schnell pipeline.")
|
61 |
+
gr.Markdown("<span style='color: red;'>Note: Sometimes it stucks or stops generating images (I don't know why). In that situation just refresh the site.</span>")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
with gr.Column(scale=2.5):
|
65 |
+
result = gr.Image(label="Generated Image", show_label=False, interactive=False)
|
66 |
+
with gr.Column(scale=1):
|
67 |
+
prompt = gr.Text(
|
68 |
+
label="Prompt",
|
69 |
+
placeholder="Describe the image you want to generate...",
|
70 |
+
lines=3,
|
71 |
+
show_label=False,
|
72 |
+
container=False,
|
73 |
+
)
|
74 |
+
generateBtn = gr.Button("🖼️ Generate Image")
|
75 |
+
enhanceBtn = gr.Button("🚀 Enhance Image")
|
76 |
+
|
77 |
+
with gr.Column("Advanced Options"):
|
78 |
+
with gr.Row():
|
79 |
+
realtime = gr.Checkbox(label="Realtime Toggler", info="If TRUE then uses more GPU but create image in realtime.", value=False)
|
80 |
+
latency = gr.Text(label="Latency")
|
81 |
+
with gr.Row():
|
82 |
+
seed = gr.Number(label="Seed", value=42)
|
83 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
|
84 |
+
with gr.Row():
|
85 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
|
86 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
|
87 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=4, step=1, value=DEFAULT_INFERENCE_STEPS)
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
gr.Markdown("### 🌟 Inspiration Gallery")
|
91 |
+
with gr.Row():
|
92 |
+
gr.Examples(
|
93 |
+
examples=examples,
|
94 |
+
fn=generate_image,
|
95 |
+
inputs=[prompt],
|
96 |
+
outputs=[result, seed, latency],
|
97 |
+
cache_examples="lazy"
|
98 |
+
)
|
99 |
+
|
100 |
+
def enhance_image(*args):
|
101 |
+
gr.Info("Enhancing Image") # currently just runs optimized pipeline for 2 steps. Further implementations later.
|
102 |
+
return next(generate_image(*args))
|
103 |
+
|
104 |
+
enhanceBtn.click(
|
105 |
+
fn=enhance_image,
|
106 |
+
inputs=[prompt, seed, width, height],
|
107 |
+
outputs=[result, seed, latency],
|
108 |
+
show_progress="hidden",
|
109 |
+
api_name="Enhance",
|
110 |
+
queue=False,
|
111 |
+
concurrency_limit=None
|
112 |
+
)
|
113 |
+
|
114 |
+
generateBtn.click(
|
115 |
+
fn=generate_image,
|
116 |
+
inputs=[prompt, seed, width, height, randomize_seed, num_inference_steps],
|
117 |
+
outputs=[result, seed, latency],
|
118 |
+
show_progress="full",
|
119 |
+
api_name="RealtimeFlux",
|
120 |
+
queue=False,
|
121 |
+
concurrency_limit=None
|
122 |
+
)
|
123 |
+
|
124 |
+
def update_ui(realtime_enabled):
|
125 |
+
return {
|
126 |
+
prompt: gr.update(interactive=True),
|
127 |
+
generateBtn: gr.update(visible=not realtime_enabled)
|
128 |
+
}
|
129 |
+
|
130 |
+
realtime.change(
|
131 |
+
fn=update_ui,
|
132 |
+
inputs=[realtime],
|
133 |
+
outputs=[prompt, generateBtn],
|
134 |
+
queue=False,
|
135 |
+
concurrency_limit=None
|
136 |
+
)
|
137 |
+
|
138 |
+
def realtime_generation(*args):
|
139 |
+
if args[0]: # If realtime is enabled
|
140 |
+
return next(generate_image(*args[1:]))
|
141 |
+
|
142 |
+
prompt.submit(
|
143 |
+
fn=generate_image,
|
144 |
+
inputs=[prompt, seed, width, height, randomize_seed, num_inference_steps],
|
145 |
+
outputs=[result, seed, latency],
|
146 |
+
show_progress="full",
|
147 |
+
api_name=False,
|
148 |
+
queue=False,
|
149 |
+
concurrency_limit=None
|
150 |
+
)
|
151 |
+
|
152 |
+
for component in [prompt, width, height, num_inference_steps]:
|
153 |
+
component.input(
|
154 |
+
fn=realtime_generation,
|
155 |
+
inputs=[realtime, prompt, seed, width, height, randomize_seed, num_inference_steps],
|
156 |
+
outputs=[result, seed, latency],
|
157 |
+
show_progress="hidden",
|
158 |
+
api_name=False,
|
159 |
+
trigger_mode="always_last",
|
160 |
+
queue=False,
|
161 |
+
concurrency_limit=None
|
162 |
+
)
|
163 |
+
|
164 |
+
# Launch the app
|
165 |
+
demo.launch()
|
custom_pipeline.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import numpy as np
|
3 |
+
from diffusers import FluxPipeline, FlowMatchEulerDiscreteScheduler
|
4 |
+
from typing import Any, Dict, List, Optional, Union
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Constants for shift calculation
|
8 |
+
BASE_SEQ_LEN = 256
|
9 |
+
MAX_SEQ_LEN = 4096
|
10 |
+
BASE_SHIFT = 0.5
|
11 |
+
MAX_SHIFT = 1.2
|
12 |
+
|
13 |
+
# Helper functions
|
14 |
+
def calculate_timestep_shift(image_seq_len: int) -> float:
|
15 |
+
"""Calculates the timestep shift (mu) based on the image sequence length."""
|
16 |
+
m = (MAX_SHIFT - BASE_SHIFT) / (MAX_SEQ_LEN - BASE_SEQ_LEN)
|
17 |
+
b = BASE_SHIFT - m * BASE_SEQ_LEN
|
18 |
+
mu = image_seq_len * m + b
|
19 |
+
return mu
|
20 |
+
|
21 |
+
def prepare_timesteps(
|
22 |
+
scheduler: FlowMatchEulerDiscreteScheduler,
|
23 |
+
num_inference_steps: Optional[int] = None,
|
24 |
+
device: Optional[Union[str, torch.device]] = None,
|
25 |
+
timesteps: Optional[List[int]] = None,
|
26 |
+
sigmas: Optional[List[float]] = None,
|
27 |
+
mu: Optional[float] = None,
|
28 |
+
) -> (torch.Tensor, int):
|
29 |
+
"""Prepares the timesteps for the diffusion process."""
|
30 |
+
if timesteps is not None and sigmas is not None:
|
31 |
+
raise ValueError("Only one of `timesteps` or `sigmas` can be passed.")
|
32 |
+
|
33 |
+
if timesteps is not None:
|
34 |
+
scheduler.set_timesteps(timesteps=timesteps, device=device)
|
35 |
+
elif sigmas is not None:
|
36 |
+
scheduler.set_timesteps(sigmas=sigmas, device=device)
|
37 |
+
else:
|
38 |
+
scheduler.set_timesteps(num_inference_steps, device=device, mu=mu)
|
39 |
+
|
40 |
+
timesteps = scheduler.timesteps
|
41 |
+
num_inference_steps = len(timesteps)
|
42 |
+
return timesteps, num_inference_steps
|
43 |
+
|
44 |
+
# FLUX pipeline function
|
45 |
+
class FLUXPipelineWithIntermediateOutputs(FluxPipeline):
|
46 |
+
"""
|
47 |
+
Extends the FluxPipeline to yield intermediate images during the denoising process
|
48 |
+
with progressively increasing resolution for faster generation.
|
49 |
+
"""
|
50 |
+
@torch.inference_mode()
|
51 |
+
def generate_images(
|
52 |
+
self,
|
53 |
+
prompt: Union[str, List[str]] = None,
|
54 |
+
prompt_2: Optional[Union[str, List[str]]] = None,
|
55 |
+
height: Optional[int] = None,
|
56 |
+
width: Optional[int] = None,
|
57 |
+
num_inference_steps: int = 4,
|
58 |
+
timesteps: List[int] = None,
|
59 |
+
guidance_scale: float = 3.5,
|
60 |
+
num_images_per_prompt: Optional[int] = 1,
|
61 |
+
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
|
62 |
+
latents: Optional[torch.FloatTensor] = None,
|
63 |
+
prompt_embeds: Optional[torch.FloatTensor] = None,
|
64 |
+
pooled_prompt_embeds: Optional[torch.FloatTensor] = None,
|
65 |
+
output_type: Optional[str] = "pil",
|
66 |
+
return_dict: bool = True,
|
67 |
+
joint_attention_kwargs: Optional[Dict[str, Any]] = None,
|
68 |
+
max_sequence_length: int = 300,
|
69 |
+
):
|
70 |
+
"""Generates images and yields intermediate results during the denoising process."""
|
71 |
+
height = height or self.default_sample_size * self.vae_scale_factor
|
72 |
+
width = width or self.default_sample_size * self.vae_scale_factor
|
73 |
+
|
74 |
+
# 1. Check inputs
|
75 |
+
self.check_inputs(
|
76 |
+
prompt,
|
77 |
+
prompt_2,
|
78 |
+
height,
|
79 |
+
width,
|
80 |
+
prompt_embeds=prompt_embeds,
|
81 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
82 |
+
max_sequence_length=max_sequence_length,
|
83 |
+
)
|
84 |
+
|
85 |
+
self._guidance_scale = guidance_scale
|
86 |
+
self._joint_attention_kwargs = joint_attention_kwargs
|
87 |
+
self._interrupt = False
|
88 |
+
|
89 |
+
# 2. Define call parameters
|
90 |
+
batch_size = 1 if isinstance(prompt, str) else len(prompt)
|
91 |
+
device = self._execution_device
|
92 |
+
|
93 |
+
# 3. Encode prompt
|
94 |
+
lora_scale = joint_attention_kwargs.get("scale", None) if joint_attention_kwargs is not None else None
|
95 |
+
prompt_embeds, pooled_prompt_embeds, text_ids = self.encode_prompt(
|
96 |
+
prompt=prompt,
|
97 |
+
prompt_2=prompt_2,
|
98 |
+
prompt_embeds=prompt_embeds,
|
99 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
100 |
+
device=device,
|
101 |
+
num_images_per_prompt=num_images_per_prompt,
|
102 |
+
max_sequence_length=max_sequence_length,
|
103 |
+
lora_scale=lora_scale,
|
104 |
+
)
|
105 |
+
# 4. Prepare latent variables
|
106 |
+
num_channels_latents = self.transformer.config.in_channels // 4
|
107 |
+
latents, latent_image_ids = self.prepare_latents(
|
108 |
+
batch_size * num_images_per_prompt,
|
109 |
+
num_channels_latents,
|
110 |
+
height,
|
111 |
+
width,
|
112 |
+
prompt_embeds.dtype,
|
113 |
+
device,
|
114 |
+
generator,
|
115 |
+
latents,
|
116 |
+
)
|
117 |
+
# 5. Prepare timesteps
|
118 |
+
sigmas = np.linspace(1.0, 1 / num_inference_steps, num_inference_steps)
|
119 |
+
image_seq_len = latents.shape[1]
|
120 |
+
mu = calculate_timestep_shift(image_seq_len)
|
121 |
+
timesteps, num_inference_steps = prepare_timesteps(
|
122 |
+
self.scheduler,
|
123 |
+
num_inference_steps,
|
124 |
+
device,
|
125 |
+
timesteps,
|
126 |
+
sigmas,
|
127 |
+
mu=mu,
|
128 |
+
)
|
129 |
+
self._num_timesteps = len(timesteps)
|
130 |
+
|
131 |
+
# Handle guidance
|
132 |
+
guidance = torch.full([1], guidance_scale, device=device, dtype=torch.float16).expand(latents.shape[0]) if self.transformer.config.guidance_embeds else None
|
133 |
+
|
134 |
+
# 6. Denoising loop
|
135 |
+
for i, t in enumerate(timesteps):
|
136 |
+
if self.interrupt:
|
137 |
+
continue
|
138 |
+
|
139 |
+
timestep = t.expand(latents.shape[0]).to(latents.dtype)
|
140 |
+
|
141 |
+
noise_pred = self.transformer(
|
142 |
+
hidden_states=latents,
|
143 |
+
timestep=timestep / 1000,
|
144 |
+
guidance=guidance,
|
145 |
+
pooled_projections=pooled_prompt_embeds,
|
146 |
+
encoder_hidden_states=prompt_embeds,
|
147 |
+
txt_ids=text_ids,
|
148 |
+
img_ids=latent_image_ids,
|
149 |
+
joint_attention_kwargs=self.joint_attention_kwargs,
|
150 |
+
return_dict=False,
|
151 |
+
)[0]
|
152 |
+
|
153 |
+
# Yield intermediate result
|
154 |
+
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
155 |
+
torch.cuda.empty_cache()
|
156 |
+
|
157 |
+
# Final image
|
158 |
+
yield self._decode_latents_to_image(latents, height, width, output_type)
|
159 |
+
self.maybe_free_model_hooks()
|
160 |
+
torch.cuda.empty_cache()
|
161 |
+
|
162 |
+
def _decode_latents_to_image(self, latents, height, width, output_type, vae=None):
|
163 |
+
"""Decodes the given latents into an image."""
|
164 |
+
vae = vae or self.vae
|
165 |
+
latents = self._unpack_latents(latents, height, width, self.vae_scale_factor)
|
166 |
+
latents = (latents / vae.config.scaling_factor) + vae.config.shift_factor
|
167 |
+
image = vae.decode(latents, return_dict=False)[0]
|
168 |
+
return self.image_processor.postprocess(image, output_type=output_type)[0]
|
gradio_cached_examples/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
gradio_cached_examples/25/Hasil Gambar/357e0c8d8a0e75b63595/image.webp
ADDED
gradio_cached_examples/25/Hasil Gambar/727c385c02cbde1f1576/image.webp
ADDED
gradio_cached_examples/25/Hasil Gambar/827754ef7725b469cc3b/image.webp
ADDED
gradio_cached_examples/25/indices.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
0
|
2 |
+
4
|
3 |
+
5
|
gradio_cached_examples/25/log.csv
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Hasil Gambar,Seed,Latency,flag,username,timestamp
|
2 |
+
"{""path"": ""gradio_cached_examples/25/Hasil Gambar/727c385c02cbde1f1576/image.webp"", ""url"": ""/file=/private/var/folders/vd/j2y_f_m51g549p6b1r04gm840000gn/T/gradio/77f2ec64bb9e013ff44412eb6113b60d159b39bc6b4896c611878433c630bceb/image.webp"", ""size"": null, ""orig_name"": ""image.webp"", ""mime_type"": null, ""is_stream"": false, ""meta"": {""_type"": ""gradio.FileData""}}",42,Latency: 2.70 seconds,,,2024-09-17 15:46:20.843746
|
3 |
+
"{""path"": ""gradio_cached_examples/25/Hasil Gambar/357e0c8d8a0e75b63595/image.webp"", ""url"": ""/file=/private/var/folders/vd/j2y_f_m51g549p6b1r04gm840000gn/T/gradio/75bf61373bd5f8769ef00ed36a36a0ad9ac4b5acdd5e87525a2bcad08cc801e5/image.webp"", ""size"": null, ""orig_name"": ""image.webp"", ""mime_type"": null, ""is_stream"": false, ""meta"": {""_type"": ""gradio.FileData""}}",42,Latency: 1.54 seconds,,,2024-09-17 15:53:24.921673
|
4 |
+
"{""path"": ""gradio_cached_examples/25/Hasil Gambar/827754ef7725b469cc3b/image.webp"", ""url"": ""/file=/private/var/folders/vd/j2y_f_m51g549p6b1r04gm840000gn/T/gradio/dc416db9554a73bb302e4594854753abd87a742ddef9604d877f85dbeff179a5/image.webp"", ""size"": null, ""orig_name"": ""image.webp"", ""mime_type"": null, ""is_stream"": false, ""meta"": {""_type"": ""gradio.FileData""}}",42,Latency: 1.66 seconds,,,2024-09-17 15:56:55.732014
|
gradio_cached_examples/29/Generated Image/c9de53a0541e2186d948/image.webp
ADDED
gradio_cached_examples/29/indices.csv
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
5
|
gradio_cached_examples/29/log.csv
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Generated Image,Seed,Latency,flag,username,timestamp
|
2 |
+
"{""path"": ""gradio_cached_examples/29/Generated Image/c9de53a0541e2186d948/image.webp"", ""url"": ""/file=/private/var/folders/vd/j2y_f_m51g549p6b1r04gm840000gn/T/gradio/f241273bd433bc7094613cce640fc438824921e40151f296ff33088e4fa26c7a/image.webp"", ""size"": null, ""orig_name"": ""image.webp"", ""mime_type"": null, ""is_stream"": false, ""meta"": {""_type"": ""gradio.FileData""}}",42,Latency: 2.30 seconds,,,2024-09-17 15:39:23.570453
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
git+https://github.com/huggingface/diffusers.git
|
3 |
+
torch
|
4 |
+
gradio
|
5 |
+
transformers
|
6 |
+
xformers
|
7 |
+
sentencepiece
|
themes.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
from typing import Iterable
|
3 |
+
from gradio.themes.base import Base
|
4 |
+
from gradio.themes.utils import colors, fonts, sizes
|
5 |
+
|
6 |
+
class IndonesiaTheme(Base):
|
7 |
+
def __init__(
|
8 |
+
self,
|
9 |
+
*,
|
10 |
+
primary_hue: colors.Color | str = colors.red,
|
11 |
+
secondary_hue: colors.Color | str = colors.gray,
|
12 |
+
neutral_hue: colors.Color | str = colors.gray,
|
13 |
+
spacing_size: sizes.Size | str = sizes.spacing_md,
|
14 |
+
radius_size: sizes.Size | str = sizes.radius_md,
|
15 |
+
text_size: sizes.Size | str = sizes.text_lg,
|
16 |
+
font: fonts.Font
|
17 |
+
| str
|
18 |
+
| Iterable[fonts.Font | str] = (
|
19 |
+
fonts.GoogleFont("Quicksand"),
|
20 |
+
"ui-sans-serif",
|
21 |
+
"sans-serif",
|
22 |
+
),
|
23 |
+
font_mono: fonts.Font
|
24 |
+
| str
|
25 |
+
| Iterable[fonts.Font | str] = (
|
26 |
+
fonts.GoogleFont("IBM Plex Mono"),
|
27 |
+
"ui-monospace",
|
28 |
+
"monospace",
|
29 |
+
),
|
30 |
+
):
|
31 |
+
super().__init__(
|
32 |
+
primary_hue=primary_hue,
|
33 |
+
secondary_hue=secondary_hue,
|
34 |
+
neutral_hue=neutral_hue,
|
35 |
+
spacing_size=spacing_size,
|
36 |
+
radius_size=radius_size,
|
37 |
+
text_size=text_size,
|
38 |
+
font=font,
|
39 |
+
font_mono=font_mono,
|
40 |
+
)
|
41 |
+
super().set(
|
42 |
+
body_background_fill="linear-gradient(to bottom, #e0e0e0, #7d7d7d)", # Gradasi abu-abu muda ke abu-abu tua
|
43 |
+
body_background_fill_dark="linear-gradient(to bottom, #7d7d7d, #4a4a4a)", # Gradasi abu-abu tua ke lebih gelap untuk dark mode
|
44 |
+
button_primary_background_fill="linear-gradient(90deg, #d84a4a, #b33030)", # Merah ke merah tua
|
45 |
+
button_primary_background_fill_hover="linear-gradient(90deg, #e85b5b, #cc4b4b)", # Merah lebih terang untuk hover
|
46 |
+
button_primary_text_color="white",
|
47 |
+
button_primary_background_fill_dark="linear-gradient(90deg, #b33030, #8f1f1f)", # Merah tua untuk dark mode
|
48 |
+
slider_color="*secondary_300",
|
49 |
+
slider_color_dark="*secondary_600",
|
50 |
+
block_title_text_weight="600",
|
51 |
+
block_border_width="3px",
|
52 |
+
block_shadow="*shadow_drop_lg",
|
53 |
+
button_shadow="*shadow_drop_lg",
|
54 |
+
button_large_padding="32px",
|
55 |
+
)
|