chimithecat commited on
Commit
855d0d2
Β·
verified Β·
1 Parent(s): a422c68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -3,49 +3,69 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
 
6
- # Load your Keras model from Hugging Face
7
- model = pipeline("image-classification", model="wellCh4n/tomato-leaf-disease-classification-vit", top_k=10)
8
 
9
- # Customize your label names here based on your model's training
10
- class_names = [
11
- "Bacterial Spot", "Early Blight", "Healthy", "Late Blight"
12
- ]
 
 
 
 
 
 
 
 
 
13
 
 
 
 
 
 
 
14
  def classify(image):
15
  if image is None:
16
- return None
 
17
  if not isinstance(image, Image.Image):
18
  image = Image.fromarray(image)
19
 
20
- image = image.resize((300, 300)) # πŸ‘ˆ match the model's input shape
21
- img_array = np.array(image) / 255.0
22
  img_array = np.expand_dims(img_array, axis=0)
23
 
24
  predictions = model.predict(img_array)[0]
25
- confidences = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
26
- return confidences
 
27
 
28
- # Build the UI using Blocks
 
 
29
  with gr.Blocks(theme=gr.themes.Soft()) as app:
30
  gr.Markdown(
31
  """
32
- # Analisis Penyakit Tanaman πŸ…
33
- Unggah gambar daun tanaman untuk mengidentifikasi potensi penyakit.
34
  """
35
  )
36
 
37
  with gr.Row():
38
  with gr.Column():
39
- image_input = gr.Image(type="pil", label="Unggah Gambar Daun")
40
- submit_button = gr.Button("Analisis Gambar", variant="primary")
41
-
42
  with gr.Column():
43
- label_output = gr.Label(label="Hasil Analisis")
 
44
 
45
  submit_button.click(
46
  fn=classify,
47
  inputs=image_input,
48
- outputs=label_output,
49
  api_name="predict"
50
  )
51
 
 
3
  import numpy as np
4
  from PIL import Image
5
 
6
+ # Constants
7
+ IMAGE_SIZE = (300, 300)
8
 
9
+ # Class index to label mapping
10
+ CLASS_NAMES = {
11
+ 0: "A healthy tomato leaf",
12
+ 1: "A tomato leaf with Leaf Mold",
13
+ 2: "A tomato leaf with Target Spot",
14
+ 3: "A tomato leaf with Late Blight",
15
+ 4: "A tomato leaf with Early Blight",
16
+ 5: "A tomato leaf with Bacterial Spot",
17
+ 6: "A tomato leaf with Septoria Leaf Spot",
18
+ 7: "A tomato leaf with Tomato Mosaic Virus",
19
+ 8: "A tomato leaf with Tomato Yellow Leaf Curl Virus",
20
+ 9: "A tomato leaf with Spider Mites Two-spotted Spider Mite"
21
+ }
22
 
23
+ # Load the model from Hugging Face Hub
24
+ model_url = "https://huggingface.co/chimithecat/penyakit_tomat/resolve/main/Tomato_Models.h5"
25
+ model_path = tf.keras.utils.get_file("Tomato_Models.h5", model_url)
26
+ model = tf.keras.models.load_model(model_path)
27
+
28
+ # Prediction function
29
  def classify(image):
30
  if image is None:
31
+ return "Tidak ada gambar.", ""
32
+
33
  if not isinstance(image, Image.Image):
34
  image = Image.fromarray(image)
35
 
36
+ img = image.resize(IMAGE_SIZE)
37
+ img_array = np.array(img) / 255.0
38
  img_array = np.expand_dims(img_array, axis=0)
39
 
40
  predictions = model.predict(img_array)[0]
41
+ predicted_index = int(np.argmax(predictions))
42
+ confidence = float(predictions[predicted_index])
43
+ label = CLASS_NAMES[predicted_index]
44
 
45
+ return f"{label}", f"Confidence: {confidence*100:.2f}%"
46
+
47
+ # UI with Gradio Blocks
48
  with gr.Blocks(theme=gr.themes.Soft()) as app:
49
  gr.Markdown(
50
  """
51
+ # πŸ… Tomato Leaf Disease Classifier
52
+ Upload a photo of a tomato leaf to detect its potential disease.
53
  """
54
  )
55
 
56
  with gr.Row():
57
  with gr.Column():
58
+ image_input = gr.Image(type="pil", label="Upload Leaf Image")
59
+ submit_button = gr.Button("Analyze", variant="primary")
60
+
61
  with gr.Column():
62
+ result_output = gr.Text(label="Prediction Result")
63
+ confidence_output = gr.Text(label="Confidence")
64
 
65
  submit_button.click(
66
  fn=classify,
67
  inputs=image_input,
68
+ outputs=[result_output, confidence_output],
69
  api_name="predict"
70
  )
71