aniket-manhas commited on
Commit
70cb3e6
·
verified ·
1 Parent(s): c8d5463

Upload 6 files

Browse files
Files changed (6) hide show
  1. .gitattributes +34 -1
  2. .gitignore +0 -1
  3. README.md +6 -47
  4. app.py +58 -53
  5. potato_disease_model.h5 +3 -0
  6. requirements.txt +0 -0
.gitattributes CHANGED
@@ -1,2 +1,35 @@
 
 
 
 
 
 
 
1
  *.h5 filter=lfs diff=lfs merge=lfs -text
2
- best_model.keras filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore CHANGED
@@ -1 +0,0 @@
1
- myenv
 
 
README.md CHANGED
@@ -1,53 +1,12 @@
1
  ---
2
- title: Potato Leaf Disease Detection
3
- emoji: 🥔
4
- colorFrom: green
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.13.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- # Potato Leaf Disease Detection
13
-
14
- This model detects diseases in potato leaves, classifying them into three categories:
15
- - Early Blight
16
- - Late Blight
17
- - Healthy
18
-
19
- ## Usage
20
-
21
- ### Web Interface
22
- The model is available through a Gradio web interface. Simply run:
23
- ```bash
24
- python app.py
25
- ```
26
- Then open your browser to `http://localhost:7860`
27
-
28
- ### API Usage
29
- You can also use the model through an API endpoint:
30
-
31
- ```python
32
- import requests
33
-
34
- # Replace with your Hugging Face Space URL
35
- API_URL = "https://your-space-url.hf.space/predict"
36
-
37
- # Load your image
38
- with open("path_to_image.jpg", "rb") as f:
39
- image_data = f.read()
40
-
41
- # Make prediction
42
- response = requests.post(API_URL, files={"file": image_data})
43
- prediction = response.json()["prediction"]
44
- print(f"Prediction: {prediction}")
45
- ```
46
-
47
- ## Model Details
48
- - Input: 256x256 RGB image of a potato leaf
49
- - Output: Classification into one of three categories
50
- - Model: TensorFlow/Keras model
51
-
52
- ## Requirements
53
- See `requirements.txt` for all dependencies.
 
1
  ---
2
+ title: Potato
3
+ emoji: 🚀
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 5.29.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,61 +1,66 @@
1
  import gradio as gr
2
- from fastapi import FastAPI, File, UploadFile
3
- from tensorflow.keras.models import load_model
4
- from tensorflow.keras.preprocessing import image
5
  import numpy as np
 
 
6
  from PIL import Image
7
- import io
8
- import uvicorn
9
- import tensorflow as tf
10
-
11
- # Configure TensorFlow to use CPU only
12
- tf.config.set_visible_devices([], 'GPU')
13
-
14
- # Load model with custom_objects to handle version compatibility
15
- MODEL_PATH = "best_model.keras"
16
- try:
17
- model = load_model(MODEL_PATH, compile=False)
18
- except Exception as e:
19
- print(f"Error loading model: {str(e)}")
20
- # Try loading with custom_objects
21
- model = load_model(MODEL_PATH, compile=False, custom_objects={'tf': tf})
22
-
23
- class_names = ["Early Blight", "Late Blight", "Healthy"]
24
-
25
- # Preprocess image
26
- def preprocess(img):
27
- if isinstance(img, str): # Handle file path
28
- img = Image.open(img)
29
- elif isinstance(img, bytes): # Handle bytes
30
- img = Image.open(io.BytesIO(img))
31
-
32
- img = img.resize((256, 256))
33
- img_array = image.img_to_array(img) / 255.0
34
- img_array = np.expand_dims(img_array, axis=0)
35
- prediction = model.predict(img_array, verbose=0) # Added verbose=0 to reduce output
36
- class_index = np.argmax(prediction[0])
37
- return class_names[class_index]
38
-
39
- # Create FastAPI app
40
- app = FastAPI(title="Potato Leaf Disease Detection API")
41
-
42
- @app.post("/predict")
43
- async def predict(file: UploadFile = File(...)):
44
- contents = await file.read()
45
- prediction = preprocess(contents)
46
- return {"prediction": prediction}
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Gradio Interface
49
  interface = gr.Interface(
50
- fn=preprocess,
51
- inputs=gr.Image(type="pil"),
52
- outputs=gr.Label(),
53
- title="Potato Leaf Disease Detection",
54
- description="Upload a potato leaf image to classify as Early Blight, Late Blight, or Healthy."
55
  )
56
 
57
- # Mount Gradio app
58
- app = gr.mount_gradio_app(app, interface, path="/")
59
-
60
- if __name__ == "__main__":
61
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import gradio as gr
 
 
 
2
  import numpy as np
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
  from PIL import Image
6
+ import requests
7
+ import os
8
+
9
+ # Constants
10
+ MODEL_URL = "https://storage.googleapis.com/potato-model-detection/potato_disease_model.h5"
11
+ MODEL_PATH = "potato_disease_model.h5"
12
+ CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]
13
+
14
+ def download_model():
15
+ """Download the model if not already available locally."""
16
+ if not os.path.exists(MODEL_PATH):
17
+ print("Downloading model...")
18
+ response = requests.get(MODEL_URL)
19
+ if response.status_code == 200:
20
+ with open(MODEL_PATH, 'wb') as f:
21
+ f.write(response.content)
22
+ print("Model downloaded successfully.")
23
+ else:
24
+ raise Exception(f"Failed to download model: HTTP {response.status_code}")
25
+
26
+ def load_disease_model(path):
27
+ """Load the trained Keras model."""
28
+ try:
29
+ return load_model(path)
30
+ except Exception as e:
31
+ raise RuntimeError(f"Error loading model: {e}")
32
+
33
+ def preprocess_image(image):
34
+ """Convert and resize image for model input."""
35
+ image = image.convert("RGB").resize((256, 256))
36
+ img_array = img_to_array(image) / 255.0
37
+ return np.expand_dims(img_array, axis=0)
38
+
39
+ def predict(image):
40
+ """Make a prediction and return class probabilities."""
41
+ try:
42
+ img_array = preprocess_image(image)
43
+ prediction = model.predict(img_array)[0]
44
+
45
+ # Normalize if not already
46
+ if not np.isclose(np.sum(prediction), 1.0, atol=1e-2):
47
+ prediction = prediction / np.sum(prediction)
48
+
49
+ return {CLASS_NAMES[i]: round(float(pred), 4) for i, pred in enumerate(prediction)}
50
+ except Exception as e:
51
+ return {"error": str(e)}
52
+
53
+ # Ensure model is ready
54
+ download_model()
55
+ model = load_disease_model(MODEL_PATH)
56
 
57
  # Gradio Interface
58
  interface = gr.Interface(
59
+ fn=predict,
60
+ inputs=gr.Image(type="pil", label="Upload Leaf Image"),
61
+ outputs=gr.Label(num_top_classes=3, label="Prediction"),
62
+ title="otato Leaf Disease Detection",
63
+ description="Upload an image of a potato leaf to detect Early Blight, Late Blight, or Healthy condition.",
64
  )
65
 
66
+ interface.launch()
 
 
 
 
potato_disease_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a0ea2599967005ff4ddec606ab9693d2823ccb4cd7cfe33bc293887d1c060ba6
3
+ size 712308576
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ