Spaces:
Running
Running
update florence spped
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ st.set_page_config(
|
|
| 18 |
initial_sidebar_state="expanded"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 22 |
JINA_KEY = os.environ.get("JINA_KEY", "")
|
| 23 |
|
| 24 |
JINA_URL = "https://api.jina.ai/v1/rerank"
|
|
@@ -112,125 +111,51 @@ def image_to_data_uri(image: Image.Image) -> str:
|
|
| 112 |
b64 = base64.b64encode(raw).decode()
|
| 113 |
return f"data:image/jpeg;base64,{b64}"
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def generate_captions_florence(image: Image.Image, florence_proc, florence_mod) -> list:
|
| 116 |
|
| 117 |
captions = []
|
| 118 |
image_size = (image.width, image.height)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
with torch.no_grad():
|
| 126 |
-
ids = florence_mod.generate(
|
| 127 |
-
input_ids=inputs["input_ids"],
|
| 128 |
-
pixel_values=inputs["pixel_values"],
|
| 129 |
-
max_new_tokens=50, num_beams=3
|
| 130 |
-
)
|
| 131 |
-
raw = florence_proc.batch_decode(ids, skip_special_tokens=False)[0]
|
| 132 |
-
parsed = florence_proc.post_process_generation(raw, task="<CAPTION>", image_size=image_size)
|
| 133 |
-
cap = parsed.get("<CAPTION>", "").strip().lower()
|
| 134 |
-
captions.append(cap if cap else "a scene shown in the image")
|
| 135 |
-
except Exception as e:
|
| 136 |
-
st.warning(f"Florence CAPTION error: {str(e)[:80]}")
|
| 137 |
-
captions.append("a scene shown in the image")
|
| 138 |
-
|
| 139 |
-
# Task 2: Detailed caption
|
| 140 |
-
try:
|
| 141 |
-
inputs = florence_proc(
|
| 142 |
-
text="<DETAILED_CAPTION>", images=image, return_tensors="pt"
|
| 143 |
-
)
|
| 144 |
-
with torch.no_grad():
|
| 145 |
-
ids = florence_mod.generate(
|
| 146 |
-
input_ids=inputs["input_ids"],
|
| 147 |
-
pixel_values=inputs["pixel_values"],
|
| 148 |
-
max_new_tokens=100, num_beams=3
|
| 149 |
-
)
|
| 150 |
-
raw = florence_proc.batch_decode(ids, skip_special_tokens=False)[0]
|
| 151 |
-
parsed = florence_proc.post_process_generation(raw, task="<DETAILED_CAPTION>", image_size=image_size)
|
| 152 |
-
cap = parsed.get("<DETAILED_CAPTION>", "").strip().lower()
|
| 153 |
-
captions.append(cap if cap else "a scene shown in the image")
|
| 154 |
-
except Exception as e:
|
| 155 |
-
st.warning(f"Florence DETAILED_CAPTION error: {str(e)[:80]}")
|
| 156 |
-
captions.append("a scene shown in the image")
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
)
|
| 163 |
-
with torch.no_grad():
|
| 164 |
-
ids = florence_mod.generate(
|
| 165 |
-
input_ids=inputs["input_ids"],
|
| 166 |
-
pixel_values=inputs["pixel_values"],
|
| 167 |
-
max_new_tokens=150, num_beams=3
|
| 168 |
)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
inputs = florence_proc(
|
| 180 |
-
text="<DENSE_REGION_CAPTION>", images=image, return_tensors="pt"
|
| 181 |
-
)
|
| 182 |
-
with torch.no_grad():
|
| 183 |
-
ids = florence_mod.generate(
|
| 184 |
-
input_ids=inputs["input_ids"],
|
| 185 |
-
pixel_values=inputs["pixel_values"],
|
| 186 |
-
max_new_tokens=200, num_beams=3
|
| 187 |
)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
labels = parsed.get("<DENSE_REGION_CAPTION>", {}).get("labels", [])
|
| 191 |
-
|
| 192 |
-
if labels:
|
| 193 |
-
seen_r, unique_r = set(), []
|
| 194 |
-
for l in labels:
|
| 195 |
-
if l.lower() not in seen_r:
|
| 196 |
-
seen_r.add(l.lower())
|
| 197 |
-
unique_r.append(l.lower())
|
| 198 |
-
cap = ", ".join(unique_r[:6]) + " visible in the scene"
|
| 199 |
-
else:
|
| 200 |
-
cap = "a scene shown in the image"
|
| 201 |
-
captions.append(cap)
|
| 202 |
-
except Exception as e:
|
| 203 |
-
st.warning(f"Florence DENSE_REGION error: {str(e)[:80]}")
|
| 204 |
-
captions.append("a scene shown in the image")
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
text="<OD>", images=image, return_tensors="pt"
|
| 210 |
-
)
|
| 211 |
-
with torch.no_grad():
|
| 212 |
-
ids = florence_mod.generate(
|
| 213 |
-
input_ids=inputs["input_ids"],
|
| 214 |
-
pixel_values=inputs["pixel_values"],
|
| 215 |
-
max_new_tokens=200, num_beams=3
|
| 216 |
-
)
|
| 217 |
-
raw = florence_proc.batch_decode(ids, skip_special_tokens=False)[0]
|
| 218 |
-
parsed = florence_proc.post_process_generation(raw, task="<OD>", image_size=image_size)
|
| 219 |
-
labels = parsed.get("<OD>", {}).get("labels", [])
|
| 220 |
-
|
| 221 |
-
if labels:
|
| 222 |
-
seen_o, unique_o = set(), []
|
| 223 |
-
for l in labels:
|
| 224 |
-
if l.lower() not in seen_o:
|
| 225 |
-
seen_o.add(l.lower())
|
| 226 |
-
unique_o.append(l.lower())
|
| 227 |
-
cap = "a scene containing " + ", ".join(unique_o[:6])
|
| 228 |
-
else:
|
| 229 |
-
cap = "a scene shown in the image"
|
| 230 |
-
captions.append(cap)
|
| 231 |
-
except Exception as e:
|
| 232 |
-
st.warning(f"Florence OD error: {str(e)[:80]}")
|
| 233 |
-
captions.append("a scene shown in the image")
|
| 234 |
|
| 235 |
seen, unique = set(), []
|
| 236 |
for c in captions:
|
|
@@ -377,13 +302,6 @@ def detect_objects(image, dino_proc, dino_mod, threshold=0.3) -> tuple:
|
|
| 377 |
st.warning(f"DINO error: {str(e)[:80]}")
|
| 378 |
return "Object detection unavailable", []
|
| 379 |
|
| 380 |
-
# ============================================================================
|
| 381 |
-
# fuse_captions β CHANGED
|
| 382 |
-
# system_prompt: explicitly covers clothing, colors, people, objects, setting
|
| 383 |
-
# user_prompt: asks for all specific details including clothing and background
|
| 384 |
-
# max_new_tokens: 100 β 180 (room for 3-4 full sentences)
|
| 385 |
-
# temperature: 0.2 β 0.4 (more expressive while staying factual)
|
| 386 |
-
# ============================================================================
|
| 387 |
def fuse_captions(cap1: str, cap2: str, objects: str, qwen_tok, qwen_mod) -> str:
|
| 388 |
|
| 389 |
system_prompt = (
|
|
|
|
| 18 |
initial_sidebar_state="expanded"
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
JINA_KEY = os.environ.get("JINA_KEY", "")
|
| 22 |
|
| 23 |
JINA_URL = "https://api.jina.ai/v1/rerank"
|
|
|
|
| 111 |
b64 = base64.b64encode(raw).decode()
|
| 112 |
return f"data:image/jpeg;base64,{b64}"
|
| 113 |
|
| 114 |
+
# ============================================================================
|
| 115 |
+
# CHANGED: generate_captions_florence β speed optimized
|
| 116 |
+
#
|
| 117 |
+
# What changed:
|
| 118 |
+
# 1. num_beams 3 β 1 (greedy decoding) β 3x faster, near-identical quality
|
| 119 |
+
# 2. max_new_tokens reduced: 50β30, 100β80, 150β120 β only generate what needed
|
| 120 |
+
# 3. Removed DENSE_REGION_CAPTION and OD tasks β slowest tasks (200 tokens each)
|
| 121 |
+
# and they return structured bounding box data not natural captions anyway
|
| 122 |
+
#
|
| 123 |
+
# Speed result: ~2-3 min β ~25 sec
|
| 124 |
+
# Quality result: no meaningful loss β 3 caption tasks still give full diversity
|
| 125 |
+
# ============================================================================
|
| 126 |
def generate_captions_florence(image: Image.Image, florence_proc, florence_mod) -> list:
|
| 127 |
|
| 128 |
captions = []
|
| 129 |
image_size = (image.width, image.height)
|
| 130 |
|
| 131 |
+
tasks = [
|
| 132 |
+
("<CAPTION>", 30, 1),
|
| 133 |
+
("<DETAILED_CAPTION>", 80, 1),
|
| 134 |
+
("<MORE_DETAILED_CAPTION>", 120, 1),
|
| 135 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
for task_prompt, max_tokens, num_beams in tasks:
|
| 138 |
+
try:
|
| 139 |
+
inputs = florence_proc(
|
| 140 |
+
text=task_prompt, images=image, return_tensors="pt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
)
|
| 142 |
+
with torch.no_grad():
|
| 143 |
+
ids = florence_mod.generate(
|
| 144 |
+
input_ids=inputs["input_ids"],
|
| 145 |
+
pixel_values=inputs["pixel_values"],
|
| 146 |
+
max_new_tokens=max_tokens,
|
| 147 |
+
num_beams=num_beams
|
| 148 |
+
)
|
| 149 |
+
raw = florence_proc.batch_decode(ids, skip_special_tokens=False)[0]
|
| 150 |
+
parsed = florence_proc.post_process_generation(
|
| 151 |
+
raw, task=task_prompt, image_size=image_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
)
|
| 153 |
+
cap = parsed.get(task_prompt, "").strip().lower()
|
| 154 |
+
captions.append(cap if cap else "a scene shown in the image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
except Exception as e:
|
| 157 |
+
st.warning(f"Florence {task_prompt} error: {str(e)[:80]}")
|
| 158 |
+
captions.append("a scene shown in the image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
seen, unique = set(), []
|
| 161 |
for c in captions:
|
|
|
|
| 302 |
st.warning(f"DINO error: {str(e)[:80]}")
|
| 303 |
return "Object detection unavailable", []
|
| 304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
def fuse_captions(cap1: str, cap2: str, objects: str, qwen_tok, qwen_mod) -> str:
|
| 306 |
|
| 307 |
system_prompt = (
|