Spaces:
Running
Running
File size: 21,220 Bytes
30a44ac 6433658 30a44ac 29154b3 30a44ac 6433658 30a44ac 29154b3 30a44ac 6433658 30a44ac 6433658 30a44ac 29154b3 30a44ac 29154b3 30a44ac 6433658 30a44ac 6433658 30a44ac 6433658 30a44ac 6433658 30a44ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | # import os
# from flask import Flask
# from flask_cors import CORS
# from models.loader import warm_up
# from routes.predict import predict_bp
# from routes.health import health_bp
# def create_app() -> Flask:
# app = Flask(__name__)
# CORS(app, resources={r"/*": {"origins": "*"}})
# app.register_blueprint(predict_bp)
# app.register_blueprint(health_bp)
# return app
# app = create_app()
# # Warm up models once at startup (eliminates cold-start latency on first request)
# with app.app_context():
# warm_up()
# if __name__ == '__main__':
# port = int(os.environ.get("PORT", 7860))
# app.run(debug=False, host='0.0.0.0', port=port)
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
from ultralytics import YOLO
import cv2
import numpy as np
from groq import Groq
import threading
import torch
from torchvision import transforms, models
from PIL import Image
import json
import re
import time
import queue
app = Flask(__name__)
# Enable CORS for RapidAPI (allow all origins)
CORS(app, resources={r"/*": {"origins": "*"}})
# Configuration
MODEL_PATH = 'model_moncef.pt'
SKIN_TYPE_MODEL_PATH = 'skin_type_classification85.pth'
MAX_IMAGE_SIZE = 1024 # Maximum dimension for input images
GROQ_TIMEOUT = 8.0 # Timeout for Groq API calls in seconds
REQUEST_TIMEOUT = 30.0 # Overall request timeout in seconds
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB max file size
# Translation dictionaries
class_translations_en = {
'acne': 'Acne', 'blackheads': 'Blackheads', 'dark circles': 'Dark Circles',
'dark spot': 'Dark Spot', 'eyebags': 'Eye Bags', 'redness': 'Redness',
'whiteheads': 'Whiteheads', 'wrinkles': 'Wrinkles', 'Acne': 'Acne',
'Acne Scar': 'Acne Scar', 'Blackhead': 'Blackhead', 'Dark Circles': 'Dark Circles',
'Dark Spots': 'Dark Spots', 'Enlarged Pores': 'Enlarged Pores', 'Eyebags': 'Eye Bags',
'Freckle': 'Freckle', 'Melasma': 'Melasma', 'Nodules': 'Nodules',
'Papules': 'Papules', 'Pustules': 'Pustules', 'Skin Redness': 'Skin Redness',
'Sun Spots': 'Sun Spots', 'Vascular': 'Vascular Lesions', 'Whitehead': 'Whitehead',
'Wrinkles': 'Wrinkles'
}
class_translations_fr = {
'acne': 'Acné', 'blackheads': 'Points noirs', 'dark circles': 'Cernes',
'dark spot': 'Tache brune', 'eyebags': 'Poches sous les yeux', 'redness': 'Rougeurs',
'whiteheads': 'Points blancs', 'wrinkles': 'Rides', 'Acne': 'Acné',
'Acne Scar': 'Cicatrice d\'acné', 'Blackhead': 'Point noir', 'Dark Circles': 'Cernes',
'Dark Spots': 'Taches brunes', 'Enlarged Pores': 'Pores dilatés', 'Eyebags': 'Poches sous les yeux',
'Freckle': 'Tache de rousseur', 'Melasma': 'Mélasma', 'Nodules': 'Nodules',
'Papules': 'Papules', 'Pustules': 'Pustules', 'Skin Redness': 'Rougeurs',
'Sun Spots': 'Taches solaires', 'Vascular': 'Lésions vasculaires', 'Whitehead': 'Point blanc',
'Wrinkles': 'Rides'
}
skin_type_classes_en = {0: 'Dry Skin', 1: 'Normal Skin', 2: 'Oily Skin'}
skin_type_classes_fr = {0: 'Peau sèche', 1: 'Peau normale', 2: 'Peau grasse'}
# Initialize models
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")
# Load YOLO model
print("Loading YOLO model...")
model = YOLO(MODEL_PATH)
# Load skin type model
print("Loading skin type model...")
skin_type_model = models.resnet50(weights=None)
num_ftrs = skin_type_model.fc.in_features
skin_type_model.fc = torch.nn.Linear(num_ftrs, 3)
try:
checkpoint = torch.load(SKIN_TYPE_MODEL_PATH, map_location=device)
model_dict = skin_type_model.state_dict()
pretrained_dict = {}
for k, v in checkpoint.items():
if k in model_dict and v.shape == model_dict[k].shape:
pretrained_dict[k] = v
elif k == 'fc.weight' and model_dict['fc.weight'].shape[1] == v.shape[1]:
pretrained_dict['fc.weight'] = v[:3, :]
elif k == 'fc.bias' and len(v) >= 3:
pretrained_dict['fc.bias'] = v[:3]
skin_type_model.load_state_dict(pretrained_dict, strict=False)
print("Skin type model loaded successfully")
except Exception as e:
print(f"Error loading skin type model: {str(e)}")
skin_type_model = skin_type_model.to(device)
skin_type_model.eval()
# Image transform for skin type model
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
def optimize_image(img, max_size=MAX_IMAGE_SIZE):
"""Resize large images to improve processing speed"""
h, w = img.shape[:2]
if max(h, w) > max_size:
scale = max_size / max(h, w)
new_w, new_h = int(w * scale), int(h * scale)
img = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
return img
def detect_skin(image):
"""Detect skin regions in the image using HSV color space"""
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_skin = np.array([0, 20, 70], dtype=np.uint8)
upper_skin = np.array([20, 255, 255], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_skin, upper_skin)
skin_pixels = cv2.countNonZero(mask)
total_pixels = image.shape[0] * image.shape[1]
skin_ratio = skin_pixels / total_pixels
return skin_ratio > 0.05
def analyze_skin_issues_with_timeout(detections, language='en', timeout=GROQ_TIMEOUT):
"""Analyze skin issues with timeout protection"""
result = {}
exception = None
def worker():
nonlocal exception
try:
result['analysis'] = analyze_skin_issues(detections, language)
except Exception as e:
exception = e
thread = threading.Thread(target=worker)
thread.start()
thread.join(timeout)
if thread.is_alive():
print("Groq API call timed out")
return get_default_analysis(language, timeout=True)
if exception:
print(f"Groq API error: {str(exception)}")
return get_default_analysis(language)
return result.get('analysis', get_default_analysis(language))
def analyze_skin_issues(detections, language='en'):
"""Analyze skin issues using Groq LLM"""
groq_api_key = os.environ.get("GROQ_API_KEY")
if not groq_api_key:
return get_default_analysis(language, reason="API key missing")
client = Groq(api_key=groq_api_key)
problems = []
for detection in detections:
problems.append(f"{detection['class_name']} (confidence: {detection['confidence']:.2f})")
skin_issues = ", ".join(problems)
if language == 'fr':
prompt = f"""En tant que spécialiste en dermatologie, analyse les problèmes de peau suivants détectés par IA: {skin_issues}.
Réponds exactement dans ce format JSON:
{{
"conseils": "Liste de conseils précis et pratiques pour traiter ces problèmes spécifiques de peau",
"interpretation": "Explication claire des causes possibles et de la gravité des problèmes détectés",
"routine": "Proposition d'une routine quotidienne adaptée pour améliorer l'état de la peau. Termine toujours par la phrase: 'Consultez la section des produits recommandés ci-dessous pour voir les solutions adaptées à vos besoins spécifiques.'"
}}
Ne mets rien d'autre dans ta réponse que ce JSON correctement formaté."""
else:
prompt = f"""As a dermatology specialist, analyze the following skin problems detected by AI: {skin_issues}.
Respond exactly in this JSON format:
{{
"conseils": "List of precise and practical advice for treating these specific skin problems",
"interpretation": "Clear explanation of possible causes and severity of the detected problems",
"routine": "Proposal for a daily routine adapted to improve skin condition. Always end with the phrase: 'Check the recommended products section below to see solutions tailored to your specific needs.'"
}}
Put nothing else in your response except this properly formatted JSON."""
start_time = time.time()
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
model="llama-3.3-70b-versatile",
stream=False,
)
response_time = time.time() - start_time
print(f"Groq API response time: {response_time:.2f}s")
response_content = chat_completion.choices[0].message.content
# Try to parse JSON from the response
json_match = re.search(r'\{.*\}', response_content, re.DOTALL)
if json_match:
try:
return json.loads(json_match.group(0))
except json.JSONDecodeError:
pass
# Fallback parsing if JSON extraction fails
conseils = "Consult a dermatologist for personalized advice."
interpretation = "Professional analysis is recommended for these skin problems."
routine = "Follow a basic routine: cleansing, moisturizing, and daily sun protection."
if language == 'fr':
conseils = "Consultez un dermatologue pour des conseils personnalisés."
interpretation = "Une analyse professionnelle est recommandée pour ces problèmes de peau."
routine = "Suivez une routine de base: nettoyage, hydratation et protection solaire quotidienne."
return {
"conseils": conseils,
"interpretation": interpretation,
"routine": routine
}
def get_default_analysis(language='en', timeout=False, reason=None):
"""Return default analysis when Groq API fails or times out"""
if language == 'fr':
if timeout:
return {
"conseils": "L'analyse prend plus de temps que prévu. Veuillez réessayer plus tard.",
"interpretation": "Le système d'analyse IA est temporairement occupé.",
"routine": "Routine standard: nettoyer doucement, hydrater et protéger du soleil."
}
return {
"conseils": "Consultez un dermatologue pour des conseils personnalisés.",
"interpretation": "Une analyse professionnelle est recommandée pour ces problèmes de peau.",
"routine": "Suivez une routine de base: nettoyage, hydratation et protection solaire quotidienne."
}
else:
if timeout:
return {
"conseils": "Analysis is taking longer than expected. Please try again later.",
"interpretation": "The AI analysis system is temporarily busy.",
"routine": "Standard routine: gentle cleansing, moisturizing, and sun protection."
}
return {
"conseils": "Consult a dermatologist for personalized advice.",
"interpretation": "Professional analysis is recommended for these skin problems.",
"routine": "Follow a basic routine: cleansing, moisturizing, and daily sun protection."
}
def warm_up_models():
"""Pre-load models and run dummy inference to reduce cold start time"""
print("Warming up models...")
dummy_img = np.zeros((224, 224, 3), dtype=np.uint8)
# Warm up YOLO model
start_time = time.time()
model(dummy_img, conf=0.15, iou=0.2)
print(f"YOLO warm-up time: {time.time() - start_time:.2f}s")
# Warm up skin type model
start_time = time.time()
dummy_tensor = transform(dummy_img).unsqueeze(0).to(device)
with torch.no_grad():
skin_type_model(dummy_tensor)
print(f"Skin type model warm-up time: {time.time() - start_time:.2f}s")
print("Models warmed up and ready")
# Call the warm-up function immediately after model loading
with app.app_context():
warm_up_models()
# @app.before_request
# def check_rapidapi_key():
# if request.path.startswith('/predict'):
# rapidapi_key = (
# request.headers.get('X-RapidAPI-Key')
# or request.headers.get('x-rapidapi-key')
# or request.environ.get('HTTP_X_RAPIDAPI_KEY')
# )
# if not rapidapi_key:
# return jsonify({
# 'error': 'Missing X-RapidAPI-Key header',
# 'received_headers': dict(request.headers)
# }), 401
@app.route('/predict', methods=['POST'])
def predict():
"""Main prediction endpoint with timeout protection"""
try:
start_time = time.time()
print("Processing new request")
# Get language parameter, default to English
language = request.args.get('lang', 'en').lower()
if language not in ['en', 'fr']:
language = 'en'
class_translations = class_translations_fr if language == 'fr' else class_translations_en
skin_type_classes = skin_type_classes_fr if language == 'fr' else skin_type_classes_en
# Validate file upload
if 'file' not in request.files:
return jsonify({'error': 'No file found'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
# Check file size
file_bytes = file.read()
if len(file_bytes) > MAX_FILE_SIZE:
return jsonify({'error': f'File too large. Maximum size is {MAX_FILE_SIZE//1024//1024}MB'}), 400
# Decode image
nparr = np.frombuffer(file_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None:
return jsonify({'error': 'Invalid image format'}), 400
# Optimize image size
original_shape = img.shape
img = optimize_image(img, MAX_IMAGE_SIZE)
new_shape = img.shape
if original_shape != new_shape:
print(f"Resized image from {original_shape} to {new_shape}")
# Check for skin in image
if not detect_skin(img):
return jsonify({'error': 'No skin detected in the image'}), 400
# Use a queue to handle the result with timeout
result_queue = queue.Queue()
def process_request():
try:
# Run inference in parallel threads
detections = []
skin_type_result = {}
errors = {}
def run_yolo():
nonlocal detections
try:
start = time.time()
results = model(img, conf=0.15, iou=0.2)[0]
for box in results.boxes:
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy().astype(int)
confidence = float(box.conf[0])
class_id = int(box.cls[0])
class_name = results.names[class_id]
translated_name = class_translations.get(class_name, class_name)
detections.append({
'class_id': class_id,
'class_name': translated_name,
'confidence': confidence,
'bbox': [int(x1), int(y1), int(x2), int(y2)]
})
print(f"YOLO inference time: {time.time() - start:.2f}s, found {len(detections)} issues")
except Exception as e:
errors['yolo'] = str(e)
print(f"YOLO error: {str(e)}")
def run_skin_type():
nonlocal skin_type_result
try:
start = time.time()
pil_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
img_tensor = transform(np.array(pil_image)).unsqueeze(0).to(device)
with torch.no_grad():
output = skin_type_model(img_tensor)
probabilities = torch.nn.functional.softmax(output, dim=1)[0]
predicted_class = output.argmax(1).item()
skin_type_result = {
'skin_type': skin_type_classes[predicted_class],
'probabilities': {
'dry': float(probabilities[0] * 100),
'normal': float(probabilities[1] * 100),
'oily': float(probabilities[2] * 100)
}
}
print(f"Skin type inference time: {time.time() - start:.2f}s")
except Exception as e:
errors['skin_type'] = str(e)
print(f"Skin type model error: {str(e)}")
# Start threads
yolo_thread = threading.Thread(target=run_yolo)
skin_thread = threading.Thread(target=run_skin_type)
yolo_thread.start()
skin_thread.start()
# Wait for threads with timeout
yolo_thread.join(timeout=15.0)
skin_thread.join(timeout=15.0)
# Check if threads completed successfully
if 'yolo' in errors or len(detections) == 0:
print("YOLO inference failed or timed out")
detections = [] # Reset to empty list
if 'skin_type' in errors or not skin_type_result:
print("Skin type inference failed or timed out")
skin_type_result = {
'skin_type': skin_type_classes[1], # Default to normal skin
'probabilities': {'dry': 0.0, 'normal': 100.0, 'oily': 0.0}
}
# Get AI analysis with timeout protection
ai_analysis_start = time.time()
ai_analysis = analyze_skin_issues_with_timeout(detections, language)
print(f"AI analysis time: {time.time() - ai_analysis_start:.2f}s")
# Prepare response
response_data = {
'success': True,
'detections': detections,
'ai_analysis': ai_analysis,
'skin_type': skin_type_result['skin_type'],
'skin_type_probabilities': skin_type_result['probabilities'],
'processing_time': round(time.time() - start_time, 2),
'image_size': {'width': new_shape[1], 'height': new_shape[0]},
'disclaimer': "This analysis is not a medical diagnosis. For persistent issues, consult a dermatologist."
}
result_queue.put(('success', response_data))
except Exception as e:
result_queue.put(('error', str(e)))
# Start processing in a separate thread
processing_thread = threading.Thread(target=process_request)
processing_thread.start()
# Wait for the result with timeout
try:
status, result = result_queue.get(timeout=REQUEST_TIMEOUT)
if status == 'success':
total_time = time.time() - start_time
print(f"Total processing time: {total_time:.2f}s")
return jsonify(result)
else:
print(f"Processing error: {result}")
return jsonify({'error': 'Internal processing error', 'message': result}), 500
except queue.Empty:
# Timeout occurred
print(f"Request timed out after {REQUEST_TIMEOUT} seconds")
return jsonify({
'error': 'Request timed out. Please try a smaller image or try again later.',
'suggestion': 'Try uploading a smaller image (under 1MB) for faster processing'
}), 504
except Exception as e:
print(f"Unexpected error: {str(e)}")
return jsonify({
'error': 'Internal server error',
'message': str(e)
}), 500
@app.route('/ping', methods=['GET'])
def ping():
"""Health check endpoint that returns simple status"""
return jsonify({
'status': 'healthy',
'message': 'Skin Analysis API is running',
'timestamp': time.time(),
'version': '1.0.0',
'uptime': 'N/A' # You can calculate uptime if needed
}), 200
@app.route('/health', methods=['GET'])
def health():
"""Health check endpoint"""
return jsonify({
'status': 'API online',
'timestamp': time.time(),
'device': str(device),
'model_status': 'loaded',
'disclaimer': "This is a non-medical analysis tool. Consult a dermatologist for medical advice."
})
if __name__ == '__main__':
Port = os.environ.get("Port")
app.run(debug=False, host='0.0.0.0', port=Port)
|