BeyzaTopbas commited on
Commit
a199367
·
verified ·
1 Parent(s): 9513256

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,10 +1,8 @@
1
- import os
2
- os.environ["TF_USE_LEGACY_KERAS"] = "1" # tell TF to use tf-keras (legacy)
3
-
4
  import streamlit as st
5
  import tensorflow as tf
6
  import numpy as np
7
  from PIL import Image
 
8
 
9
  # ================== SETTINGS ==================
10
  IMG_SIZE = (170, 170) # same as target_size in your ImageDataGenerator
@@ -16,22 +14,22 @@ CLASS_NAMES = [
16
  "Leaf Blight",
17
  ]
18
 
19
- MODEL_FILENAME = "grape_disease_model.h5" # make sure this file is in the same folder
20
  # =============================================
21
 
22
 
23
-
24
  @st.cache_resource
25
- def load_model():
26
  """
27
- Load the Keras model once and cache it.
28
  """
29
- if not os.path.exists(MODEL_FILENAME):
30
- st.error(f"Model file '{MODEL_FILENAME}' not found in this directory.")
31
  st.stop()
32
 
33
- model = tf.keras.models.load_model(MODEL_FILENAME)
34
- return model
 
35
 
36
 
37
  def preprocess_image(img: Image.Image) -> np.ndarray:
@@ -54,16 +52,23 @@ def preprocess_image(img: Image.Image) -> np.ndarray:
54
 
55
  def predict_image(img: Image.Image):
56
  """
57
- Returns (predicted_label, confidence, all_probabilities)
58
- for multi-class classification.
59
  """
60
- # ⬇️ load model only when needed
61
- model = load_model()
 
 
 
 
 
 
 
 
62
 
63
- x = preprocess_image(img)
64
- preds = model.predict(x)
65
 
66
- # expected: shape (1, 4) with softmax
67
  probs = preds[0]
68
  idx = int(np.argmax(probs))
69
  label = CLASS_NAMES[idx]
 
 
 
 
1
  import streamlit as st
2
  import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
+ import os
6
 
7
  # ================== SETTINGS ==================
8
  IMG_SIZE = (170, 170) # same as target_size in your ImageDataGenerator
 
14
  "Leaf Blight",
15
  ]
16
 
17
+ TFLITE_MODEL_FILENAME = "grape_disease_model.tflite" # new TFLite model
18
  # =============================================
19
 
20
 
 
21
  @st.cache_resource
22
+ def load_interpreter():
23
  """
24
+ Load the TFLite model once and cache the interpreter.
25
  """
26
+ if not os.path.exists(TFLITE_MODEL_FILENAME):
27
+ st.error(f"Model file '{TFLITE_MODEL_FILENAME}' not found in this directory.")
28
  st.stop()
29
 
30
+ interpreter = tf.lite.Interpreter(model_path=TFLITE_MODEL_FILENAME)
31
+ interpreter.allocate_tensors()
32
+ return interpreter
33
 
34
 
35
  def preprocess_image(img: Image.Image) -> np.ndarray:
 
52
 
53
  def predict_image(img: Image.Image):
54
  """
55
+ Run inference using the TFLite interpreter.
56
+ Returns (predicted_label, confidence, all_probabilities).
57
  """
58
+ interpreter = load_interpreter()
59
+
60
+ input_details = interpreter.get_input_details()
61
+ output_details = interpreter.get_output_details()
62
+
63
+ x = preprocess_image(img).astype(np.float32)
64
+
65
+ # feed input
66
+ interpreter.set_tensor(input_details[0]['index'], x)
67
+ interpreter.invoke()
68
 
69
+ # get output
70
+ preds = interpreter.get_tensor(output_details[0]['index'])
71
 
 
72
  probs = preds[0]
73
  idx = int(np.argmax(probs))
74
  label = CLASS_NAMES[idx]