nivashuggingface commited on
Commit
ff06826
·
verified ·
1 Parent(s): 78af0ef

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +61 -2
app.py CHANGED
@@ -3,10 +3,69 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
  import io
 
 
 
6
 
7
- # Load the model from Hugging Face
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  MODEL_PATH = "https://huggingface.co/nivashuggingface/digit-recognition/resolve/main/saved_model"
9
- model = tf.saved_model.load(MODEL_PATH)
 
 
 
 
 
 
 
 
 
10
 
11
  def preprocess_image(img):
12
  """Preprocess the drawn image for prediction"""
 
3
  import numpy as np
4
  from PIL import Image
5
  import io
6
+ import os
7
+ import requests
8
+ import tempfile
9
 
10
+ # Function to download the model from Hugging Face
11
+ def download_model_from_hf(model_path, local_dir):
12
+ """Download model files from Hugging Face"""
13
+ # Create a temporary directory to store the model
14
+ os.makedirs(local_dir, exist_ok=True)
15
+
16
+ # Extract the repository and file path from the URL
17
+ # Example URL: https://huggingface.co/nivashuggingface/digit-recognition/resolve/main/saved_model
18
+ parts = model_path.split('/')
19
+ repo_id = f"{parts[3]}/{parts[4]}"
20
+ file_path = '/'.join(parts[6:])
21
+
22
+ # Download the model files
23
+ api_url = f"https://huggingface.co/api/models/{repo_id}/revision/main/files/{file_path}"
24
+ response = requests.get(api_url)
25
+
26
+ if response.status_code == 200:
27
+ # Download the saved_model.pb file
28
+ saved_model_pb_url = f"https://huggingface.co/{repo_id}/resolve/main/{file_path}/saved_model.pb"
29
+ pb_response = requests.get(saved_model_pb_url)
30
+ if pb_response.status_code == 200:
31
+ with open(os.path.join(local_dir, "saved_model.pb"), "wb") as f:
32
+ f.write(pb_response.content)
33
+
34
+ # Download the variables directory
35
+ variables_dir = os.path.join(local_dir, "variables")
36
+ os.makedirs(variables_dir, exist_ok=True)
37
+
38
+ # Download variables.data-00000-of-00001
39
+ variables_url = f"https://huggingface.co/{repo_id}/resolve/main/{file_path}/variables/variables.data-00000-of-00001"
40
+ var_response = requests.get(variables_url)
41
+ if var_response.status_code == 200:
42
+ with open(os.path.join(variables_dir, "variables.data-00000-of-00001"), "wb") as f:
43
+ f.write(var_response.content)
44
+
45
+ # Download variables.index
46
+ index_url = f"https://huggingface.co/{repo_id}/resolve/main/{file_path}/variables/variables.index"
47
+ index_response = requests.get(index_url)
48
+ if index_response.status_code == 200:
49
+ with open(os.path.join(variables_dir, "variables.index"), "wb") as f:
50
+ f.write(index_response.content)
51
+
52
+ return True
53
+ else:
54
+ print(f"Failed to download model: {response.status_code}")
55
+ return False
56
+
57
+ # Create a temporary directory for the model
58
  MODEL_PATH = "https://huggingface.co/nivashuggingface/digit-recognition/resolve/main/saved_model"
59
+ LOCAL_MODEL_DIR = os.path.join(tempfile.gettempdir(), "digit_recognition_model")
60
+
61
+ # Download the model if it doesn't exist locally
62
+ if not os.path.exists(os.path.join(LOCAL_MODEL_DIR, "saved_model.pb")):
63
+ print("Downloading model from Hugging Face...")
64
+ download_model_from_hf(MODEL_PATH, LOCAL_MODEL_DIR)
65
+
66
+ # Load the model from local directory
67
+ print(f"Loading model from {LOCAL_MODEL_DIR}")
68
+ model = tf.saved_model.load(LOCAL_MODEL_DIR)
69
 
70
  def preprocess_image(img):
71
  """Preprocess the drawn image for prediction"""