Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,23 +28,6 @@ from RealESRGAN import RealESRGAN
|
|
| 28 |
|
| 29 |
import os
|
| 30 |
|
| 31 |
-
from unittest.mock import patch
|
| 32 |
-
from typing import Union
|
| 33 |
-
from transformers.dynamic_module_utils import get_imports
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# def fixed_get_imports(filename):
|
| 37 |
-
# """Work around for https://huggingface.co/microsoft/phi-1_5/discussions/72."""
|
| 38 |
-
# if not str(filename).endswith("/modeling_florence2.py"):
|
| 39 |
-
# return get_imports(filename)
|
| 40 |
-
# imports = get_imports(filename)
|
| 41 |
-
# imports.remove("flash_attn")
|
| 42 |
-
# return imports
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
import subprocess
|
| 46 |
-
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 47 |
-
|
| 48 |
def download_file(url, folder_path, filename):
|
| 49 |
if not os.path.exists(folder_path):
|
| 50 |
os.makedirs(folder_path)
|
|
@@ -125,14 +108,6 @@ DEFAULT_NEGATIVE_SUFFIX = "Nsfw oversaturated crappy_art low_quality blurry bad_
|
|
| 125 |
# Initialize Florence model
|
| 126 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 127 |
|
| 128 |
-
#def load_models():
|
| 129 |
-
# with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
|
| 130 |
-
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-Large', trust_remote_code=True).to(device).eval()
|
| 131 |
-
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-Large', trust_remote_code=True)
|
| 132 |
-
# return florence_model, florence_processor
|
| 133 |
-
|
| 134 |
-
#florence_model, florence_processor = load_models()
|
| 135 |
-
|
| 136 |
# Prompt Enhancer
|
| 137 |
enhancer_medium = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance", device=device)
|
| 138 |
enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
|
|
@@ -157,28 +132,6 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 157 |
lazy_realesrgan_x2 = LazyRealESRGAN(device, scale=2)
|
| 158 |
lazy_realesrgan_x4 = LazyRealESRGAN(device, scale=4)
|
| 159 |
|
| 160 |
-
# Florence caption function
|
| 161 |
-
def florence_caption(image):
|
| 162 |
-
# Convert image to PIL if it's not already
|
| 163 |
-
if not isinstance(image, Image.Image):
|
| 164 |
-
image = Image.fromarray(image)
|
| 165 |
-
|
| 166 |
-
inputs = florence_processor(text="<DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
|
| 167 |
-
generated_ids = florence_model.generate(
|
| 168 |
-
input_ids=inputs["input_ids"],
|
| 169 |
-
pixel_values=inputs["pixel_values"],
|
| 170 |
-
max_new_tokens=1024,
|
| 171 |
-
early_stopping=False,
|
| 172 |
-
do_sample=False,
|
| 173 |
-
num_beams=3,
|
| 174 |
-
)
|
| 175 |
-
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 176 |
-
parsed_answer = florence_processor.post_process_generation(
|
| 177 |
-
generated_text,
|
| 178 |
-
task="<DETAILED_CAPTION>",
|
| 179 |
-
image_size=(image.width, image.height)
|
| 180 |
-
)
|
| 181 |
-
return parsed_answer["<DETAILED_CAPTION>"]
|
| 182 |
|
| 183 |
# Prompt Enhancer function
|
| 184 |
def enhance_prompt(input_prompt, model_choice):
|
|
@@ -208,11 +161,9 @@ def upscale_image(image, scale):
|
|
| 208 |
|
| 209 |
@spaces.GPU(duration=120)
|
| 210 |
def generate_image(model_choice, additional_positive_prompt, additional_negative_prompt, height, width, num_inference_steps,
|
| 211 |
-
guidance_scale, num_images_per_prompt, use_random_seed, seed, sampler, clip_skip,
|
| 212 |
-
use_florence2, use_medium_enhancer, use_long_enhancer,
|
| 213 |
use_positive_prefix, use_positive_suffix, use_negative_prefix, use_negative_suffix,
|
| 214 |
-
use_upscaler, upscale_factor,
|
| 215 |
-
input_image=None, progress=gr.Progress(track_tqdm=True)):
|
| 216 |
|
| 217 |
# Select the appropriate pipe based on the model choice
|
| 218 |
if model_choice == "Pony Realism v22":
|
|
@@ -236,12 +187,6 @@ def generate_image(model_choice, additional_positive_prompt, additional_negative
|
|
| 236 |
# Start with the default positive prompt prefix if enabled
|
| 237 |
full_positive_prompt = DEFAULT_POSITIVE_PREFIX + ", " if use_positive_prefix else ""
|
| 238 |
|
| 239 |
-
# Add Florence-2 caption if enabled and image is provided
|
| 240 |
-
if use_florence2 and input_image is not None:
|
| 241 |
-
florence2_caption = florence_caption(input_image)
|
| 242 |
-
florence2_caption = florence2_caption.lower().replace('.', ',')
|
| 243 |
-
additional_positive_prompt = f"{florence2_caption}, {additional_positive_prompt}" if additional_positive_prompt else florence2_caption
|
| 244 |
-
|
| 245 |
# Enhance only the additional positive prompt if enhancers are enabled
|
| 246 |
if additional_positive_prompt:
|
| 247 |
enhanced_prompt = additional_positive_prompt
|
|
@@ -336,9 +281,7 @@ with gr.Blocks(theme='bethecloud/storj_theme') as demo:
|
|
| 336 |
sampler = gr.Dropdown(label="Sampler", choices=list(samplers.keys()), value="Euler a")
|
| 337 |
clip_skip = gr.Slider(1, 4, 2, step=1, label="Clip skip")
|
| 338 |
|
| 339 |
-
with gr.Accordion("
|
| 340 |
-
input_image = gr.Image(label="Input Image for Florence-2 Captioner")
|
| 341 |
-
use_florence2 = gr.Checkbox(label="Use Florence-2 Captioner", value=False)
|
| 342 |
use_medium_enhancer = gr.Checkbox(label="Use Medium Prompt Enhancer", value=False)
|
| 343 |
use_long_enhancer = gr.Checkbox(label="Use Long Prompt Enhancer", value=False)
|
| 344 |
|
|
@@ -382,10 +325,9 @@ with gr.Blocks(theme='bethecloud/storj_theme') as demo:
|
|
| 382 |
model_choice, # Add this new input
|
| 383 |
positive_prompt, negative_prompt, height, width, num_inference_steps,
|
| 384 |
guidance_scale, num_images_per_prompt, use_random_seed, seed, sampler,
|
| 385 |
-
clip_skip,
|
| 386 |
use_positive_prefix, use_positive_suffix, use_negative_prefix, use_negative_suffix,
|
| 387 |
-
use_upscaler, upscale_factor
|
| 388 |
-
input_image
|
| 389 |
],
|
| 390 |
outputs=[output_gallery, seed_used, full_positive_prompt_used, full_negative_prompt_used]
|
| 391 |
)
|
|
|
|
| 28 |
|
| 29 |
import os
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def download_file(url, folder_path, filename):
|
| 32 |
if not os.path.exists(folder_path):
|
| 33 |
os.makedirs(folder_path)
|
|
|
|
| 108 |
# Initialize Florence model
|
| 109 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# Prompt Enhancer
|
| 112 |
enhancer_medium = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance", device=device)
|
| 113 |
enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
|
|
|
|
| 132 |
lazy_realesrgan_x2 = LazyRealESRGAN(device, scale=2)
|
| 133 |
lazy_realesrgan_x4 = LazyRealESRGAN(device, scale=4)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Prompt Enhancer function
|
| 137 |
def enhance_prompt(input_prompt, model_choice):
|
|
|
|
| 161 |
|
| 162 |
@spaces.GPU(duration=120)
|
| 163 |
def generate_image(model_choice, additional_positive_prompt, additional_negative_prompt, height, width, num_inference_steps,
|
| 164 |
+
guidance_scale, num_images_per_prompt, use_random_seed, seed, sampler, clip_skip, use_medium_enhancer, use_long_enhancer,
|
|
|
|
| 165 |
use_positive_prefix, use_positive_suffix, use_negative_prefix, use_negative_suffix,
|
| 166 |
+
use_upscaler, upscale_factor, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 167 |
|
| 168 |
# Select the appropriate pipe based on the model choice
|
| 169 |
if model_choice == "Pony Realism v22":
|
|
|
|
| 187 |
# Start with the default positive prompt prefix if enabled
|
| 188 |
full_positive_prompt = DEFAULT_POSITIVE_PREFIX + ", " if use_positive_prefix else ""
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
# Enhance only the additional positive prompt if enhancers are enabled
|
| 191 |
if additional_positive_prompt:
|
| 192 |
enhanced_prompt = additional_positive_prompt
|
|
|
|
| 281 |
sampler = gr.Dropdown(label="Sampler", choices=list(samplers.keys()), value="Euler a")
|
| 282 |
clip_skip = gr.Slider(1, 4, 2, step=1, label="Clip skip")
|
| 283 |
|
| 284 |
+
with gr.Accordion("Enhancers", open=False):
|
|
|
|
|
|
|
| 285 |
use_medium_enhancer = gr.Checkbox(label="Use Medium Prompt Enhancer", value=False)
|
| 286 |
use_long_enhancer = gr.Checkbox(label="Use Long Prompt Enhancer", value=False)
|
| 287 |
|
|
|
|
| 325 |
model_choice, # Add this new input
|
| 326 |
positive_prompt, negative_prompt, height, width, num_inference_steps,
|
| 327 |
guidance_scale, num_images_per_prompt, use_random_seed, seed, sampler,
|
| 328 |
+
clip_skip, use_medium_enhancer, use_long_enhancer,
|
| 329 |
use_positive_prefix, use_positive_suffix, use_negative_prefix, use_negative_suffix,
|
| 330 |
+
use_upscaler, upscale_factor
|
|
|
|
| 331 |
],
|
| 332 |
outputs=[output_gallery, seed_used, full_positive_prompt_used, full_negative_prompt_used]
|
| 333 |
)
|