Spaces:
Sleeping
Sleeping
PRANJAL KAR commited on
Commit ·
e08d9b8
1
Parent(s): bf915b1
Hf Commit
Browse files- .gitignore +1 -0
- Dockerfile +11 -0
- efficientnetb3-Plant Village Disease-99.71.h5 +3 -0
- main.py +119 -0
- requirements.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
e/
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
efficientnetb3-Plant Village Disease-99.71.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:25d6e7bd3d261061d40f574c9d15bce41a83345764f1bfae01fa8bc1bb14eadd
|
| 3 |
+
size 135055192
|
main.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import uvicorn
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import numpy as np
|
| 6 |
+
from fastapi import FastAPI, File, UploadFile
|
| 7 |
+
from starlette.middleware.cors import CORSMiddleware
|
| 8 |
+
from tensorflow.keras.models import load_model
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from tensorflow.keras.applications.vgg16 import preprocess_input
|
| 11 |
+
|
| 12 |
+
# Initialize FastAPI app
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Add CORS middleware to allow cross-origin requests
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"], # Adjust this to limit which domains can make requests
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
# Check if GPU is available and use it for TensorFlow operations
|
| 23 |
+
gpus = tf.config.experimental.list_physical_devices('GPU')
|
| 24 |
+
if gpus:
|
| 25 |
+
try:
|
| 26 |
+
# Currently, memory growth needs to be the same across GPUs
|
| 27 |
+
for gpu in gpus:
|
| 28 |
+
tf.config.experimental.set_memory_growth(gpu, True)
|
| 29 |
+
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
|
| 30 |
+
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
|
| 31 |
+
except RuntimeError as e:
|
| 32 |
+
# Memory growth must be set before GPUs have been initialized
|
| 33 |
+
print(e)
|
| 34 |
+
|
| 35 |
+
# Load the pre-trained model during app startup
|
| 36 |
+
model = load_model('efficientnetb3-Plant Village Disease-99.71.h5') # Replace with your actual model path
|
| 37 |
+
|
| 38 |
+
# Define class labels
|
| 39 |
+
class_labels = {
|
| 40 |
+
0: 'Apple___Apple_scab',
|
| 41 |
+
1: 'Apple___Black_rot',
|
| 42 |
+
2: 'Apple___Cedar_apple_rust',
|
| 43 |
+
3: 'Apple___healthy',
|
| 44 |
+
4: 'Blueberry___healthy',
|
| 45 |
+
5: 'Cherry_(including_sour)___Powdery_mildew',
|
| 46 |
+
6: 'Cherry_(including_sour)___healthy',
|
| 47 |
+
7: 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
|
| 48 |
+
8: 'Corn_(maize)___Common_rust_',
|
| 49 |
+
9: 'Corn_(maize)___Northern_Leaf_Blight',
|
| 50 |
+
10: 'Corn_(maize)___healthy',
|
| 51 |
+
11: 'Grape___Black_rot',
|
| 52 |
+
12: 'Grape___Esca_(Black_Measles)',
|
| 53 |
+
13: 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
|
| 54 |
+
14: 'Grape___healthy',
|
| 55 |
+
15: 'Orange___Haunglongbing_(Citrus_greening)',
|
| 56 |
+
16: 'Peach___Bacterial_spot',
|
| 57 |
+
17: 'Peach___healthy',
|
| 58 |
+
18: 'Pepper,_bell___Bacterial_spot',
|
| 59 |
+
19: 'Pepper,_bell___healthy',
|
| 60 |
+
20: 'Potato___Early_blight',
|
| 61 |
+
21: 'Potato___Late_blight',
|
| 62 |
+
22: 'Potato___healthy',
|
| 63 |
+
23: 'Raspberry___healthy',
|
| 64 |
+
24: 'Soybean___healthy',
|
| 65 |
+
25: 'Squash___Powdery_mildew',
|
| 66 |
+
26: 'Strawberry___Leaf_scorch',
|
| 67 |
+
27: 'Strawberry___healthy',
|
| 68 |
+
28: 'Tomato___Bacterial_spot',
|
| 69 |
+
29: 'Tomato___Early_blight',
|
| 70 |
+
30: 'Tomato___Late_blight',
|
| 71 |
+
31: 'Tomato___Leaf_Mold',
|
| 72 |
+
32: 'Tomato___Septoria_leaf_spot',
|
| 73 |
+
33: 'Tomato___Spider_mites Two-spotted_spider_mite',
|
| 74 |
+
34: 'Tomato___Target_Spot',
|
| 75 |
+
35: 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
|
| 76 |
+
36: 'Tomato___Tomato_mosaic_virus',
|
| 77 |
+
37: 'Tomato___healthy'
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# Define a route to accept single image uploads and make predictions
|
| 82 |
+
@app.post("/predict/")
|
| 83 |
+
async def predict_single_image(file: UploadFile):
|
| 84 |
+
try:
|
| 85 |
+
# Read the uploaded image
|
| 86 |
+
image = await file.read()
|
| 87 |
+
img = Image.open(io.BytesIO(image))
|
| 88 |
+
|
| 89 |
+
# Model Params
|
| 90 |
+
img_size = (224, 224)
|
| 91 |
+
channels = 3 # either BGR or Grayscale
|
| 92 |
+
color = 'rgb'
|
| 93 |
+
img_shape = (img_size[0], img_size[1], channels)
|
| 94 |
+
|
| 95 |
+
# Preprocess the image to match the input requirements of your model
|
| 96 |
+
img = img.resize((224, 224)) # Adjust the size as needed
|
| 97 |
+
img = np.asarray(img)
|
| 98 |
+
img = preprocess_input(img)
|
| 99 |
+
|
| 100 |
+
# Make a prediction using your pre-trained model
|
| 101 |
+
predictions = model.predict(np.expand_dims(img, axis=0))
|
| 102 |
+
|
| 103 |
+
# Convert prediction indices to class labels
|
| 104 |
+
predicted_class_index = np.argmax(predictions)
|
| 105 |
+
predicted_class_label = class_labels.get(predicted_class_index, "Unknown")
|
| 106 |
+
|
| 107 |
+
# You can format the predictions as needed and return them
|
| 108 |
+
result = {
|
| 109 |
+
"predicted_class": predicted_class_label,
|
| 110 |
+
"class_probabilities": predictions.tolist(),
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
return result
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
return {"error": str(e)}
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
uvicorn.run(app, port=8000)
|
requirements.txt
ADDED
|
Binary file (2.04 kB). View file
|
|
|