File tree Expand file tree Collapse file tree
official/projects/waste_identification_ml/docker_solution/prediction_api Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2626
2727import io
2828import json
29+ import os
2930import fastapi
31+ import fastapi .security
3032import PIL
3133import tensorflow as tf , tf_keras
3234import uvicorn
3739
3840app = fastapi .FastAPI ()
3941model_manager = app_utils .ModelManager ()
42+ _api_key_header = fastapi .security .APIKeyHeader (name = 'X-API-Key' )
43+
44+
45+ def _verify_api_key (api_key : str = fastapi .Security (_api_key_header )):
46+ expected = os .environ .get ('PREDICTION_API_KEY' , '' )
47+ if not expected or api_key != expected :
48+ raise fastapi .HTTPException (status_code = 401 , detail = 'Invalid or missing API key' )
4049
4150
4251@app .on_event ('startup' )
@@ -47,6 +56,7 @@ def startup_event():
4756@app .post ('/predict' )
4857async def predict (
4958 image : fastapi .UploadFile = fastapi .File (default = None ),
59+ _ : None = fastapi .Depends (_verify_api_key ),
5060) -> fastapi .responses .JSONResponse :
5161 """Predicts objects in the uploaded image.
5262
You can’t perform that action at this time.
0 commit comments