Aryagm commited on
Commit
3fc327c
·
verified ·
1 Parent(s): 1c19b2e

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ model18cls/saved_model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -13,6 +13,23 @@ license: mit
13
 
14
  Fast GPU-accelerated brain MRI segmentation API.
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  ## API Endpoints
17
 
18
  ### POST /segment
 
13
 
14
  Fast GPU-accelerated brain MRI segmentation API.
15
 
16
+ ## Setup Instructions
17
+
18
+ ### 1. Convert the model (run locally first)
19
+
20
+ ```bash
21
+ # Install conversion dependencies
22
+ pip install tensorflowjs tensorflow
23
+
24
+ # Convert tfjs model to H5 format
25
+ cd huggingface
26
+ python convert_model.py ../public/models/model18cls ./model18cls
27
+ ```
28
+
29
+ ### 2. Upload to Hugging Face Space
30
+
31
+ The `model18cls/` folder should now contain `model.h5` and `saved_model/`.
32
+
33
  ## API Endpoints
34
 
35
  ### POST /segment
app.py CHANGED
@@ -25,31 +25,41 @@ app.add_middleware(
25
  model = None
26
  MODEL_PATH = "model18cls"
27
 
28
- def load_tfjs_model(model_path):
29
- """
30
- Load a TensorFlow.js LayersModel from model.json + weight shards.
31
- Converts it to a Keras model for inference.
32
  """
33
- import tensorflowjs as tfjs
34
-
35
- # Load the tfjs model and convert to Keras
36
- model = tfjs.converters.load_keras_model(os.path.join(model_path, "model.json"))
37
- return model
38
 
39
- def load_model():
40
- """Load TensorFlow model on startup"""
41
  global model
42
  if model is None:
43
  print(f"Loading model from {MODEL_PATH}...")
44
 
45
- # Check if it's a tfjs model (has model.json) or SavedModel
46
- model_json_path = os.path.join(MODEL_PATH, "model.json")
47
- if os.path.exists(model_json_path):
48
- print("Detected TensorFlow.js format, converting...")
49
- model = load_tfjs_model(MODEL_PATH)
50
- else:
51
- print("Loading as SavedModel format...")
 
 
 
 
 
 
 
 
 
 
52
  model = tf.keras.models.load_model(MODEL_PATH)
 
 
 
 
 
 
53
 
54
  print("Model loaded successfully!")
55
  print(f"Input shape: {model.input_shape}")
 
25
  model = None
26
  MODEL_PATH = "model18cls"
27
 
28
+ def load_model():
 
 
 
29
  """
30
+ Load TensorFlow model on startup.
31
+ Supports H5, SavedModel, or Keras formats.
 
 
 
32
 
33
+ NOTE: Convert tfjs models first using convert_model.py
34
+ """
35
  global model
36
  if model is None:
37
  print(f"Loading model from {MODEL_PATH}...")
38
 
39
+ # Check for different model formats
40
+ h5_path = os.path.join(MODEL_PATH, "model.h5")
41
+ keras_path = os.path.join(MODEL_PATH, "model.keras")
42
+ saved_model_dir = os.path.join(MODEL_PATH, "saved_model")
43
+
44
+ if os.path.exists(h5_path):
45
+ print("Loading H5 format...")
46
+ model = tf.keras.models.load_model(h5_path)
47
+ elif os.path.exists(keras_path):
48
+ print("Loading Keras format...")
49
+ model = tf.keras.models.load_model(keras_path)
50
+ elif os.path.exists(saved_model_dir):
51
+ print("Loading SavedModel format...")
52
+ model = tf.keras.models.load_model(saved_model_dir)
53
+ elif os.path.exists(MODEL_PATH) and os.path.isdir(MODEL_PATH):
54
+ # Try loading directory as SavedModel
55
+ print("Loading as SavedModel directory...")
56
  model = tf.keras.models.load_model(MODEL_PATH)
57
+ else:
58
+ raise FileNotFoundError(
59
+ f"No model found in {MODEL_PATH}. "
60
+ "Please convert the tfjs model first using: "
61
+ "python convert_model.py ../public/models/model18cls ./model18cls"
62
+ )
63
 
64
  print("Model loaded successfully!")
65
  print(f"Input shape: {model.input_shape}")
convert_model.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Convert TensorFlow.js model to Keras H5 format.
4
+ Run this locally before uploading to Hugging Face.
5
+
6
+ Usage:
7
+ pip install tensorflowjs tensorflow
8
+ python convert_model.py ../public/models/model18cls ./model18cls
9
+ """
10
+
11
+ import sys
12
+ import os
13
+
14
+ def convert_tfjs_to_keras(input_path, output_path):
15
+ """Convert tfjs model to Keras H5 format"""
16
+ # Import here to avoid issues if not installed
17
+ import tensorflowjs as tfjs
18
+ import tensorflow as tf
19
+
20
+ print(f"Converting {input_path} to Keras format...")
21
+
22
+ # Load tfjs model
23
+ model_json = os.path.join(input_path, "model.json")
24
+ model = tfjs.converters.load_keras_model(model_json)
25
+
26
+ # Create output directory
27
+ os.makedirs(output_path, exist_ok=True)
28
+
29
+ # Save as H5
30
+ h5_path = os.path.join(output_path, "model.h5")
31
+ model.save(h5_path)
32
+ print(f"Saved to {h5_path}")
33
+
34
+ # Also save as SavedModel for better compatibility
35
+ savedmodel_path = os.path.join(output_path, "saved_model")
36
+ model.save(savedmodel_path, save_format='tf')
37
+ print(f"Saved to {savedmodel_path}")
38
+
39
+ print("Conversion complete!")
40
+ print(f"Model input shape: {model.input_shape}")
41
+ print(f"Model output shape: {model.output_shape}")
42
+
43
+ if __name__ == "__main__":
44
+ if len(sys.argv) < 3:
45
+ print("Usage: python convert_model.py <input_tfjs_path> <output_path>")
46
+ print("Example: python convert_model.py ../public/models/model18cls ./model18cls")
47
+ sys.exit(1)
48
+
49
+ input_path = sys.argv[1]
50
+ output_path = sys.argv[2]
51
+
52
+ convert_tfjs_to_keras(input_path, output_path)
model18cls/model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79ec7062d63fc42594a0c6538247934b97c7042d70033760ca688cbf99cb0d34
3
+ size 442236
model18cls/saved_model/fingerprint.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6c960c45c2b00bf76cfa8d6ac4924a33a5be27b2fb4278543b048ba7740ce3bd
3
+ size 77
model18cls/saved_model/keras_metadata.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bb6956e1d4160290701b3e5bda555c1a679b6be456f749ac22999276562d1ee5
3
+ size 40634
model18cls/saved_model/saved_model.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:11505c3a150ae680e364454caf30210518f633ae92b80b0b178725c16fa2afde
3
+ size 242134
model18cls/saved_model/variables/variables.data-00000-of-00001 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0a11f9621a21dc7a3f50f28c8d575d943b94c481ed9604e5815031952147ce08
3
+ size 399788
model18cls/saved_model/variables/variables.index ADDED
Binary file (1.46 kB). View file
 
requirements.txt CHANGED
@@ -2,6 +2,5 @@ fastapi==0.104.1
2
  uvicorn[standard]==0.24.0
3
  python-multipart==0.0.6
4
  tensorflow==2.15.0
5
- tensorflowjs==4.15.0
6
  nibabel==5.2.0
7
  numpy==1.26.2
 
2
  uvicorn[standard]==0.24.0
3
  python-multipart==0.0.6
4
  tensorflow==2.15.0
 
5
  nibabel==5.2.0
6
  numpy==1.26.2