Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,11 +7,15 @@ def install_packages():
|
|
| 7 |
"transformers>=4.46.0",
|
| 8 |
"diffusers>=0.31.0",
|
| 9 |
"accelerate>=0.26.0",
|
| 10 |
-
"huggingface-hub>=0.23.0"
|
|
|
|
| 11 |
]
|
| 12 |
|
| 13 |
for package in packages:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Run installation before other imports
|
| 17 |
try:
|
|
@@ -84,27 +88,56 @@ torch.backends.cuda.matmul.allow_tf32 = True
|
|
| 84 |
|
| 85 |
# Florence 모델 초기화
|
| 86 |
print("Initializing Florence models...")
|
| 87 |
-
florence_models = {
|
| 88 |
-
|
| 89 |
-
'gokaygokay/Florence-2-Flux-Large',
|
| 90 |
-
trust_remote_code=True
|
| 91 |
-
).eval(),
|
| 92 |
-
'gokaygokay/Florence-2-Flux': AutoModelForCausalLM.from_pretrained(
|
| 93 |
-
'gokaygokay/Florence-2-Flux',
|
| 94 |
-
trust_remote_code=True
|
| 95 |
-
).eval(),
|
| 96 |
-
}
|
| 97 |
|
| 98 |
-
|
| 99 |
-
'
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def filter_prompt(prompt):
|
| 110 |
inappropriate_keywords = [
|
|
@@ -139,6 +172,18 @@ pipe = FluxPipeline.from_pretrained(
|
|
| 139 |
torch_dtype=torch.bfloat16
|
| 140 |
)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
print("Loading LoRA weights...")
|
| 143 |
pipe.load_lora_weights(
|
| 144 |
hf_hub_download(
|
|
@@ -160,6 +205,15 @@ except Exception as e:
|
|
| 160 |
|
| 161 |
@spaces.GPU
|
| 162 |
def generate_caption(image, model_name='gokaygokay/Florence-2-Flux-Large'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
image = Image.fromarray(image)
|
| 164 |
task_prompt = "<DESCRIPTION>"
|
| 165 |
prompt = task_prompt + "Describe this image in great detail."
|
|
@@ -346,10 +400,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 346 |
)
|
| 347 |
|
| 348 |
# Florence 모델 선택 - 숨김 처리
|
|
|
|
| 349 |
florence_model = gr.Dropdown(
|
| 350 |
-
choices=
|
| 351 |
label="Caption Model",
|
| 352 |
-
value=
|
| 353 |
visible=False
|
| 354 |
)
|
| 355 |
|
|
|
|
| 7 |
"transformers>=4.46.0",
|
| 8 |
"diffusers>=0.31.0",
|
| 9 |
"accelerate>=0.26.0",
|
| 10 |
+
"huggingface-hub>=0.23.0",
|
| 11 |
+
"timm", # Required for Florence-2
|
| 12 |
]
|
| 13 |
|
| 14 |
for package in packages:
|
| 15 |
+
try:
|
| 16 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", package], check=True)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Warning: Could not install {package}: {e}")
|
| 19 |
|
| 20 |
# Run installation before other imports
|
| 21 |
try:
|
|
|
|
| 88 |
|
| 89 |
# Florence 모델 초기화
|
| 90 |
print("Initializing Florence models...")
|
| 91 |
+
florence_models = {}
|
| 92 |
+
florence_processors = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
try:
|
| 95 |
+
# Try importing timm to verify it's available
|
| 96 |
+
import timm
|
| 97 |
+
print("timm library available")
|
| 98 |
+
except ImportError:
|
| 99 |
+
print("Installing timm...")
|
| 100 |
+
subprocess.run([sys.executable, "-m", "pip", "install", "timm"], check=True)
|
| 101 |
+
import timm
|
| 102 |
+
|
| 103 |
+
# Initialize Florence models with error handling
|
| 104 |
+
model_names = ['gokaygokay/Florence-2-Flux-Large', 'gokaygokay/Florence-2-Flux']
|
| 105 |
+
|
| 106 |
+
for model_name in model_names:
|
| 107 |
+
try:
|
| 108 |
+
print(f"Loading {model_name}...")
|
| 109 |
+
florence_models[model_name] = AutoModelForCausalLM.from_pretrained(
|
| 110 |
+
model_name,
|
| 111 |
+
trust_remote_code=True
|
| 112 |
+
).eval()
|
| 113 |
+
florence_processors[model_name] = AutoProcessor.from_pretrained(
|
| 114 |
+
model_name,
|
| 115 |
+
trust_remote_code=True
|
| 116 |
+
)
|
| 117 |
+
print(f"Successfully loaded {model_name}")
|
| 118 |
+
except Exception as e:
|
| 119 |
+
print(f"Warning: Could not load {model_name}: {e}")
|
| 120 |
+
# If the large model fails, we'll fall back to the smaller one
|
| 121 |
+
if model_name == 'gokaygokay/Florence-2-Flux-Large' and len(florence_models) == 0:
|
| 122 |
+
print("Attempting to load fallback model...")
|
| 123 |
+
try:
|
| 124 |
+
fallback_model = 'gokaygokay/Florence-2-Flux'
|
| 125 |
+
florence_models[model_name] = AutoModelForCausalLM.from_pretrained(
|
| 126 |
+
fallback_model,
|
| 127 |
+
trust_remote_code=True
|
| 128 |
+
).eval()
|
| 129 |
+
florence_processors[model_name] = AutoProcessor.from_pretrained(
|
| 130 |
+
fallback_model,
|
| 131 |
+
trust_remote_code=True
|
| 132 |
+
)
|
| 133 |
+
print(f"Using {fallback_model} as fallback")
|
| 134 |
+
except Exception as e2:
|
| 135 |
+
print(f"Error loading fallback model: {e2}")
|
| 136 |
+
|
| 137 |
+
if not florence_models:
|
| 138 |
+
print("ERROR: No Florence models could be loaded. Caption generation will not work.")
|
| 139 |
+
else:
|
| 140 |
+
print(f"Loaded {len(florence_models)} Florence model(s)")
|
| 141 |
|
| 142 |
def filter_prompt(prompt):
|
| 143 |
inappropriate_keywords = [
|
|
|
|
| 172 |
torch_dtype=torch.bfloat16
|
| 173 |
)
|
| 174 |
|
| 175 |
+
# Configure attention mechanism
|
| 176 |
+
if ATTN_METHOD == "xformers":
|
| 177 |
+
try:
|
| 178 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 179 |
+
print("Enabled xformers memory efficient attention")
|
| 180 |
+
except Exception as e:
|
| 181 |
+
print(f"Could not enable xformers: {e}")
|
| 182 |
+
elif ATTN_METHOD == "flash_attn":
|
| 183 |
+
print("Flash attention available")
|
| 184 |
+
else:
|
| 185 |
+
print("Using standard attention")
|
| 186 |
+
|
| 187 |
print("Loading LoRA weights...")
|
| 188 |
pipe.load_lora_weights(
|
| 189 |
hf_hub_download(
|
|
|
|
| 205 |
|
| 206 |
@spaces.GPU
|
| 207 |
def generate_caption(image, model_name='gokaygokay/Florence-2-Flux-Large'):
|
| 208 |
+
if not florence_models:
|
| 209 |
+
gr.Warning("Caption models are not loaded. Please refresh the page.")
|
| 210 |
+
return "Caption generation unavailable - please describe your image manually"
|
| 211 |
+
|
| 212 |
+
# Use fallback model if the requested one isn't available
|
| 213 |
+
if model_name not in florence_models:
|
| 214 |
+
model_name = list(florence_models.keys())[0]
|
| 215 |
+
print(f"Using fallback model: {model_name}")
|
| 216 |
+
|
| 217 |
image = Image.fromarray(image)
|
| 218 |
task_prompt = "<DESCRIPTION>"
|
| 219 |
prompt = task_prompt + "Describe this image in great detail."
|
|
|
|
| 400 |
)
|
| 401 |
|
| 402 |
# Florence 모델 선택 - 숨김 처리
|
| 403 |
+
available_models = list(florence_models.keys()) if florence_models else []
|
| 404 |
florence_model = gr.Dropdown(
|
| 405 |
+
choices=available_models,
|
| 406 |
label="Caption Model",
|
| 407 |
+
value=available_models[0] if available_models else None,
|
| 408 |
visible=False
|
| 409 |
)
|
| 410 |
|