ECCesA commited on
Commit
212b656
·
verified ·
1 Parent(s): 5846c5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -1,9 +1,20 @@
 
1
  import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
 
4
 
5
- # 1. Load the model and class names
6
- model = tf.keras.models.load_model('plantvillage_efficientnet_b0.keras')
 
 
 
 
 
 
 
 
 
7
 
8
  classes = [
9
  'Pepper__bell___Bacterial_spot', 'Pepper__bell___healthy', 'Potato___Early_blight',
@@ -15,6 +26,9 @@ classes = [
15
  ]
16
 
17
  def predict(image):
 
 
 
18
  img = tf.cast(image, tf.float32)
19
  img = tf.image.resize(img, (224, 224))
20
  img = tf.keras.applications.efficientnet.preprocess_input(img)
@@ -23,14 +37,19 @@ def predict(image):
23
  preds = model.predict(img)[0]
24
  return {classes[i]: float(preds[i]) for i in range(len(classes))}
25
 
26
- # 2. Build Interface
27
  demo = gr.Interface(
28
  fn=predict,
29
  inputs=gr.Image(),
30
  outputs=gr.Label(num_top_classes=3),
31
  title="Plant Disease Detector",
32
- description="Identify plant leaf diseases using EfficientNetB0."
33
  )
34
 
35
  if __name__ == '__main__':
36
- demo.launch()
 
 
 
 
 
 
 
1
+ app_content = """
2
  import gradio as gr
3
  import tensorflow as tf
4
  import numpy as np
5
+ import os
6
 
7
+ # 1. Load the model with error handling
8
+ try:
9
+ model_path = 'plantvillage_efficientnet_b0.keras'
10
+ if not os.path.exists(model_path):
11
+ raise FileNotFoundError(f"Model file {model_path} not found in Space!")
12
+
13
+ model = tf.keras.models.load_model(model_path, compile=False)
14
+ print("Model loaded successfully!")
15
+ except Exception as e:
16
+ print(f"CRITICAL ERROR LOADING MODEL: {e}")
17
+ model = None
18
 
19
  classes = [
20
  'Pepper__bell___Bacterial_spot', 'Pepper__bell___healthy', 'Potato___Early_blight',
 
26
  ]
27
 
28
  def predict(image):
29
+ if model is None:
30
+ return "Error: Model failed to load. Check Space logs."
31
+
32
  img = tf.cast(image, tf.float32)
33
  img = tf.image.resize(img, (224, 224))
34
  img = tf.keras.applications.efficientnet.preprocess_input(img)
 
37
  preds = model.predict(img)[0]
38
  return {classes[i]: float(preds[i]) for i in range(len(classes))}
39
 
 
40
  demo = gr.Interface(
41
  fn=predict,
42
  inputs=gr.Image(),
43
  outputs=gr.Label(num_top_classes=3),
44
  title="Plant Disease Detector",
45
+ description="Identify plant leaf diseases."
46
  )
47
 
48
  if __name__ == '__main__':
49
+ demo.launch()
50
+ """
51
+
52
+ with open('app.py', 'w') as f:
53
+ f.write(app_content.strip())
54
+
55
+ print('app.py has been updated with extra error checking. Download and upload it to HF!')