HashamUllah commited on
Commit
9650572
1 Parent(s): 4b28a17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -50
app.py CHANGED
@@ -1,50 +1,45 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException
2
- from fastapi.responses import JSONResponse
3
- import tensorflow as tf
4
- import numpy as np
5
- from PIL import Image
6
- import io
7
- import json
8
-
9
- app = FastAPI()
10
-
11
- # Load the TensorFlow model
12
- model = tf.keras.models.load_model('./plant_disease_detection.h5')
13
-
14
- # Load categories
15
- with open('./categories.json') as f:
16
- categories = json.load(f)
17
-
18
- def preprocess_image(image_bytes):
19
- # Convert the image to a NumPy array
20
- image = Image.open(io.BytesIO(image_bytes))
21
- image = image.resize((224, 224)) # Adjust size as needed
22
- image_array = np.array(image) / 255.0 # Normalize to [0, 1]
23
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
24
- return image_array
25
-
26
- @app.post('/predict')
27
- async def predict(file: UploadFile = File(...)):
28
- if file.content_type.startswith('image/') is False:
29
- raise HTTPException(status_code=400, detail='Invalid file type')
30
-
31
- image_bytes = await file.read()
32
- image_array = preprocess_image(image_bytes)
33
-
34
- # Make prediction
35
- predictions = model.predict(image_array)
36
- predicted_class = np.argmax(predictions, axis=1)[0]
37
-
38
- # Map to category names
39
- predicted_label = categories.get(str(predicted_class), 'Unknown')
40
-
41
- return JSONResponse(content={
42
- 'class': predicted_label,
43
- 'confidence': float(predictions[0][predicted_class])
44
- })
45
-
46
-
47
- if __name__ == '__main__':
48
- import uvicorn
49
- uvicorn.run(app, host='0.0.0.0', port=8080)
50
-
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import io
6
+ import json
7
+
8
+ # Load the TensorFlow model
9
+ model = tf.keras.models.load_model('./plant_disease_detection.h5')
10
+
11
+ # Load categories
12
+ with open('./categories.json') as f:
13
+ categories = json.load(f)
14
+
15
+ def preprocess_image(image):
16
+ # Convert the image to a NumPy array
17
+ image = image.resize((224, 224)) # Adjust size as needed
18
+ image_array = np.array(image) / 255.0 # Normalize to [0, 1]
19
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
20
+ return image_array
21
+
22
+ def predict(image):
23
+ image_array = preprocess_image(image)
24
+
25
+ # Make prediction
26
+ predictions = model.predict(image_array)
27
+ predicted_class = np.argmax(predictions, axis=1)[0]
28
+
29
+ # Map to category names
30
+ predicted_label = categories.get(str(predicted_class), 'Unknown')
31
+
32
+ return predicted_label, float(predictions[0][predicted_class])
33
+
34
+ # Create a Gradio interface
35
+ iface = gr.Interface(
36
+ fn=predict,
37
+ inputs=gr.Image(type="pil"),
38
+ outputs=[gr.Label(), gr.Number()],
39
+ title="Plant Disease Detection",
40
+ description="Upload an image of a plant leaf to detect if it has any diseases."
41
+ )
42
+
43
+ # Launch the interface
44
+ if __name__ == "__main__":
45
+ iface.launch()