aroussya commited on
Commit
b1899bd
·
0 Parent(s):

Initial commit

Browse files
Files changed (6) hide show
  1. Dockerfile.dockerfile +12 -0
  2. Procfile +1 -0
  3. README.md +8 -0
  4. app.py +38 -0
  5. requirements.txt +6 -0
  6. utils.py +14 -0
Dockerfile.dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ # Installer dépendances
4
+ WORKDIR /app
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ # Copier le code
9
+ COPY . .
10
+
11
+ # Lancer l'application avec Gunicorn
12
+ CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:7860"]
Procfile ADDED
@@ -0,0 +1 @@
 
 
1
+ web: python app.py
README.md ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # API Alzheimer avec Flask + Hugging Face + Render
2
+
3
+ Cette API prend en entrée une image et retourne une prédiction en utilisant un modèle TensorFlow Lite stocké sur Hugging Face.
4
+
5
+ ## Déploiement
6
+ - Code hébergé sur GitHub
7
+ - Modèle hébergé sur Hugging Face
8
+ - API déployée sur Render
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import os
5
+ from huggingface_hub import hf_hub_download
6
+ from utils import preprocess_image
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Récupérer le modèle depuis Hugging Face Hub
11
+ MODEL_PATH = hf_hub_download(
12
+ repo_id="aroussya/alzheimer-tflite",
13
+ filename="alzheimer_model_float32.tflite",
14
+ token=os.getenv("HF_TOKEN") # le token est dans l'environnement
15
+ )
16
+
17
+ # Charger le modèle TFLite
18
+ interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
19
+ interpreter.allocate_tensors()
20
+
21
+ input_details = interpreter.get_input_details()
22
+ output_details = interpreter.get_output_details()
23
+
24
+ @app.route("/predict", methods=["POST"])
25
+ def predict():
26
+ data = request.get_json()
27
+ image = np.array(data['image'], dtype=np.float32)
28
+ image = preprocess_image(image)
29
+
30
+ interpreter.set_tensor(input_details[0]['index'], image)
31
+ interpreter.invoke()
32
+ output_data = interpreter.get_tensor(output_details[0]['index'])
33
+ prediction = output_data.tolist()[0]
34
+
35
+ return jsonify({"prediction": prediction})
36
+
37
+ if __name__ == "__main__":
38
+ app.run(host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flask
2
+ numpy
3
+ tensorflow
4
+ huggingface-hub
5
+ gunicorn
6
+ opencv-python
utils.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+
4
+ def preprocess_image(image_array):
5
+ """
6
+ Prépare l'image pour le modèle TFLite.
7
+ - Redimensionne à 128x128 (adapter si ton modèle a une autre taille d'entrée)
8
+ - Normalise entre 0 et 1
9
+ - Ajoute la dimension batch
10
+ """
11
+ image = np.array(image_array, dtype=np.float32)
12
+ image = cv2.resize(image, (128, 128))
13
+ image = image / 255.0
14
+ return image.reshape(1, 128, 128, 3)