Sharris commited on
Commit
cfce11e
·
verified ·
1 Parent(s): b08da36

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +46 -31
app.py CHANGED
@@ -5,45 +5,60 @@ import tensorflow as tf
5
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6
  import gradio as gr
7
 
8
- # Try to load model from various sources
9
  model = None
 
10
 
11
- # Try local files first (for development)
12
- local_model_paths = ["saved_model", "best_model.h5", "final_model.h5"]
13
- for path in local_model_paths:
14
- if os.path.exists(path):
15
- try:
16
- model = tf.keras.models.load_model(path, compile=False)
17
- print(f"Loaded model from local path: {path}")
18
- break
19
- except Exception as e:
20
- print(f"Failed to load local model from {path}: {e}")
21
 
22
- # If no local model, try to download from Hugging Face Hub
23
- if model is None:
24
- HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "Sharris/age_detection_regression")
 
 
 
 
 
 
 
 
 
 
 
25
  try:
26
- from huggingface_hub import hf_hub_download
27
- # Try to download the .h5 model file
28
- model_path = hf_hub_download(repo_id=HF_MODEL_ID, filename="best_model.h5")
29
- model = tf.keras.models.load_model(model_path, compile=False)
30
- print(f"Loaded model from HF Hub: {HF_MODEL_ID}/best_model.h5")
31
- except Exception as e:
32
- print(f"Failed to load model from HF Hub ({HF_MODEL_ID}): {e}")
33
- # Fallback: try to download entire repo and load from there
34
- try:
35
- from huggingface_hub import snapshot_download
36
- repo_dir = snapshot_download(repo_id=HF_MODEL_ID)
37
- model_file = os.path.join(repo_dir, "best_model.h5")
38
  if os.path.exists(model_file):
39
- model = tf.keras.models.load_model(model_file, compile=False)
40
- print(f"Loaded model from downloaded repo: {model_file}")
41
- except Exception as e2:
42
- print(f"Fallback download also failed: {e2}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  if model is None:
45
  raise RuntimeError(
46
- "No model found. Ensure 'best_model.h5' exists locally or set HF_MODEL_ID env var to a Hugging Face model repo containing the model."
47
  )
48
 
49
  INPUT_SIZE = (224, 224)
 
5
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
6
  import gradio as gr
7
 
8
+ # Load model from Hugging Face Hub
9
  model = None
10
+ HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "Sharris/age_detection_regression")
11
 
12
+ print(f"Attempting to load model from: {HF_MODEL_ID}")
 
 
 
 
 
 
 
 
 
13
 
14
+ try:
15
+ from huggingface_hub import hf_hub_download
16
+ print("Downloading best_model.h5...")
17
+ model_path = hf_hub_download(repo_id=HF_MODEL_ID, filename="best_model.h5")
18
+ print(f"Model downloaded to: {model_path}")
19
+
20
+ print("Loading model with TensorFlow...")
21
+ model = tf.keras.models.load_model(model_path, compile=False)
22
+ print(f"✅ Successfully loaded model from {HF_MODEL_ID}")
23
+
24
+ except Exception as e:
25
+ print(f"❌ Failed to download best_model.h5: {e}")
26
+
27
+ # Fallback: try to download entire repo and look for model files
28
  try:
29
+ print("Trying fallback: downloading entire repository...")
30
+ from huggingface_hub import snapshot_download
31
+ repo_dir = snapshot_download(repo_id=HF_MODEL_ID)
32
+ print(f"Repository downloaded to: {repo_dir}")
33
+
34
+ # Look for model files in the downloaded repo
35
+ possible_files = ["best_model.h5", "final_model.h5", "model.h5"]
36
+ for filename in possible_files:
37
+ model_file = os.path.join(repo_dir, filename)
 
 
 
38
  if os.path.exists(model_file):
39
+ print(f"Found model file: {model_file}")
40
+ try:
41
+ model = tf.keras.models.load_model(model_file, compile=False)
42
+ print(f" Successfully loaded model from {model_file}")
43
+ break
44
+ except Exception as load_error:
45
+ print(f"Failed to load {model_file}: {load_error}")
46
+ continue
47
+
48
+ if model is None:
49
+ # List all files in the repo for debugging
50
+ import os
51
+ print("Files in downloaded repository:")
52
+ for root, dirs, files in os.walk(repo_dir):
53
+ for file in files:
54
+ print(f" {os.path.join(root, file)}")
55
+
56
+ except Exception as e2:
57
+ print(f"❌ Fallback download also failed: {e2}")
58
 
59
  if model is None:
60
  raise RuntimeError(
61
+ f" Could not load model from {HF_MODEL_ID}. Please ensure the repository contains a valid model file (best_model.h5, final_model.h5, or model.h5)."
62
  )
63
 
64
  INPUT_SIZE = (224, 224)