Spaces:
Running
Running
Download app.py from RabiHmedtak/API_RapidAPI_test: direct link, hf CLI and curl.
- Browser
- Download file 21.2 kB
-
https://huggingface.co/spaces/RabiHmedtak/API_RapidAPI_test/resolve/main/app.py
- Command line
-
hf download hf://spaces/RabiHmedtak/API_RapidAPI_test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/RabiHmedtak/API_RapidAPI_test/resolve/main/app.py
21.2 kB
| # 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 | |
| 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 | |
| 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 | |
| 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) | |