Ahmedhassan54 commited on
Commit
3a1a754
·
verified ·
1 Parent(s): 032b177

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -7,6 +7,9 @@ import os
7
  import pandas as pd
8
  import logging
9
 
 
 
 
10
  # Setup logging
11
  logging.basicConfig(level=logging.INFO)
12
  logger = logging.getLogger(__name__)
@@ -30,7 +33,9 @@ def load_model():
30
  )
31
  logger.info(f"Model path: {model_path}")
32
 
33
- model = tf.keras.models.load_model(model_path)
 
 
34
  logger.info("Model loaded successfully!")
35
  except Exception as e:
36
  logger.error(f"Model loading failed: {str(e)}")
@@ -46,7 +51,7 @@ def classify_image(image):
46
 
47
  # Convert to PIL Image if numpy array
48
  if isinstance(image, np.ndarray):
49
- image = Image.fromarray(image)
50
 
51
  # Preprocess
52
  image = image.resize((150, 150))
@@ -56,7 +61,8 @@ def classify_image(image):
56
 
57
  # Predict
58
  if model is not None:
59
- pred = model.predict(img_array, verbose=0)
 
60
  confidence = float(pred[0][0])
61
  else:
62
  confidence = 0.75 # Demo value
@@ -75,7 +81,7 @@ def classify_image(image):
75
 
76
  except Exception as e:
77
  logger.error(f"Error: {str(e)}")
78
- return {"Error": str(e)}, pd.DataFrame()
79
 
80
  # Interface
81
  with gr.Blocks() as demo:
@@ -93,9 +99,8 @@ with gr.Blocks() as demo:
93
  x="Class", y="Confidence", y_lim=[0,1]
94
  )
95
 
96
- # Fixed button click handler - removed api_name
97
  classify_btn.click(
98
- fn=classify_image,
99
  inputs=img_input,
100
  outputs=[label_out, plot_out]
101
  )
@@ -113,4 +118,4 @@ with gr.Blocks() as demo:
113
  )
114
 
115
  if __name__ == "__main__":
116
- demo.launch()
 
7
  import pandas as pd
8
  import logging
9
 
10
+ # Disable GPU if not available (for Hugging Face Spaces)
11
+ os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
12
+
13
  # Setup logging
14
  logging.basicConfig(level=logging.INFO)
15
  logger = logging.getLogger(__name__)
 
33
  )
34
  logger.info(f"Model path: {model_path}")
35
 
36
+ # Explicitly disable GPU
37
+ with tf.device('/CPU:0'):
38
+ model = tf.keras.models.load_model(model_path)
39
  logger.info("Model loaded successfully!")
40
  except Exception as e:
41
  logger.error(f"Model loading failed: {str(e)}")
 
51
 
52
  # Convert to PIL Image if numpy array
53
  if isinstance(image, np.ndarray):
54
+ image = Image.fromarray(image.astype('uint8'))
55
 
56
  # Preprocess
57
  image = image.resize((150, 150))
 
61
 
62
  # Predict
63
  if model is not None:
64
+ with tf.device('/CPU:0'):
65
+ pred = model.predict(img_array, verbose=0)
66
  confidence = float(pred[0][0])
67
  else:
68
  confidence = 0.75 # Demo value
 
81
 
82
  except Exception as e:
83
  logger.error(f"Error: {str(e)}")
84
+ return {"Error": str(e)}, pd.DataFrame({'Class': ['Cat', 'Dog'], 'Confidence': [0.5, 0.5]})
85
 
86
  # Interface
87
  with gr.Blocks() as demo:
 
99
  x="Class", y="Confidence", y_lim=[0,1]
100
  )
101
 
 
102
  classify_btn.click(
103
+ classify_image,
104
  inputs=img_input,
105
  outputs=[label_out, plot_out]
106
  )
 
118
  )
119
 
120
  if __name__ == "__main__":
121
+ demo.launch(server_name="0.0.0.0", server_port=7860)