Spaces:
Running
on
Zero
Running
on
Zero
import torch | |
from diffusers import DiffusionPipeline | |
import gradio as gr | |
# Load the base model and apply the LoRA weights for super realism | |
def load_pipeline(): | |
base_model = "black-forest-labs/FLUX.1-dev" | |
lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA" | |
trigger_word = "Super Realism" # Recommended trigger word | |
# Load the base model | |
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16) | |
# Load the LoRA weights into the pipeline | |
pipe.load_lora_weights(lora_repo) | |
# Use GPU if available | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
pipe.to(device) | |
return pipe | |
# Instantiate the pipeline once on Space startup | |
pipe = load_pipeline() | |
# Define a function for image generation | |
def generate_image(prompt, seed, width, height, guidance_scale, randomize_seed): | |
# If randomize_seed is selected, allow the model to generate a random seed | |
if randomize_seed: | |
seed = None | |
# Ensure the prompt includes realism trigger words if needed | |
if "realistic" not in prompt.lower() and "realism" not in prompt.lower(): | |
prompt += " realistic, realism" | |
# Generate the image | |
output = pipe( | |
prompt=prompt, | |
seed=seed, | |
width=width, | |
height=height, | |
guidance_scale=guidance_scale | |
) | |
return output.images[0] | |
# Set up Gradio interface | |
iface = gr.Interface( | |
fn=generate_image, | |
inputs=[ | |
gr.inputs.Textbox(lines=2, label="Prompt", placeholder="Enter your prompt, e.g., 'A tiny astronaut hatching from an egg on the moon, 4k, planet theme'"), | |
gr.inputs.Slider(0, 10000, step=1, default=0, label="Seed (0 for random)"), | |
gr.inputs.Slider(256, 1024, step=64, default=1024, label="Width"), | |
gr.inputs.Slider(256, 1024, step=64, default=1024, label="Height"), | |
gr.inputs.Slider(1, 20, step=0.5, default=6, label="Guidance Scale"), | |
gr.inputs.Checkbox(default=True, label="Randomize Seed") | |
], | |
outputs=gr.outputs.Image(type="pil", label="Generated Image"), | |
title="Flux Super Realism LoRA Demo", | |
description=( | |
"This demo uses the Flux Super Realism LoRA model for ultra-realistic image generation. " | |
"You can use the trigger word 'Super Realism' (recommended) along with other realism-related words " | |
"to guide the generation process." | |
), | |
) | |
if __name__ == "__main__": | |
iface.launch() | |