abdullahzunorain commited on
Commit
d4c798e
·
verified ·
1 Parent(s): 3fa3f62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -11
app.py CHANGED
@@ -8,7 +8,7 @@ import json
8
  with open("class_indices.json", "r") as f:
9
  class_labels = json.load(f)
10
 
11
- # Load trained model
12
  model = tf.keras.models.load_model("best_model.h5")
13
 
14
  def preprocess(img: Image.Image) -> np.ndarray:
@@ -18,23 +18,45 @@ def preprocess(img: Image.Image) -> np.ndarray:
18
 
19
  def predict_disease(image: Image.Image) -> str:
20
  try:
21
- processed_img = preprocess(image)
22
- preds = model.predict(processed_img)
23
  idx = np.argmax(preds)
24
  label = class_labels[str(idx)]
25
  confidence = preds[0][idx]
26
- return f"**Prediction:** {label}\n**Confidence:** {confidence:.2%}"
27
  except Exception as e:
28
  return f"Prediction error: {e}"
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  interface = gr.Interface(
31
  fn=predict_disease,
32
- inputs=gr.Image(type="pil", label="Upload Tomato Leaf Image"),
33
- outputs=gr.Markdown(label="Result"),
34
- title="Tomato Leaf Disease Detection",
35
- description="Upload a tomato leaf image to identify the disease with confidence.",
36
- theme="default",
37
  allow_flagging="never",
 
 
38
  )
39
 
40
  if __name__ == "__main__":
@@ -42,8 +64,6 @@ if __name__ == "__main__":
42
 
43
 
44
 
45
-
46
-
47
  # ----------------------------------------------------------------------------------------------------------------------
48
 
49
  # import gradio as gr
 
8
  with open("class_indices.json", "r") as f:
9
  class_labels = json.load(f)
10
 
11
+ # Load model
12
  model = tf.keras.models.load_model("best_model.h5")
13
 
14
  def preprocess(img: Image.Image) -> np.ndarray:
 
18
 
19
  def predict_disease(image: Image.Image) -> str:
20
  try:
21
+ img = preprocess(image)
22
+ preds = model.predict(img)
23
  idx = np.argmax(preds)
24
  label = class_labels[str(idx)]
25
  confidence = preds[0][idx]
26
+ return f"### Prediction Result\n**Disease:** {label}\n**Confidence:** {confidence:.2%}"
27
  except Exception as e:
28
  return f"Prediction error: {e}"
29
 
30
+ css = """
31
+ .gradio-container {
32
+ max-width: 600px;
33
+ margin: auto;
34
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
35
+ }
36
+ h1, h3 {
37
+ color: #2c3e50;
38
+ }
39
+ .gr-button {
40
+ background-color: #27ae60;
41
+ border-radius: 5px;
42
+ border: none;
43
+ color: white;
44
+ font-weight: bold;
45
+ }
46
+ .gr-button:hover {
47
+ background-color: #219150;
48
+ }
49
+ """
50
+
51
  interface = gr.Interface(
52
  fn=predict_disease,
53
+ inputs=gr.Image(type="pil", label="Upload a clear image of a tomato leaf"),
54
+ outputs=gr.Markdown(label="Detection Result"),
55
+ title="🍅 Tomato Leaf Disease Detector",
56
+ description="Upload a tomato leaf image to detect diseases with confidence using a trained TensorFlow model.",
 
57
  allow_flagging="never",
58
+ theme="default",
59
+ css=css,
60
  )
61
 
62
  if __name__ == "__main__":
 
64
 
65
 
66
 
 
 
67
  # ----------------------------------------------------------------------------------------------------------------------
68
 
69
  # import gradio as gr