Spaces:
Runtime error
Runtime error
Anurag Bhardwaj
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,66 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load the base model and apply the LoRA weights for super realism
|
| 6 |
+
def load_pipeline():
|
| 7 |
+
base_model = "black-forest-labs/FLUX.1-dev"
|
| 8 |
+
lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
|
| 9 |
+
trigger_word = "Super Realism" # Recommended trigger word
|
| 10 |
|
| 11 |
+
# Load the base model
|
| 12 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
|
| 13 |
+
|
| 14 |
+
# Load the LoRA weights into the pipeline
|
| 15 |
+
pipe.load_lora_weights(lora_repo)
|
| 16 |
+
|
| 17 |
+
# Use GPU if available
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
pipe.to(device)
|
| 20 |
+
return pipe
|
| 21 |
+
|
| 22 |
+
# Instantiate the pipeline once on Space startup
|
| 23 |
+
pipe = load_pipeline()
|
| 24 |
+
|
| 25 |
+
# Define a function for image generation
|
| 26 |
+
def generate_image(prompt, seed, width, height, guidance_scale, randomize_seed):
|
| 27 |
+
# If randomize_seed is selected, allow the model to generate a random seed
|
| 28 |
+
if randomize_seed:
|
| 29 |
+
seed = None
|
| 30 |
+
|
| 31 |
+
# Ensure the prompt includes realism trigger words if needed
|
| 32 |
+
if "realistic" not in prompt.lower() and "realism" not in prompt.lower():
|
| 33 |
+
prompt += " realistic, realism"
|
| 34 |
+
|
| 35 |
+
# Generate the image
|
| 36 |
+
output = pipe(
|
| 37 |
+
prompt=prompt,
|
| 38 |
+
seed=seed,
|
| 39 |
+
width=width,
|
| 40 |
+
height=height,
|
| 41 |
+
guidance_scale=guidance_scale
|
| 42 |
+
)
|
| 43 |
+
return output.images[0]
|
| 44 |
+
|
| 45 |
+
# Set up Gradio interface
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=generate_image,
|
| 48 |
+
inputs=[
|
| 49 |
+
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'"),
|
| 50 |
+
gr.inputs.Slider(0, 10000, step=1, default=0, label="Seed (0 for random)"),
|
| 51 |
+
gr.inputs.Slider(256, 1024, step=64, default=1024, label="Width"),
|
| 52 |
+
gr.inputs.Slider(256, 1024, step=64, default=1024, label="Height"),
|
| 53 |
+
gr.inputs.Slider(1, 20, step=0.5, default=6, label="Guidance Scale"),
|
| 54 |
+
gr.inputs.Checkbox(default=True, label="Randomize Seed")
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.outputs.Image(type="pil", label="Generated Image"),
|
| 57 |
+
title="Flux Super Realism LoRA Demo",
|
| 58 |
+
description=(
|
| 59 |
+
"This demo uses the Flux Super Realism LoRA model for ultra-realistic image generation. "
|
| 60 |
+
"You can use the trigger word 'Super Realism' (recommended) along with other realism-related words "
|
| 61 |
+
"to guide the generation process."
|
| 62 |
+
),
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
iface.launch()
|