|
import spaces |
|
import argparse |
|
import os |
|
import time |
|
from os import path |
|
from safetensors.torch import load_file |
|
from huggingface_hub import hf_hub_download |
|
from transformers.utils.hub import move_cache |
|
|
|
|
|
cache_path = path.join(path.dirname(path.abspath(__file__)), "models") |
|
|
|
os.environ["HF_HUB_CACHE"] = cache_path |
|
os.environ["HF_HOME"] = cache_path |
|
|
|
import gradio as gr |
|
import torch |
|
from diffusers import FluxPipeline |
|
|
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
|
|
class timer: |
|
def __init__(self, method_name="timed process"): |
|
self.method = method_name |
|
def __enter__(self): |
|
self.start = time.time() |
|
print(f"{self.method} starts") |
|
def __exit__(self, exc_type, exc_val, exc_tb): |
|
end = time.time() |
|
print(f"{self.method} took {str(round(end - self.start, 2))}s") |
|
|
|
if not path.exists(cache_path): |
|
os.makedirs(cache_path, exist_ok=True) |
|
|
|
pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16) |
|
pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors")) |
|
pipe.fuse_lora(lora_scale=0.125) |
|
pipe.to(device="cuda", dtype=torch.bfloat16) |
|
|
|
|
|
css = """ |
|
# gen_btn{height: 100%} |
|
#gen_column{align-self: stretch} |
|
.primary{background-color: #4C76FF !important} |
|
#grower-label-span span{background-color: #4C76FF !important} |
|
#grower-label-image label{background-color: #4C76FF !important} |
|
""" |
|
|
|
|
|
|
|
js_code = """ |
|
function createGradioAnimation() { |
|
const emojis = ['โจ', '๐ค', '๐', '๐จ', '๐', '๐ฑ', '๐ฎ', '๐ฅฐ', '๐', '๐']; |
|
const gravity = 0.5; |
|
const bounceFactor = -0.7; |
|
const friction = 0.9; |
|
|
|
document.getElementById('gen_btn').addEventListener('click', (event) => { |
|
const count = Math.floor(Math.random() * 6) + 10; |
|
for (let i = 0; i < count; i++) { |
|
createEmoji(event.clientX, event.clientY); |
|
} |
|
}); |
|
|
|
function createEmoji(x, y) { |
|
const emojiElement = document.createElement('div'); |
|
emojiElement.textContent = emojis[Math.floor(Math.random() * emojis.length)]; |
|
emojiElement.style.position = 'absolute'; |
|
emojiElement.style.fontSize = '24px'; |
|
emojiElement.style.transition = 'opacity 0.1s ease-out'; |
|
document.body.appendChild(emojiElement); |
|
|
|
const rect = emojiElement.getBoundingClientRect(); |
|
let posX = x - rect.width / 2; |
|
let posY = y - rect.height / 2; |
|
let velX = (Math.random() - 0.5) * 10; |
|
let velY = (Math.random() - 0.5) * 10; |
|
|
|
function update() { |
|
if (posY + rect.height >= window.innerHeight) { |
|
posY = window.innerHeight - rect.height; |
|
velY *= bounceFactor; |
|
} else { |
|
velY += gravity; |
|
} |
|
|
|
if (posX <= 0 || posX + rect.width >= window.innerWidth) { |
|
velX *= bounceFactor; |
|
} |
|
|
|
velX *= friction; |
|
velY *= friction; |
|
|
|
posX += velX; |
|
posY += velY; |
|
|
|
emojiElement.style.transform = `translate(${posX}px, ${posY}px)`; |
|
|
|
if (Math.abs(velX) > 0.1 || Math.abs(velY) > 0.1) { |
|
requestAnimationFrame(update); |
|
} else { |
|
emojiElement.style.opacity=0; |
|
setTimeout(function(){ |
|
emojiElement.remove();}, 2000); |
|
} |
|
} |
|
|
|
update(); |
|
} |
|
return 'Animation created'; |
|
} |
|
""" |
|
|
|
with gr.Blocks(theme='charbel-malo/Crystal', js=js_code) as demo: |
|
gr.Markdown( |
|
""" |
|
<div style="text-align: left;margin-top:20px"> |
|
<h1><img src="https://staging.the-grower.com/assets/images/grower_logo_dark.png" style="height:50px;object-fit:contain;"> GrowerAI VisionPRO</h1> |
|
<p style="font-size: 1rem; margin-bottom: 1.5rem;">HyperFlux-based Image Generation Model 8Steps-Lora</p> |
|
</div> |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=3): |
|
with gr.Group(): |
|
base_prompt = gr.Textbox( |
|
label="Base Prompt", |
|
placeholder="E.g., A serene landscape with mountains and a lake at sunset", |
|
lines=3, |
|
elem_id="grower-label-span" |
|
) |
|
with gr.Accordion("Advanced Prompt Settings", open=False): |
|
subject = gr.Textbox(label="Subject", placeholder="Enter the subject") |
|
object_ = gr.Textbox(label="Object", placeholder="Enter the object") |
|
style = gr.Textbox(label="Style", placeholder="Enter the style") |
|
clothing = gr.Textbox(label="Clothing", placeholder="Enter the clothing") |
|
objective = gr.Dropdown( |
|
choices=["digital marketing post","website hero visual","Ad cover","Movie poster"], |
|
value=None, |
|
multiselect=False, |
|
label="Objective", |
|
info="Select an objective" |
|
) |
|
with gr.Accordion("Advanced Settings", open=False): |
|
with gr.Group(): |
|
with gr.Row(): |
|
height = gr.Slider(label="Height", minimum=256, maximum=1152, step=64, value=1024) |
|
width = gr.Slider(label="Width", minimum=256, maximum=1152, step=64, value=1024) |
|
|
|
with gr.Row(): |
|
steps = gr.Slider(label="Inference Steps", minimum=6, maximum=25, step=1, value=8) |
|
scales = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=5.0, step=0.1, value=3.5) |
|
|
|
seed = gr.Number(label="Seed (for reproducibility)", value=3413, precision=0) |
|
|
|
generate_btn = gr.Button("Generate Image", variant="primary", scale=1, elem_id="gen_btn") |
|
|
|
with gr.Column(scale=4): |
|
output = gr.Image(label="Your Generated Image", elem_id="grower-label-image") |
|
|
|
gr.Markdown( |
|
""" |
|
<div style="margin: 2rem auto; padding: 1rem; border-radius: 10px;"> |
|
<h2 style="font-size: 1.5rem; margin-bottom: 1rem;">How to Use</h2> |
|
<ol style="padding-left: 1.5rem;"> |
|
<li>Enter a detailed description of the image you want to create.</li> |
|
<li>Adjust advanced settings if desired (tap to expand).</li> |
|
<li>Tap "Generate Image" and wait for your creation!</li> |
|
</ol> |
|
<p style="margin-top: 1rem; font-style: italic;">Tip: Be specific in your description for best results!</p> |
|
</div> |
|
""" |
|
) |
|
|
|
@spaces.GPU |
|
def process_image(height, width, steps, scales, base_prompt, subject, object_, style, clothing, objective, seed): |
|
|
|
advanced_prompt_template = ( |
|
"Create a highly stylized digital avatar of {subject}, holding {object}. " |
|
"joy, simplified {subject} avatar or emoji. , typical of 3D digital art :: " |
|
"The overall style is {style}. {clothing}. and modern digital art style, " |
|
"detailed shading, and dynamic positioning that makes it suitable for {objective}" |
|
) |
|
advanced_prompt = advanced_prompt_template.format( |
|
subject=subject, |
|
object=object_, |
|
style=style, |
|
clothing=clothing, |
|
objective=objective |
|
) |
|
|
|
prompt = advanced_prompt |
|
global pipe |
|
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"): |
|
return pipe( |
|
prompt=[prompt], |
|
generator=torch.Generator().manual_seed(int(seed)), |
|
num_inference_steps=int(steps), |
|
guidance_scale=float(scales), |
|
height=int(height), |
|
width=int(width), |
|
max_sequence_length=256 |
|
).images[0] |
|
|
|
generate_btn.click( |
|
process_image, |
|
inputs=[ |
|
height, width, steps, scales, base_prompt, subject, object_, style, clothing, objective, seed |
|
], |
|
outputs=output |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |