Spaces:
Sleeping
Sleeping
Update analysis.py
Browse files- analysis.py +17 -16
analysis.py
CHANGED
|
@@ -17,10 +17,10 @@ from mimetypes import guess_type
|
|
| 17 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 18 |
MODEL_COMBINED = "models/gemini-2.5-flash"
|
| 19 |
|
| 20 |
-
# Consistency settings
|
| 21 |
-
ENABLE_MULTI_PASS =
|
| 22 |
-
VALIDATION_PASSES =
|
| 23 |
-
MAX_VARIANCE_THRESHOLD = 0.15
|
| 24 |
|
| 25 |
_analysis_cache = {}
|
| 26 |
_usage_log = []
|
|
@@ -269,9 +269,9 @@ Return STRICT JSON with ALL these fields (use exact field names):
|
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
-
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 273 |
ACNE ANALYSIS - OBJECTIVE COUNTING CRITERIA (CRITICAL FOR CONSISTENCY)
|
| 274 |
-
ββββββββββββββββββββββββ
|
| 275 |
|
| 276 |
**ACTIVE_ACNE** - Count visible inflamed red/pink lesions (pustules, papules):
|
| 277 |
β’ 0.00-0.15: 0 lesions (clear skin)
|
|
@@ -405,7 +405,6 @@ def _perform_single_analysis(image_path: str) -> dict:
|
|
| 405 |
"temperature": 0,
|
| 406 |
"top_p": 1,
|
| 407 |
"top_k": 1,
|
| 408 |
-
# Add seed if/when supported: "seed": 42
|
| 409 |
}
|
| 410 |
)
|
| 411 |
elapsed = time.time() - start_time
|
|
@@ -487,9 +486,7 @@ def analyze_skin_complete(
|
|
| 487 |
results.append(result)
|
| 488 |
print(f" β Pass {i+1}/{VALIDATION_PASSES} completed")
|
| 489 |
|
| 490 |
-
#
|
| 491 |
-
if i < VALIDATION_PASSES - 1:
|
| 492 |
-
time.sleep(0.5)
|
| 493 |
|
| 494 |
except Exception as e:
|
| 495 |
print(f" β οΈ Pass {i+1} failed: {e}")
|
|
@@ -513,7 +510,7 @@ def analyze_skin_complete(
|
|
| 513 |
final_result = average_analyses(results)
|
| 514 |
|
| 515 |
else:
|
| 516 |
-
# Single-pass analysis
|
| 517 |
try:
|
| 518 |
final_result = retry_with_backoff(_call, max_retries=max_retries)
|
| 519 |
except Exception as e:
|
|
@@ -702,12 +699,16 @@ def build_detected_text(category, severity):
|
|
| 702 |
# =========================
|
| 703 |
# HIGH-LEVEL ANALYSIS WRAPPER
|
| 704 |
# =========================
|
| 705 |
-
def get_comprehensive_analysis(image_path):
|
| 706 |
"""
|
| 707 |
Get comprehensive skin analysis with all scores and metadata.
|
| 708 |
This is the main entry point called by the Flask API.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
"""
|
| 710 |
-
raw = analyze_skin_complete(image_path)
|
| 711 |
if not raw:
|
| 712 |
return None
|
| 713 |
|
|
@@ -771,8 +772,8 @@ def get_comprehensive_analysis(image_path):
|
|
| 771 |
"metadata": {
|
| 772 |
"analyzed_at": datetime.now().isoformat(),
|
| 773 |
"model_used": MODEL_COMBINED,
|
| 774 |
-
"multipass_enabled": ENABLE_MULTI_PASS,
|
| 775 |
-
"validation_passes": VALIDATION_PASSES if ENABLE_MULTI_PASS else 1
|
| 776 |
}
|
| 777 |
}
|
| 778 |
|
|
@@ -860,4 +861,4 @@ def clear_cache():
|
|
| 860 |
"""Clear the analysis cache."""
|
| 861 |
global _analysis_cache
|
| 862 |
_analysis_cache = {}
|
| 863 |
-
print("β Analysis cache cleared")
|
|
|
|
| 17 |
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 18 |
MODEL_COMBINED = "models/gemini-2.5-flash"
|
| 19 |
|
| 20 |
+
# Consistency settings - OPTIMIZED FOR SPEED
|
| 21 |
+
ENABLE_MULTI_PASS = False # β
DISABLED by default for fast responses
|
| 22 |
+
VALIDATION_PASSES = 2 # Reduced from 3 to 2 (if enabled)
|
| 23 |
+
MAX_VARIANCE_THRESHOLD = 0.15
|
| 24 |
|
| 25 |
_analysis_cache = {}
|
| 26 |
_usage_log = []
|
|
|
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββοΏ½οΏ½β
|
| 273 |
ACNE ANALYSIS - OBJECTIVE COUNTING CRITERIA (CRITICAL FOR CONSISTENCY)
|
| 274 |
+
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 275 |
|
| 276 |
**ACTIVE_ACNE** - Count visible inflamed red/pink lesions (pustules, papules):
|
| 277 |
β’ 0.00-0.15: 0 lesions (clear skin)
|
|
|
|
| 405 |
"temperature": 0,
|
| 406 |
"top_p": 1,
|
| 407 |
"top_k": 1,
|
|
|
|
| 408 |
}
|
| 409 |
)
|
| 410 |
elapsed = time.time() - start_time
|
|
|
|
| 486 |
results.append(result)
|
| 487 |
print(f" β Pass {i+1}/{VALIDATION_PASSES} completed")
|
| 488 |
|
| 489 |
+
# NO DELAY - Gemini API doesn't need it
|
|
|
|
|
|
|
| 490 |
|
| 491 |
except Exception as e:
|
| 492 |
print(f" β οΈ Pass {i+1} failed: {e}")
|
|
|
|
| 510 |
final_result = average_analyses(results)
|
| 511 |
|
| 512 |
else:
|
| 513 |
+
# Single-pass analysis (FAST)
|
| 514 |
try:
|
| 515 |
final_result = retry_with_backoff(_call, max_retries=max_retries)
|
| 516 |
except Exception as e:
|
|
|
|
| 699 |
# =========================
|
| 700 |
# HIGH-LEVEL ANALYSIS WRAPPER
|
| 701 |
# =========================
|
| 702 |
+
def get_comprehensive_analysis(image_path, enable_multipass=None):
|
| 703 |
"""
|
| 704 |
Get comprehensive skin analysis with all scores and metadata.
|
| 705 |
This is the main entry point called by the Flask API.
|
| 706 |
+
|
| 707 |
+
Args:
|
| 708 |
+
image_path: Path to the image file
|
| 709 |
+
enable_multipass: Optional override for multi-pass validation
|
| 710 |
"""
|
| 711 |
+
raw = analyze_skin_complete(image_path, enable_multipass=enable_multipass)
|
| 712 |
if not raw:
|
| 713 |
return None
|
| 714 |
|
|
|
|
| 772 |
"metadata": {
|
| 773 |
"analyzed_at": datetime.now().isoformat(),
|
| 774 |
"model_used": MODEL_COMBINED,
|
| 775 |
+
"multipass_enabled": enable_multipass if enable_multipass is not None else ENABLE_MULTI_PASS,
|
| 776 |
+
"validation_passes": VALIDATION_PASSES if (enable_multipass if enable_multipass is not None else ENABLE_MULTI_PASS) else 1
|
| 777 |
}
|
| 778 |
}
|
| 779 |
|
|
|
|
| 861 |
"""Clear the analysis cache."""
|
| 862 |
global _analysis_cache
|
| 863 |
_analysis_cache = {}
|
| 864 |
+
print("β Analysis cache cleared")
|