chimithecat commited on
Commit
90554df
·
verified ·
1 Parent(s): dcea028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -29
app.py CHANGED
@@ -1,29 +1,55 @@
1
- import gradio as gr
2
- import tensorflow as tf
3
- import numpy as np
4
- from PIL import Image
5
-
6
- # Download and load model
7
- model_url = "https://huggingface.co/chimithecat/penyakit_tomat/resolve/main/Tomato_Models.h5"
8
- model_path = tf.keras.utils.get_file("Tomato_Models.h5", model_url)
9
- model = tf.keras.models.load_model(model_path)
10
-
11
- # Define class names (update based on your training labels)
12
- class_names = [
13
- "Bacterial Spot", "Early Blight", "Healthy", "Late Blight"
14
- ]
15
-
16
- def predict(img: Image.Image):
17
- img = img.resize((224, 224)) # Resize to match training size
18
- img = np.array(img) / 255.0
19
- img = np.expand_dims(img, axis=0) # Add batch dimension
20
- predictions = model.predict(img)[0]
21
- return {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
22
-
23
- gr.Interface(
24
- fn=predict,
25
- inputs=gr.Image(type="pil"),
26
- outputs=gr.Label(num_top_classes=4),
27
- title="Tomato Leaf Disease Classifier",
28
- description="Upload a tomato leaf image to detect its disease"
29
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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_url = "https://huggingface.co/chimithecat/penyakit_tomat/resolve/main/Tomato_Models.h5"
8
+ model_path = tf.keras.utils.get_file("Tomato_Models.h5", model_url)
9
+ model = tf.keras.models.load_model(model_path)
10
+
11
+ # Customize your label names here based on your model's training
12
+ class_names = [
13
+ "Bacterial Spot", "Early Blight", "Healthy", "Late Blight"
14
+ ]
15
+
16
+ # Predict function for a single image
17
+ def classify(image):
18
+ if image is None:
19
+ return None
20
+ if not isinstance(image, Image.Image):
21
+ image = Image.fromarray(image)
22
+
23
+ image = image.resize((224, 224)) # Resize to expected input
24
+ img_array = np.array(image) / 255.0
25
+ img_array = np.expand_dims(img_array, axis=0)
26
+
27
+ predictions = model.predict(img_array)[0]
28
+ confidences = {class_names[i]: float(predictions[i]) for i in range(len(class_names))}
29
+ return confidences
30
+
31
+ # Build the UI using Blocks
32
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
33
+ gr.Markdown(
34
+ """
35
+ # Analisis Penyakit Tanaman 🍅
36
+ Unggah gambar daun tanaman untuk mengidentifikasi potensi penyakit.
37
+ """
38
+ )
39
+
40
+ with gr.Row():
41
+ with gr.Column():
42
+ image_input = gr.Image(type="pil", label="Unggah Gambar Daun")
43
+ submit_button = gr.Button("Analisis Gambar", variant="primary")
44
+
45
+ with gr.Column():
46
+ label_output = gr.Label(label="Hasil Analisis")
47
+
48
+ submit_button.click(
49
+ fn=classify,
50
+ inputs=image_input,
51
+ outputs=label_output,
52
+ api_name="predict"
53
+ )
54
+
55
+ app.launch(show_api=True)