Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -289,7 +289,7 @@ def analyze_outfit(input_img):
|
|
| 289 |
# Continue without fashion model input
|
| 290 |
|
| 291 |
|
| 292 |
-
|
| 293 |
clip_detected_item_prob = 0.0
|
| 294 |
category_key = 'mid' # Default category
|
| 295 |
final_score_str = "N/A"
|
|
@@ -324,7 +324,7 @@ def analyze_outfit(input_img):
|
|
| 324 |
|
| 325 |
raw_weighted_score = (drip_score * 1) + (mid_score * 0.5) + (not_score * 0.1)
|
| 326 |
|
| 327 |
-
final_score = raw_weighted_score * 100
|
| 328 |
|
| 329 |
|
| 330 |
final_score = min(max(final_score, 0), 100)
|
|
@@ -369,97 +369,7 @@ def analyze_outfit(input_img):
|
|
| 369 |
|
| 370 |
return ("<p style='color: #FF5555;'>Error during CLIP analysis.</p>",
|
| 371 |
None, f"Analysis Error: {e}")
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
# --- Tunable Scoring Parameters ---
|
| 375 |
-
# Adjust these weights to control how much each category's average probability contributes
|
| 376 |
-
# A negative weight for trash means a high trash probability reduces the score.
|
| 377 |
-
W_DRIP = 100.0
|
| 378 |
-
W_MID = 40.0 # Reduced weight for mid compared to drip
|
| 379 |
-
W_TRASH = -60.0 # Negative weight for trash, making it a penalty
|
| 380 |
-
|
| 381 |
-
# Adjust these thresholds based on the final scores you observe for different outfits
|
| 382 |
-
THRESHOLD_DRIP = 65.0 # Score >= this is Drippy
|
| 383 |
-
THRESHOLD_MID = 30.0 # Score >= this is Mid (and < THRESHOLD_DRIP)
|
| 384 |
-
# Scores below THRESHOLD_MID will be Trash
|
| 385 |
-
|
| 386 |
-
# --- Initialize Variables ---
|
| 387 |
-
clip_detected_item = "look" # Default fallback item name
|
| 388 |
-
clip_detected_item_prob = 0.0
|
| 389 |
-
final_score = 0.0 # Initialize score
|
| 390 |
-
category_key = 'not_drippy' # Default category before calculation (can be anything, overridden later)
|
| 391 |
-
score_label = "Score" # Default label before calculation
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
try:
|
| 395 |
-
# --- CLIP Processing ---
|
| 396 |
-
image_tensor = clip_preprocess(cropped_img).unsqueeze(0).to(DEVICE)
|
| 397 |
-
text_tokens = clip.tokenize(all_prompts).to(DEVICE)
|
| 398 |
-
|
| 399 |
-
with torch.no_grad():
|
| 400 |
-
logits, _ = clip_model(image_tensor, text_tokens)
|
| 401 |
-
all_probs = logits.softmax(dim=-1).cpu().numpy()[0]
|
| 402 |
-
|
| 403 |
-
# --- Calculate Average Probabilities per Category ---
|
| 404 |
-
drip_len = len(style_prompts['drippy'])
|
| 405 |
-
mid_len = len(style_prompts['mid'])
|
| 406 |
-
# Calculate average probabilities for each category
|
| 407 |
-
drip_avg_prob = np.mean(all_probs[0 : drip_len])
|
| 408 |
-
mid_avg_prob = np.mean(all_probs[drip_len : drip_len + mid_len])
|
| 409 |
-
not_avg_prob = np.mean(all_probs[drip_len + mid_len : style_prompts_end_index])
|
| 410 |
-
|
| 411 |
-
# --- Calculate Weighted Score (directly scaled to ~0-100) ---
|
| 412 |
-
# Multiply average probabilities by tunable weights
|
| 413 |
-
weighted_score = (drip_avg_prob * W_DRIP) + (mid_avg_prob * W_MID) + (not_avg_prob * W_TRASH)
|
| 414 |
-
|
| 415 |
-
# --- Final Score & Clipping ---
|
| 416 |
-
# Clip the score to ensure it's within the 0-100 range
|
| 417 |
-
final_score = min(max(weighted_score, 0.0), 100.0) # Ensures score is between 0 and 100
|
| 418 |
-
|
| 419 |
-
# --- Determine Category based on Tunable Thresholds ---
|
| 420 |
-
if final_score >= THRESHOLD_DRIP:
|
| 421 |
-
category_key = 'drippy'
|
| 422 |
-
score_label = "Drip Score"
|
| 423 |
-
elif final_score >= THRESHOLD_MID:
|
| 424 |
-
category_key = 'mid'
|
| 425 |
-
score_label = "Mid Score"
|
| 426 |
-
else:
|
| 427 |
-
category_key = 'not_drippy'
|
| 428 |
-
score_label = "Trash Score"
|
| 429 |
-
|
| 430 |
-
# --- Format Output Strings ---
|
| 431 |
-
category_label = CATEGORY_LABEL_MAP[category_key]
|
| 432 |
-
percentage_score_str = f"{final_score:.0f}" # Format as whole number (e.g., "75")
|
| 433 |
-
# The "/100%" is added in the HTML string below
|
| 434 |
-
|
| 435 |
-
# --- Print Debug Info ---
|
| 436 |
-
print(f"Avg Probs: Drip={drip_avg_prob:.4f}, Mid={mid_avg_prob:.4f}, Trash={not_avg_prob:.4f}")
|
| 437 |
-
print(f"Weighted Score before clip: {weighted_score:.4f}")
|
| 438 |
-
print(f"Style analysis: Category={category_label}, Score = {score_label}={percentage_score_str}/100 (Final Score: {final_score:.4f})")
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
# --- Get top clothing item from CLIP ---
|
| 442 |
-
# (This part remains largely the same, make sure you copy from here down to the end of the try block)
|
| 443 |
-
top_3_clip_items = get_top_clip_clothing(all_probs, n=3)
|
| 444 |
-
|
| 445 |
-
if top_3_clip_items:
|
| 446 |
-
detected_items_str = ", ".join([f"{item[0]} ({item[1]*100:.1f}%)" for item in top_3_clip_items]) # Show item and probability
|
| 447 |
-
print(f"I think I detected: {detected_items_str}")
|
| 448 |
-
clip_detected_item, clip_detected_item_prob = top_3_clip_items[0]
|
| 449 |
-
else:
|
| 450 |
-
print("I couldn't confidently identify specific clothing items via CLIP.")
|
| 451 |
-
clip_detected_item = "piece" # Use a different fallback if CLIP fails
|
| 452 |
-
clip_detected_item_prob = 0.0 # Ensure prob is defined
|
| 453 |
-
|
| 454 |
-
except Exception as e:
|
| 455 |
-
print(f"Error during CLIP analysis: {e}")
|
| 456 |
-
# Keep existing error return format
|
| 457 |
-
return ("<p style='color: #FF5555;'>Error during CLIP analysis.</p>",
|
| 458 |
-
None, f"Analysis Error: {e}")
|
| 459 |
-
|
| 460 |
-
# --- Determine the Final Item to Mention in Response ---
|
| 461 |
-
# (This block remains the same after the try block)
|
| 462 |
-
# ... keep the rest of your analyze_outfit function from here ...
|
| 463 |
# Determine the Final Item to Mention in Response
|
| 464 |
final_clothing_item = "style" # Ultimate fallback generic term
|
| 465 |
generic_response_needed = False
|
|
|
|
| 289 |
# Continue without fashion model input
|
| 290 |
|
| 291 |
|
| 292 |
+
clip_detected_item = "look" # Default fallback item name
|
| 293 |
clip_detected_item_prob = 0.0
|
| 294 |
category_key = 'mid' # Default category
|
| 295 |
final_score_str = "N/A"
|
|
|
|
| 324 |
|
| 325 |
raw_weighted_score = (drip_score * 1) + (mid_score * 0.5) + (not_score * 0.1)
|
| 326 |
|
| 327 |
+
final_score = raw_weighted_score * 100 + 10
|
| 328 |
|
| 329 |
|
| 330 |
final_score = min(max(final_score, 0), 100)
|
|
|
|
| 369 |
|
| 370 |
return ("<p style='color: #FF5555;'>Error during CLIP analysis.</p>",
|
| 371 |
None, f"Analysis Error: {e}")
|
| 372 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
# Determine the Final Item to Mention in Response
|
| 374 |
final_clothing_item = "style" # Ultimate fallback generic term
|
| 375 |
generic_response_needed = False
|