Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,85 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import uvicorn
|
| 4 |
-
from classifier import Classifier
|
| 5 |
-
from model import Model
|
| 6 |
-
from nlp import NLP
|
| 7 |
-
import logging
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
-
nlp = NLP()
|
| 13 |
-
router = APIRouter()
|
| 14 |
-
classifier = Classifier()
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
-
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import base64
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import numpy as np
|
| 6 |
+
from fastapi import FastAPI
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from keras.models import load_model
|
| 9 |
+
from keras.utils import img_to_array
|
| 10 |
+
from tensorflow.keras.utils import load_img, img_to_array
|
| 11 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Load the Keras model
|
| 14 |
+
model = load_model('modelMobileNetNew.h5')
|
| 15 |
|
| 16 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
class ImageData(BaseModel):
|
| 19 |
+
message: str
|
| 20 |
|
| 21 |
+
def base64_to_pil(img_base64):
|
| 22 |
+
image_data = re.sub('data:image/.+;base64,', '', str(img_base64))
|
| 23 |
+
pil_image = Image.open(BytesIO(base64.b64decode(image_data)))
|
| 24 |
+
return pil_image
|
| 25 |
|
| 26 |
+
def model_predict(img, model):
|
| 27 |
+
img = img.resize((224, 224))
|
| 28 |
+
x = img_to_array(img)
|
| 29 |
+
x = x.reshape(-1, 224, 224, 3)
|
| 30 |
+
x = x.astype('float32')
|
| 31 |
+
x = x / 255.0
|
| 32 |
+
preds = model.predict(x)
|
| 33 |
+
return preds
|
| 34 |
|
| 35 |
+
target_names = ['australopithecus_aferensis',
|
| 36 |
+
'badak_bercula_satu',
|
| 37 |
+
'batu_lempung_krisikan',
|
| 38 |
+
'fauna_darat_semedo',
|
| 39 |
+
'fragmen_tengkorak',
|
| 40 |
+
'fragmen_tengkorak_parential',
|
| 41 |
+
'fragmen_tengkorak_sambung_macan',
|
| 42 |
+
'fragmen_tulang_fermur_bawah',
|
| 43 |
+
'gigi_buaya',
|
| 44 |
+
'gigi_gajah',
|
| 45 |
+
'gigi_hiu',
|
| 46 |
+
'homo_erectus_ngawi',
|
| 47 |
+
'homo_habilis',
|
| 48 |
+
'homo_sapiens_wajak',
|
| 49 |
+
'kapak_penetak',
|
| 50 |
+
'kapak_primbas']
|
| 51 |
+
|
| 52 |
+
display_names = ['Australopithecus Aferensis',
|
| 53 |
+
'Badak Bercula Satu',
|
| 54 |
+
'Batu Lempung Krisikan',
|
| 55 |
+
'Fauna Darat Semedo',
|
| 56 |
+
'Fragmen Tengkorak',
|
| 57 |
+
'Fragmen Tengkorak Parential',
|
| 58 |
+
'Fragmen Tengkorak Sambung_macan',
|
| 59 |
+
'Fragmen Tulang Fermur Bawah',
|
| 60 |
+
'Gigi Buaya',
|
| 61 |
+
'Gigi Gajah',
|
| 62 |
+
'Gigi Hiu',
|
| 63 |
+
'Homo Erectus Ngawi',
|
| 64 |
+
'Homo Habilis',
|
| 65 |
+
'Homo Sapiens Wajak',
|
| 66 |
+
'Kapak Penetak',
|
| 67 |
+
'Kapak Primbas']
|
| 68 |
+
|
| 69 |
+
label_mapping = dict(zip(target_names, display_names))
|
| 70 |
+
|
| 71 |
+
@app.post("/predict")
|
| 72 |
+
async def predict(image_data: ImageData):
|
| 73 |
+
try:
|
| 74 |
+
data = image_data.message
|
| 75 |
+
img = base64_to_pil(data)
|
| 76 |
+
pred = model_predict(img, model)
|
| 77 |
+
hasil_label = label_mapping[target_names[np.argmax(pred)]]
|
| 78 |
+
hasil_prob = "{:.2f}".format(100 * np.max(pred))
|
| 79 |
+
return {'message': 'Berhasil melakukan prediksi', 'nama': hasil_label, 'probability': hasil_prob}
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print("EXCEPTION in Predict POST")
|
| 82 |
+
return {'message': str(e), 'nama': '', 'probability': ''}
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|