Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,12 @@
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from diffusers import FluxPipeline
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
# --- 1. Model Configuration ---
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
base_model_id = "black-forest-labs/FLUX.1-Krea-dev"
|
| 12 |
lora_model_id = "tusharmagar/flux1-krea-dev-lora-solarpunk"
|
| 13 |
|
|
@@ -17,40 +18,36 @@ lora_model_id = "tusharmagar/flux1-krea-dev-lora-solarpunk"
|
|
| 17 |
pipe = FluxPipeline.from_pretrained(
|
| 18 |
base_model_id,
|
| 19 |
torch_dtype=torch.bfloat16,
|
| 20 |
-
token=os.environ.get("HF_TOKEN")
|
| 21 |
)
|
| 22 |
-
# Move the pipeline to the GPU
|
| 23 |
pipe.to(device)
|
| 24 |
|
| 25 |
# Load and fuse your LoRA weights into the pipeline.
|
| 26 |
-
# The model will now use your fine-tuned adjustments for all subsequent inferences.
|
| 27 |
pipe.load_lora_weights(lora_model_id)
|
| 28 |
pipe.fuse_lora()
|
| 29 |
|
| 30 |
|
| 31 |
# --- 3. Define the Inference Function ---
|
| 32 |
-
# This function takes a user's prompt and returns a generated image.
|
| 33 |
-
# We've simplified it by hardcoding optimal settings for this model.
|
| 34 |
def generate_image(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 35 |
"""
|
| 36 |
Generates an image based on the provided prompt.
|
| 37 |
"""
|
| 38 |
-
print(f"Generating image for prompt: {prompt}")
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Run the pipeline to generate the image.
|
| 41 |
-
# We use a generator for reproducibility.
|
| 42 |
-
# FLUX models work well with a low number of steps and no guidance scale.
|
| 43 |
image = pipe(
|
| 44 |
prompt=prompt,
|
| 45 |
num_inference_steps=8,
|
| 46 |
guidance_scale=0.0,
|
| 47 |
-
generator=
|
| 48 |
).images[0]
|
| 49 |
|
| 50 |
return image
|
| 51 |
|
| 52 |
# --- 4. Create the Gradio Interface ---
|
| 53 |
-
# This section builds the web user interface.
|
| 54 |
css = """
|
| 55 |
#col-container {
|
| 56 |
margin: 0 auto;
|
|
@@ -59,17 +56,15 @@ css = """
|
|
| 59 |
"""
|
| 60 |
with gr.Blocks(css=css) as demo:
|
| 61 |
with gr.Column(elem_id="col-container"):
|
| 62 |
-
# Add a title and description for your demo
|
| 63 |
gr.Markdown(
|
| 64 |
"""
|
| 65 |
# FLUX.1 Solarpunk LoRA βοΈ
|
| 66 |
An interactive demo for the [flux1-krea-dev-lora-solarpunk](https://huggingface.co/tusharmagar/flux1-krea-dev-lora-solarpunk) model.
|
| 67 |
-
This LoRA excels at creating dreamy, solarpunk imaginations of real-world cities.
|
| 68 |
**Don't forget to add the trigger word `[SLRPNK]` to your prompt!**
|
| 69 |
"""
|
| 70 |
)
|
| 71 |
|
| 72 |
-
# Create the main input and output components
|
| 73 |
with gr.Row():
|
| 74 |
prompt_input = gr.Textbox(
|
| 75 |
label="Enter your prompt",
|
|
@@ -83,7 +78,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 83 |
|
| 84 |
image_output = gr.Image(label="Result", show_label=False)
|
| 85 |
|
| 86 |
-
# Add your specific examples to make it easy for users to try
|
| 87 |
gr.Examples(
|
| 88 |
examples=[
|
| 89 |
"Solarpunk London with hexagonal solar panels, white architecture, keeping Big Ben unchanged, with a double decker bus on the road [SLRPNK]",
|
|
@@ -96,7 +90,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 96 |
fn=generate_image,
|
| 97 |
)
|
| 98 |
|
| 99 |
-
# Connect the button click and prompt submission to the generation function
|
| 100 |
generate_button.click(
|
| 101 |
fn=generate_image,
|
| 102 |
inputs=[prompt_input],
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from diffusers import FluxPipeline
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# --- 1. Model Configuration ---
|
| 9 |
+
# Automatically detect if a GPU is available, otherwise use CPU
|
| 10 |
+
# The app will only work well on a GPU, but this prevents it from crashing on startup.
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
base_model_id = "black-forest-labs/FLUX.1-Krea-dev"
|
| 13 |
lora_model_id = "tusharmagar/flux1-krea-dev-lora-solarpunk"
|
| 14 |
|
|
|
|
| 18 |
pipe = FluxPipeline.from_pretrained(
|
| 19 |
base_model_id,
|
| 20 |
torch_dtype=torch.bfloat16,
|
| 21 |
+
token=os.environ.get("HF_TOKEN")
|
| 22 |
)
|
| 23 |
+
# Move the pipeline to the selected device (GPU)
|
| 24 |
pipe.to(device)
|
| 25 |
|
| 26 |
# Load and fuse your LoRA weights into the pipeline.
|
|
|
|
| 27 |
pipe.load_lora_weights(lora_model_id)
|
| 28 |
pipe.fuse_lora()
|
| 29 |
|
| 30 |
|
| 31 |
# --- 3. Define the Inference Function ---
|
|
|
|
|
|
|
| 32 |
def generate_image(prompt, progress=gr.Progress(track_tqdm=True)):
|
| 33 |
"""
|
| 34 |
Generates an image based on the provided prompt.
|
| 35 |
"""
|
| 36 |
+
print(f"Generating image for prompt: {prompt}")
|
| 37 |
+
|
| 38 |
+
# Ensure the generator is on the correct device
|
| 39 |
+
generator = torch.Generator(device=device).manual_seed(42)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
image = pipe(
|
| 42 |
prompt=prompt,
|
| 43 |
num_inference_steps=8,
|
| 44 |
guidance_scale=0.0,
|
| 45 |
+
generator=generator
|
| 46 |
).images[0]
|
| 47 |
|
| 48 |
return image
|
| 49 |
|
| 50 |
# --- 4. Create the Gradio Interface ---
|
|
|
|
| 51 |
css = """
|
| 52 |
#col-container {
|
| 53 |
margin: 0 auto;
|
|
|
|
| 56 |
"""
|
| 57 |
with gr.Blocks(css=css) as demo:
|
| 58 |
with gr.Column(elem_id="col-container"):
|
|
|
|
| 59 |
gr.Markdown(
|
| 60 |
"""
|
| 61 |
# FLUX.1 Solarpunk LoRA βοΈ
|
| 62 |
An interactive demo for the [flux1-krea-dev-lora-solarpunk](https://huggingface.co/tusharmagar/flux1-krea-dev-lora-solarpunk) model.
|
| 63 |
+
This LoRA excels at creating dreamy, solarpunk imaginations of real-world cities.
|
| 64 |
**Don't forget to add the trigger word `[SLRPNK]` to your prompt!**
|
| 65 |
"""
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
with gr.Row():
|
| 69 |
prompt_input = gr.Textbox(
|
| 70 |
label="Enter your prompt",
|
|
|
|
| 78 |
|
| 79 |
image_output = gr.Image(label="Result", show_label=False)
|
| 80 |
|
|
|
|
| 81 |
gr.Examples(
|
| 82 |
examples=[
|
| 83 |
"Solarpunk London with hexagonal solar panels, white architecture, keeping Big Ben unchanged, with a double decker bus on the road [SLRPNK]",
|
|
|
|
| 90 |
fn=generate_image,
|
| 91 |
)
|
| 92 |
|
|
|
|
| 93 |
generate_button.click(
|
| 94 |
fn=generate_image,
|
| 95 |
inputs=[prompt_input],
|