Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
-
from transformers import AutoProcessor,
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import random
|
| 8 |
import numpy as np
|
|
@@ -20,7 +20,9 @@ dtype = torch.bfloat16
|
|
| 20 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 21 |
|
| 22 |
# FLUX.1-dev model
|
| 23 |
-
pipe = DiffusionPipeline.from_pretrained(
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Initialize Qwen2VL model
|
| 26 |
qwen_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
|
@@ -32,7 +34,7 @@ qwen_processor = AutoProcessor.from_pretrained("prithivMLmods/JSONify-Flux", tru
|
|
| 32 |
enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
|
| 33 |
|
| 34 |
MAX_SEED = np.iinfo(np.int32).max
|
| 35 |
-
MAX_IMAGE_SIZE =
|
| 36 |
|
| 37 |
# Qwen2VL caption function
|
| 38 |
@spaces.GPU
|
|
@@ -101,14 +103,24 @@ def process_workflow(image, text_prompt, use_enhancer, seed, randomize_seed, wid
|
|
| 101 |
|
| 102 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
return image, prompt, seed
|
| 114 |
|
|
@@ -151,10 +163,10 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="blue", secondar
|
|
| 151 |
use_enhancer = gr.Checkbox(label="Use Prompt Enhancer", value=False)
|
| 152 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 153 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 154 |
-
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=
|
| 155 |
-
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=
|
| 156 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
|
| 157 |
-
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=
|
| 158 |
|
| 159 |
generate_btn = gr.Button("Generate Image", elem_classes="submit-btn")
|
| 160 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
+
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration, pipeline
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import random
|
| 8 |
import numpy as np
|
|
|
|
| 20 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 21 |
|
| 22 |
# FLUX.1-dev model
|
| 23 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 24 |
+
"black-forest-labs/FLUX.1-dev", torch_dtype=dtype, token=huggingface_token
|
| 25 |
+
).to(device)
|
| 26 |
|
| 27 |
# Initialize Qwen2VL model
|
| 28 |
qwen_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
|
|
|
| 34 |
enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
|
| 35 |
|
| 36 |
MAX_SEED = np.iinfo(np.int32).max
|
| 37 |
+
MAX_IMAGE_SIZE = 1024 # Reduced to prevent memory issues
|
| 38 |
|
| 39 |
# Qwen2VL caption function
|
| 40 |
@spaces.GPU
|
|
|
|
| 103 |
|
| 104 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 105 |
|
| 106 |
+
# Reduce memory usage by clearing GPU cache
|
| 107 |
+
torch.cuda.empty_cache()
|
| 108 |
+
|
| 109 |
+
# Generate image with FLUX.1-dev
|
| 110 |
+
try:
|
| 111 |
+
image = pipe(
|
| 112 |
+
prompt=prompt,
|
| 113 |
+
generator=generator,
|
| 114 |
+
num_inference_steps=num_inference_steps,
|
| 115 |
+
width=width,
|
| 116 |
+
height=height,
|
| 117 |
+
guidance_scale=guidance_scale
|
| 118 |
+
).images[0]
|
| 119 |
+
except RuntimeError as e:
|
| 120 |
+
if "CUDA out of memory" in str(e):
|
| 121 |
+
raise RuntimeError("CUDA out of memory. Try reducing image size or inference steps.")
|
| 122 |
+
else:
|
| 123 |
+
raise e
|
| 124 |
|
| 125 |
return image, prompt, seed
|
| 126 |
|
|
|
|
| 163 |
use_enhancer = gr.Checkbox(label="Use Prompt Enhancer", value=False)
|
| 164 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 165 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 166 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512) # Reduced default width
|
| 167 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=512) # Reduced default height
|
| 168 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1, maximum=15, step=0.1, value=3.5)
|
| 169 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=20) # Reduced default steps
|
| 170 |
|
| 171 |
generate_btn = gr.Button("Generate Image", elem_classes="submit-btn")
|
| 172 |
|