pavuluriashwitha commited on
Commit
bef3075
·
verified ·
1 Parent(s): 5ffb8a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -5,9 +5,6 @@ import streamlit as st
5
  st.set_page_config(page_title="Image Classifier", layout="wide")
6
 
7
  # Now import other libraries
8
- os.system("pip install tensorflow")
9
- os.system("pip install scikit-learn")
10
-
11
  import tensorflow as tf
12
  import numpy as np
13
  import pickle
@@ -19,12 +16,17 @@ LABEL_ENCODER_PATH = "le.pkl"
19
  EXPECTED_SIZE = (64, 64) # Update this based on your model's input shape
20
 
21
  def load_resources():
22
- """Load model and label encoder."""
23
  try:
24
- # Try loading with custom InputLayer configuration
25
  custom_objects = {
26
- 'InputLayer': lambda **kwargs: tf.keras.layers.InputLayer(**{k: v for k, v in kwargs.items() if k != 'batch_shape'})
 
 
 
27
  }
 
 
28
  model = tf.keras.models.load_model(
29
  MODEL_PATH,
30
  compile=False,
@@ -32,6 +34,7 @@ def load_resources():
32
  )
33
  except Exception as e:
34
  st.error(f"Error loading model: {str(e)}")
 
35
  return None, None
36
 
37
  try:
@@ -92,5 +95,6 @@ if uploaded_file:
92
  if model is None or label_encoder is None:
93
  st.error("Model or label encoder failed to load. Please check the files.")
94
  else:
95
- prediction = predict(image)
96
- st.success(f"🎯 Predicted Class: {prediction}")
 
 
5
  st.set_page_config(page_title="Image Classifier", layout="wide")
6
 
7
  # Now import other libraries
 
 
 
8
  import tensorflow as tf
9
  import numpy as np
10
  import pickle
 
16
  EXPECTED_SIZE = (64, 64) # Update this based on your model's input shape
17
 
18
  def load_resources():
19
+ """Load model and label encoder with custom object handling."""
20
  try:
21
+ # Define custom objects to handle compatibility issues
22
  custom_objects = {
23
+ # Handle InputLayer batch_shape issue
24
+ 'InputLayer': lambda **kwargs: tf.keras.layers.InputLayer(**{k: v for k, v in kwargs.items() if k != 'batch_shape'}),
25
+ # Handle DTypePolicy issue
26
+ 'DTypePolicy': tf.keras.mixed_precision.Policy
27
  }
28
+
29
+ # Try loading with custom objects
30
  model = tf.keras.models.load_model(
31
  MODEL_PATH,
32
  compile=False,
 
34
  )
35
  except Exception as e:
36
  st.error(f"Error loading model: {str(e)}")
37
+ st.error("Please ensure you're using TensorFlow 2.x and the model file is not corrupted.")
38
  return None, None
39
 
40
  try:
 
95
  if model is None or label_encoder is None:
96
  st.error("Model or label encoder failed to load. Please check the files.")
97
  else:
98
+ with st.spinner('Predicting...'):
99
+ prediction = predict(image)
100
+ st.success(f"🎯 Predicted Class: {prediction}")