somoncef commited on
Commit
24743d2
·
1 Parent(s): 01c810c

feat : API compatible with RapidAPI

Browse files
Files changed (1) hide show
  1. app.py +16 -4
app.py CHANGED
@@ -15,7 +15,9 @@ import time
15
  import queue
16
 
17
  app = Flask(__name__)
18
- CORS(app) # Allow all origins for RapidAPI compatibility
 
 
19
 
20
  # Configuration
21
  MODEL_PATH = 'model_moncef.pt'
@@ -263,6 +265,14 @@ def warm_up_models():
263
  with app.app_context():
264
  warm_up_models()
265
 
 
 
 
 
 
 
 
 
266
  @app.route('/predict', methods=['POST'])
267
  def predict():
268
  """Main prediction endpoint with timeout protection"""
@@ -402,7 +412,8 @@ def predict():
402
  'skin_type': skin_type_result['skin_type'],
403
  'skin_type_probabilities': skin_type_result['probabilities'],
404
  'processing_time': round(time.time() - start_time, 2),
405
- 'image_size': {'width': new_shape[1], 'height': new_shape[0]}
 
406
  }
407
 
408
  result_queue.put(('success', response_data))
@@ -439,13 +450,14 @@ def predict():
439
  }), 500
440
 
441
  @app.route('/health', methods=['GET'])
442
- def health():
443
  """Health check endpoint"""
444
  return jsonify({
445
  'status': 'API online',
446
  'timestamp': time.time(),
447
  'device': str(device),
448
- 'model_status': 'loaded'
 
449
  })
450
 
451
  if __name__ == '__main__':
 
15
  import queue
16
 
17
  app = Flask(__name__)
18
+
19
+ # Enable CORS for RapidAPI (allow all origins)
20
+ CORS(app, resources={r"/*": {"origins": "*"}})
21
 
22
  # Configuration
23
  MODEL_PATH = 'model_moncef.pt'
 
265
  with app.app_context():
266
  warm_up_models()
267
 
268
+ @app.before_request
269
+ def check_rapidapi_key():
270
+ """Verify RapidAPI key for security"""
271
+ if request.path == '/predict':
272
+ rapidapi_key = request.headers.get('X-RapidAPI-Key')
273
+ if not rapidapi_key:
274
+ return jsonify({'error': 'Missing X-RapidAPI-Key header'}), 401
275
+
276
  @app.route('/predict', methods=['POST'])
277
  def predict():
278
  """Main prediction endpoint with timeout protection"""
 
412
  'skin_type': skin_type_result['skin_type'],
413
  'skin_type_probabilities': skin_type_result['probabilities'],
414
  'processing_time': round(time.time() - start_time, 2),
415
+ 'image_size': {'width': new_shape[1], 'height': new_shape[0]},
416
+ 'disclaimer': "This analysis is not a medical diagnosis. For persistent issues, consult a dermatologist."
417
  }
418
 
419
  result_queue.put(('success', response_data))
 
450
  }), 500
451
 
452
  @app.route('/health', methods=['GET'])
453
+ def health():
454
  """Health check endpoint"""
455
  return jsonify({
456
  'status': 'API online',
457
  'timestamp': time.time(),
458
  'device': str(device),
459
+ 'model_status': 'loaded',
460
+ 'disclaimer': "This is a non-medical analysis tool. Consult a dermatologist for medical advice."
461
  })
462
 
463
  if __name__ == '__main__':