Frist Test
Browse files- Dockerfile +13 -0
- app.py +55 -0
- d.sh +3 -0
- keras_model.h5 +3 -0
- labels.txt +2 -0
- requirements.txt +8 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
FROM python:3.10
|
4 |
+
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
COPY ./requirements.txt /code/requirements.txt
|
8 |
+
RUN pip install --upgrade pip
|
9 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
10 |
+
|
11 |
+
COPY . .
|
12 |
+
|
13 |
+
CMD ["gunicorn","-b","0.0.0.0:7860","--timeout","600" ,"app:app"]
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from keras.models import load_model
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Load the Keras model
|
11 |
+
model = load_model('keras_model.h5') # Replace 'your_model.h5' with the path to your .h5 file
|
12 |
+
|
13 |
+
# Function to preprocess the input image
|
14 |
+
def preprocess_image(img):
|
15 |
+
img = img.resize((224, 224)) # Assuming input size of 224x224
|
16 |
+
img_array = np.array(img)
|
17 |
+
img_array = img_array.astype('float32') / 255 # Normalization
|
18 |
+
img_array = np.expand_dims(img_array, axis=0)
|
19 |
+
return img_array
|
20 |
+
|
21 |
+
# Define a function to predict the class of an image
|
22 |
+
def predict_class(img):
|
23 |
+
processed_image = preprocess_image(img)
|
24 |
+
prediction = model.predict(processed_image)
|
25 |
+
return prediction
|
26 |
+
|
27 |
+
@app.post("/predict/")
|
28 |
+
async def predict(file: UploadFile = File(...)):
|
29 |
+
contents = await file.read()
|
30 |
+
img = Image.open(io.BytesIO(contents))
|
31 |
+
prediction = predict_class(img)
|
32 |
+
|
33 |
+
# Assuming your model output is a list of probabilities for each class
|
34 |
+
# You may need to modify this based on your model's output
|
35 |
+
prediction = prediction.tolist()[0]
|
36 |
+
|
37 |
+
# Assuming you have two classes: Blight disease and Powdery mildew
|
38 |
+
# Modify this based on your actual class names
|
39 |
+
class_names = ["Blight disease on grape leaves", "Powdery mildew on grapes"]
|
40 |
+
result = {"prediction": class_names[np.argmax(prediction)], "probabilities": prediction}
|
41 |
+
return result
|
42 |
+
|
43 |
+
# Allow CORS (Cross-Origin Resource Sharing) for all origins
|
44 |
+
app.add_middleware(
|
45 |
+
CORSMiddleware,
|
46 |
+
allow_origins=["*"],
|
47 |
+
allow_credentials=True,
|
48 |
+
allow_methods=["GET", "POST", "OPTIONS"],
|
49 |
+
allow_headers=["*"],
|
50 |
+
)
|
51 |
+
|
52 |
+
# Handle OPTIONS requests
|
53 |
+
@app.options("/predict/")
|
54 |
+
async def options_predict():
|
55 |
+
return {"methods": ["POST"]}
|
d.sh
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git add .
|
2 |
+
git commit -m "Frist Test"
|
3 |
+
git push
|
keras_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:92e8795ce70d47fbc85f9b578a54246ec53f22f2471c2ee6d9c31d79a4400f03
|
3 |
+
size 2453432
|
labels.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
0 Blight disease on grape leaves
|
2 |
+
1 Powdery mildew on grapes
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
flask_cors
|
3 |
+
gunicorn
|
4 |
+
uvicorn
|
5 |
+
keras
|
6 |
+
tensorflow
|
7 |
+
numpy
|
8 |
+
pillow
|