chicong commited on
Commit
bd7fe72
·
1 Parent(s): 482d6bc

Fix HF runtime model deserialization compatibility

Browse files
Files changed (3) hide show
  1. Dockerfile +4 -2
  2. main.py +37 -1
  3. requirements.txt +7 -6
Dockerfile CHANGED
@@ -1,11 +1,13 @@
1
- FROM python:3.10-slim
2
  WORKDIR /app
3
 
4
  COPY ./requirements.txt .
5
- RUN pip install --no-cache-dir -r requirements.txt
6
 
7
  COPY . .
8
 
 
 
9
  EXPOSE 7860
10
 
11
  # Lệnh khởi chạy API FastAPI (Hugging Face dùng port 7860)
 
1
+ FROM python:3.12-slim
2
  WORKDIR /app
3
 
4
  COPY ./requirements.txt .
5
+ RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt
6
 
7
  COPY . .
8
 
9
+ ENV PYTHONUNBUFFERED=1
10
+
11
  EXPOSE 7860
12
 
13
  # Lệnh khởi chạy API FastAPI (Hugging Face dùng port 7860)
main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from io import BytesIO
2
  from PIL import Image
3
  import tensorflow as tf
@@ -44,7 +45,32 @@ DISEASE_INFO = {
44
  }
45
  }
46
 
47
- model = load_model('plant_disease_recog_model_pwp_3.keras')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  def read_file_as_image(data):
50
  img = Image.open(BytesIO(data))
@@ -52,6 +78,16 @@ def read_file_as_image(data):
52
  img = img.resize((224, 224))
53
  return np.array(img)
54
 
 
 
 
 
 
 
 
 
 
 
55
  @app.post("/detect-disease")
56
  async def detectDisease(file: UploadFile = File(...)):
57
 
 
1
+ import os
2
  from io import BytesIO
3
  from PIL import Image
4
  import tensorflow as tf
 
45
  }
46
  }
47
 
48
+ MODEL_PATH = os.getenv("MODEL_PATH", "plant_disease_recog_model_pwp_3.keras")
49
+
50
+
51
+ def load_model_with_compatibility(model_path):
52
+ # Some Keras/TensorFlow versions do not accept this key during deserialization.
53
+ from tensorflow.keras.layers import Dense
54
+
55
+ original_dense_init = Dense.__init__
56
+
57
+ def patched_dense_init(self, *args, **kwargs):
58
+ kwargs.pop("quantization_config", None)
59
+ return original_dense_init(self, *args, **kwargs)
60
+
61
+ Dense.__init__ = patched_dense_init
62
+
63
+ try:
64
+ try:
65
+ return load_model(model_path, compile=False, safe_mode=False)
66
+ except TypeError:
67
+ return load_model(model_path, compile=False)
68
+ finally:
69
+ Dense.__init__ = original_dense_init
70
+
71
+
72
+ model = load_model_with_compatibility(MODEL_PATH)
73
+ print(f"[Startup] TensorFlow={tf.__version__}, model={MODEL_PATH}")
74
 
75
  def read_file_as_image(data):
76
  img = Image.open(BytesIO(data))
 
78
  img = img.resize((224, 224))
79
  return np.array(img)
80
 
81
+
82
+ @app.get("/")
83
+ async def root():
84
+ return {"status": "ok", "message": "SmartFarm disease API is running"}
85
+
86
+
87
+ @app.get("/health")
88
+ async def health():
89
+ return {"status": "ok", "model_loaded": model is not None}
90
+
91
  @app.post("/detect-disease")
92
  async def detectDisease(file: UploadFile = File(...)):
93
 
requirements.txt CHANGED
@@ -1,6 +1,7 @@
1
- fastapi
2
- uvicorn
3
- python-multipart
4
- tensorflow
5
- pillow
6
- numpy
 
 
1
+ fastapi==0.136.0
2
+ uvicorn==0.44.0
3
+ python-multipart==0.0.20
4
+ tensorflow==2.21.0
5
+ keras==3.14.0
6
+ pillow==12.2.0
7
+ numpy>=1.26.0,<2.2.0