gdleds commited on
Commit
368b63a
·
1 Parent(s): 4e47a55
Files changed (1) hide show
  1. appfast.py +48 -0
appfast.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import joblib
4
+ import boto3
5
+ import os
6
+ import io
7
+ import numpy as np
8
+ from dotenv import load_dotenv
9
+
10
+ # Charger les secrets (.env ou .secrets)
11
+ load_dotenv(dotenv_path='.secrets')
12
+
13
+ # Initialiser l'app FastAPI
14
+ app = FastAPI(title="GetAround Pricing API")
15
+
16
+ # Config S3
17
+ S3_BUCKET = os.getenv("S3_BUCKET")
18
+ MODEL_KEY = os.getenv("MODEL_KEY")
19
+
20
+ # Connexion S3
21
+ s3 = boto3.client(
22
+ "s3",
23
+ aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
24
+ aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY")
25
+ )
26
+
27
+ # Charger le modèle depuis S3
28
+ def load_model_from_s3(bucket, key):
29
+ print(f"Téléchargement du modèle depuis s3://{bucket}/{key}")
30
+ response = s3.get_object(Bucket=bucket, Key=key)
31
+ bytestream = io.BytesIO(response["Body"].read())
32
+ return joblib.load(bytestream)
33
+
34
+ model = load_model_from_s3(S3_BUCKET, MODEL_KEY)
35
+
36
+ # Définition du format d'entrée
37
+ class InputData(BaseModel):
38
+ input: list
39
+
40
+ @app.post("/predict")
41
+ def predict(data: InputData):
42
+ X = np.array(data.input)
43
+ preds = model.predict(X)
44
+ return {"prediction": preds.tolist()}
45
+
46
+ @app.get("/")
47
+ def home():
48
+ return {"message": "Bienvenue sur l'API GetAround Pricing! Utilisez /predict pour faire une prédiction."}