Siraja704 commited on
Commit
5c6141b
Β·
1 Parent(s): 0da07af

Fix model loading compatibility issues

Browse files

- Switch from JAX to TensorFlow backend for better model compatibility
- Use traditional HuggingFace Hub download approach instead of keras.saving
- Add huggingface_hub dependency for proper model downloading
- Fix 'weights_store' error by using compatible loading method
- Update documentation to reflect TensorFlow backend usage

Resolves: ValueError: Expected a model.weights.h5 or model.weights.npz file
Resolves: UnboundLocalError: local variable 'weights_store' referenced before assignment

Files changed (3) hide show
  1. README.md +6 -5
  2. app.py +27 -11
  3. requirements.txt +3 -3
README.md CHANGED
@@ -17,23 +17,24 @@ AI-powered skin condition analysis using deep learning with EfficientNetV2 archi
17
 
18
  ## Features
19
 
20
- - **Modern Keras 3.0 with JAX Backend**: Uses the latest Keras with JAX for improved performance
21
- - **Direct HuggingFace Model Loading**: Loads models directly from HuggingFace Hub
22
  - **5 Skin Conditions**: Classifies Atopic Dermatitis, Eczema, Psoriasis, Seborrheic Keratoses, and Tinea Ringworm Candidiasis
23
  - **Medical Disclaimers**: Includes proper medical disclaimers and advice
24
 
25
  ## Technical Stack
26
 
27
- - **Backend**: JAX (via Keras 3.0)
28
  - **Frontend**: Gradio 5.0+
29
  - **Model Architecture**: EfficientNetV2
30
- - **Model Source**: Direct loading from `hf://Siraja704/DermaAI`
31
 
32
  ## Setup
33
 
34
- This application uses Keras 3.0 with JAX backend for optimal performance. The model is loaded directly from HuggingFace Hub using the new `keras.saving.load_model("hf://...")` syntax.
35
 
36
  If the model repository is private, you may need to:
 
37
  1. Go to the Space settings
38
  2. Add a new secret with key `HF_TOKEN` and your Hugging Face access token as the value
39
  3. Make sure your token has access to the `Siraja704/DermaAI` model repository
 
17
 
18
  ## Features
19
 
20
+ - **Modern Keras 3.0 with TensorFlow Backend**: Uses Keras 3.0 with TensorFlow for reliable model compatibility
21
+ - **HuggingFace Hub Integration**: Downloads and loads models from HuggingFace Hub
22
  - **5 Skin Conditions**: Classifies Atopic Dermatitis, Eczema, Psoriasis, Seborrheic Keratoses, and Tinea Ringworm Candidiasis
23
  - **Medical Disclaimers**: Includes proper medical disclaimers and advice
24
 
25
  ## Technical Stack
26
 
27
+ - **Backend**: TensorFlow (via Keras 3.0)
28
  - **Frontend**: Gradio 5.0+
29
  - **Model Architecture**: EfficientNetV2
30
+ - **Model Source**: `Siraja704/DermaAI` via HuggingFace Hub
31
 
32
  ## Setup
33
 
34
+ This application uses Keras 3.0 with TensorFlow backend for reliable model compatibility. The model is downloaded from HuggingFace Hub using the `huggingface_hub` library.
35
 
36
  If the model repository is private, you may need to:
37
+
38
  1. Go to the Space settings
39
  2. Add a new secret with key `HF_TOKEN` and your Hugging Face access token as the value
40
  3. Make sure your token has access to the `Siraja704/DermaAI` model repository
app.py CHANGED
@@ -1,13 +1,14 @@
1
- # Available backend options are: "jax", "torch", "tensorflow".
2
  import os
3
- os.environ["KERAS_BACKEND"] = "jax"
4
 
5
  import gradio as gr
6
- import keras
 
7
  import numpy as np
8
  from PIL import Image
9
- import tensorflow as tf # Keep for preprocessing function
10
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
 
11
 
12
  # Class names for the 5 skin conditions
13
  CLASS_NAMES = [
@@ -33,20 +34,35 @@ class DermaAIModel:
33
  self.load_model()
34
 
35
  def load_model(self):
36
- """Load the DermaAI model from Hugging Face Hub using Keras 3.0"""
37
  try:
38
  print("πŸ”„ Loading DermaAI model from Hugging Face...")
 
39
  # Get authentication token from environment variable only (secure)
40
  hf_token = os.getenv("HF_TOKEN")
41
 
 
42
  if hf_token:
43
- # Load model with authentication
44
- self.model = keras.saving.load_model("hf://Siraja704/DermaAI", token=hf_token)
45
- print("βœ… Model loaded successfully with authentication!")
 
 
 
 
46
  else:
47
- # Try loading without token (if model becomes public)
48
- self.model = keras.saving.load_model("hf://Siraja704/DermaAI")
49
- print("βœ… Model loaded successfully!")
 
 
 
 
 
 
 
 
 
50
  except Exception as e:
51
  error_msg = str(e)
52
  print(f"❌ Error loading model: {e}")
 
1
+ # Use TensorFlow backend for better compatibility with existing models
2
  import os
3
+ os.environ["KERAS_BACKEND"] = "tensorflow"
4
 
5
  import gradio as gr
6
+ import tensorflow as tf
7
+ from tensorflow import keras
8
  import numpy as np
9
  from PIL import Image
 
10
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
11
+ from huggingface_hub import hf_hub_download
12
 
13
  # Class names for the 5 skin conditions
14
  CLASS_NAMES = [
 
34
  self.load_model()
35
 
36
  def load_model(self):
37
+ """Load the DermaAI model from Hugging Face Hub using traditional approach"""
38
  try:
39
  print("πŸ”„ Loading DermaAI model from Hugging Face...")
40
+
41
  # Get authentication token from environment variable only (secure)
42
  hf_token = os.getenv("HF_TOKEN")
43
 
44
+ # Download model file using HuggingFace Hub
45
  if hf_token:
46
+ print("πŸ” Using authentication token...")
47
+ model_path = hf_hub_download(
48
+ repo_id="Siraja704/DermaAI",
49
+ filename="DermaAI.keras",
50
+ token=hf_token,
51
+ cache_dir="./model_cache"
52
+ )
53
  else:
54
+ print("πŸ“‚ Trying without authentication...")
55
+ model_path = hf_hub_download(
56
+ repo_id="Siraja704/DermaAI",
57
+ filename="DermaAI.keras",
58
+ cache_dir="./model_cache"
59
+ )
60
+
61
+ # Load the model using TensorFlow/Keras
62
+ print(f"πŸ“ Loading model from: {model_path}")
63
+ self.model = keras.models.load_model(model_path)
64
+ print("βœ… Model loaded successfully!")
65
+
66
  except Exception as e:
67
  error_msg = str(e)
68
  print(f"❌ Error loading model: {e}")
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
  gradio>=5.0.0
2
- keras>=3.0.0
3
- jax[cpu]>=0.4.0
4
  tensorflow>=2.13.0
 
5
  pillow>=9.0.0
6
- numpy>=1.21.0
 
 
1
  gradio>=5.0.0
 
 
2
  tensorflow>=2.13.0
3
+ keras>=3.0.0
4
  pillow>=9.0.0
5
+ numpy>=1.21.0
6
+ huggingface_hub>=0.16.0