panditamey commited on
Commit
807f483
1 Parent(s): ef1eeb9

Create api.py

Browse files
Files changed (1) hide show
  1. api.py +55 -0
api.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI,UploadFile,File
2
+ from pydantic import BaseModel
3
+ import pickle
4
+ import json
5
+ import pandas as pd
6
+ from tensorflow.keras.models import load_model
7
+ from tensorflow.keras.preprocessing import image
8
+ from tensorflow.keras.applications.inception_v3 import preprocess_input
9
+ import numpy as np
10
+ import os
11
+ import gdown
12
+ import lightgbm as lgb
13
+ from PIL import Image
14
+
15
+ CHUNK_SIZE = 1024
16
+
17
+ app = FastAPI(
18
+ title='Flower Classification API',
19
+ description='API for Flower Classification',
20
+ )
21
+
22
+ id = "1ry4L9L1-kyc79F1MnYMemJ5P81Gr_mHP"
23
+ output = "model_flowers_classification.h5"
24
+ gdown.download(id=id, output=output, quiet=False)
25
+ # from zipfile import ZipFile
26
+ # with ZipFile("modelcrops.zip", 'r') as zObject:
27
+ # zObject.extractall(
28
+ # path="")
29
+
30
+
31
+ predict_ml=load_model('model_flowers_classification.h5')
32
+
33
+
34
+ @app.post('/predict')
35
+ async def cropdisease(file: UploadFile = File(...)):
36
+ try:
37
+ contents = file.file.read()
38
+ with open(file.filename, 'wb') as f:
39
+ f.write(contents)
40
+ except Exception:
41
+ return {"message": "There was an error uploading the file"}
42
+ finally:
43
+ file.file.close()
44
+ classes = ['Lilly','Lotus','Orchid','Sunflower', 'Tulip']
45
+ img=image.load_img(str(file.filename),target_size=(224,224))
46
+ x=image.img_to_array(img)
47
+ x=x/255
48
+ img_data=np.expand_dims(x,axis=0)
49
+ prediction = predict_ml.predict(img_data)
50
+ predictions = list(prediction[0])
51
+ max_num = max(predictions)
52
+ index = predictions.index(max_num)
53
+ print(classes[index])
54
+ os.remove(str(file.filename))
55
+ return {"output":classes[index]}