funterpie commited on
Commit
a49366f
·
verified ·
1 Parent(s): 4dc82b1

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -30
main.py CHANGED
@@ -1,48 +1,27 @@
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from tensorflow.keras.models import load_model
3
- import tensorflow as tf
4
- import os
5
  from PIL import Image
6
  import numpy as np
7
  import io
8
 
9
- # Initialize FastAPI app
10
- app = FastAPI(
11
- title="MNIST Digit Classifier API",
12
- version="1.0"
13
- )
14
 
15
- # Global model variable
16
  model = None
17
 
18
-
19
- # Load model at startup
20
  @app.on_event("startup")
21
  def load_model_on_startup():
22
  global model
23
  try:
24
- # Prefer .keras (native Keras 3 format), fallback to .h5
25
- if os.path.exists("mnist_model.keras"):
26
- model_path = "mnist_model.keras"
27
- else:
28
- model_path = "mnist_model.h5"
29
-
30
- print(f"Loading model from: {model_path}")
31
- model = load_model(model_path, compile=False)
32
-
33
- # Recompile the model
34
- model.compile(
35
- optimizer='adam',
36
- loss='sparse_categorical_crossentropy',
37
- metrics=['accuracy']
38
- )
39
-
40
  print("✅ Model loaded successfully!")
41
- print(f"Model input shape: {model.input_shape}")
42
-
43
  except Exception as e:
44
- print(f"❌ Failed to load model: {e}")
45
- raise RuntimeError(f"Error loading model: {e}")
 
 
46
 
47
 
48
  # Health check
 
1
  from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from tensorflow.keras.models import load_model
 
 
3
  from PIL import Image
4
  import numpy as np
5
  import io
6
 
7
+ app = FastAPI(title="MNIST Digit Classifier API", version="1.0")
 
 
 
 
8
 
 
9
  model = None
10
 
 
 
11
  @app.on_event("startup")
12
  def load_model_on_startup():
13
  global model
14
  try:
15
+ model = load_model("mnist_model_fixed.h5", compile=False)
16
+ model.compile(optimizer='adam',
17
+ loss='sparse_categorical_crossentropy',
18
+ metrics=['accuracy'])
 
 
 
 
 
 
 
 
 
 
 
 
19
  print("✅ Model loaded successfully!")
 
 
20
  except Exception as e:
21
+ print(f"❌ Error: {e}")
22
+ raise RuntimeError(f"Model loading failed: {e}")
23
+
24
+ # ... rest of your endpoints (keep the predict function as is)
25
 
26
 
27
  # Health check