Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
import keras
|
| 5 |
import zipfile
|
| 6 |
import json
|
| 7 |
-
import
|
| 8 |
-
import spaces # <--- 1. Add this import at the top
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@keras.saving.register_keras_serializable(package="Custom")
|
| 12 |
def preprocess_input(x):
|
| 13 |
return x
|
| 14 |
|
|
|
|
| 15 |
def strip_quantization_keys(config):
|
| 16 |
if isinstance(config, dict):
|
| 17 |
config.pop('quantization_config', None)
|
|
@@ -21,8 +26,11 @@ def strip_quantization_keys(config):
|
|
| 21 |
for item in config:
|
| 22 |
strip_quantization_keys(item)
|
| 23 |
|
|
|
|
| 24 |
def load_sanitized_model(original_path):
|
| 25 |
sanitized_path = "sanitized_classifier.keras"
|
|
|
|
|
|
|
| 26 |
with zipfile.ZipFile(original_path, 'r') as zin:
|
| 27 |
with zipfile.ZipFile(sanitized_path, 'w') as zout:
|
| 28 |
for item in zin.infolist():
|
|
@@ -32,16 +40,23 @@ def load_sanitized_model(original_path):
|
|
| 32 |
strip_quantization_keys(config_dict)
|
| 33 |
data = json.dumps(config_dict).encode('utf-8')
|
| 34 |
zout.writestr(item, data)
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if os.path.exists(sanitized_path):
|
| 37 |
os.remove(sanitized_path)
|
| 38 |
return model
|
| 39 |
|
| 40 |
print("Loading Keras model into Hugging Face memory...")
|
| 41 |
model_loaded = load_sanitized_model('garbage_classifier.keras')
|
|
|
|
| 42 |
lookup = {0: "Cardboard", 1: "Glass", 2: "Metal", 3: "Paper", 4: "Plastic", 5: "Trash"}
|
| 43 |
|
| 44 |
-
#
|
| 45 |
@spaces.GPU
|
| 46 |
def predict_image(img):
|
| 47 |
if img is None:
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 4 |
+
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
| 8 |
import tensorflow as tf
|
| 9 |
import keras
|
| 10 |
import zipfile
|
| 11 |
import json
|
| 12 |
+
import spaces
|
|
|
|
| 13 |
|
| 14 |
+
# 1. Stand-in function for your model's Lambda layer structure
|
| 15 |
@keras.saving.register_keras_serializable(package="Custom")
|
| 16 |
def preprocess_input(x):
|
| 17 |
return x
|
| 18 |
|
| 19 |
+
# 2. Helper function to scrub out the bad parameters from the inner json dictionary
|
| 20 |
def strip_quantization_keys(config):
|
| 21 |
if isinstance(config, dict):
|
| 22 |
config.pop('quantization_config', None)
|
|
|
|
| 26 |
for item in config:
|
| 27 |
strip_quantization_keys(item)
|
| 28 |
|
| 29 |
+
# 3. Read, rewrite, and hot-load the sanitized structural data
|
| 30 |
def load_sanitized_model(original_path):
|
| 31 |
sanitized_path = "sanitized_classifier.keras"
|
| 32 |
+
print("Intercepting model zip archive to remove quantization flags...")
|
| 33 |
+
|
| 34 |
with zipfile.ZipFile(original_path, 'r') as zin:
|
| 35 |
with zipfile.ZipFile(sanitized_path, 'w') as zout:
|
| 36 |
for item in zin.infolist():
|
|
|
|
| 40 |
strip_quantization_keys(config_dict)
|
| 41 |
data = json.dumps(config_dict).encode('utf-8')
|
| 42 |
zout.writestr(item, data)
|
| 43 |
+
|
| 44 |
+
model = keras.models.load_model(
|
| 45 |
+
sanitized_path,
|
| 46 |
+
custom_objects={'preprocess_input': preprocess_input},
|
| 47 |
+
safe_mode=False
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
if os.path.exists(sanitized_path):
|
| 51 |
os.remove(sanitized_path)
|
| 52 |
return model
|
| 53 |
|
| 54 |
print("Loading Keras model into Hugging Face memory...")
|
| 55 |
model_loaded = load_sanitized_model('garbage_classifier.keras')
|
| 56 |
+
|
| 57 |
lookup = {0: "Cardboard", 1: "Glass", 2: "Metal", 3: "Paper", 4: "Plastic", 5: "Trash"}
|
| 58 |
|
| 59 |
+
# 4. Wrap the inference function so ZeroGPU yields control correctly
|
| 60 |
@spaces.GPU
|
| 61 |
def predict_image(img):
|
| 62 |
if img is None:
|