Spaces:
Sleeping
Sleeping
Upload model cnn
Browse files- app.py +68 -0
- model_cnn.h5 +3 -0
- requirements.txt +102 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
from tensorflow.keras.preprocessing import image
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"], # sesuaikan jika perlu
|
| 16 |
+
allow_credentials=True,
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
MODEL_PATH = 'model_cnn.h5'
|
| 22 |
+
IMG_HEIGHT = 224
|
| 23 |
+
IMG_WIDTH = 224
|
| 24 |
+
|
| 25 |
+
class_names = [
|
| 26 |
+
'freshapples', 'freshbanana', 'freshbittergroud', 'freshcapsicum', 'freshcucumber',
|
| 27 |
+
'freshokra', 'freshoranges', 'freshpotato', 'freshtomato',
|
| 28 |
+
'rottenapples', 'rottenbanana', 'rottenbittergroud', 'rottencapsicum', 'rottencucumber',
|
| 29 |
+
'rottenokra', 'rottenoranges', 'rottenpotato', 'rottentomato'
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
model = load_model(MODEL_PATH)
|
| 33 |
+
|
| 34 |
+
def read_imagefile(file) -> Image.Image:
|
| 35 |
+
image = Image.open(BytesIO(file)).convert("RGB")
|
| 36 |
+
return image
|
| 37 |
+
|
| 38 |
+
def predict(img: Image.Image):
|
| 39 |
+
img = img.resize((IMG_WIDTH, IMG_HEIGHT))
|
| 40 |
+
img_array = image.img_to_array(img)
|
| 41 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 42 |
+
|
| 43 |
+
prediction = model.predict(img_array)
|
| 44 |
+
predicted_class = np.argmax(prediction[0])
|
| 45 |
+
confidence = float(prediction[0][predicted_class]) * 100
|
| 46 |
+
|
| 47 |
+
return class_names[predicted_class], confidence
|
| 48 |
+
|
| 49 |
+
@app.post("/predict")
|
| 50 |
+
async def predict_image(file: UploadFile = File(...)):
|
| 51 |
+
try:
|
| 52 |
+
img = read_imagefile(await file.read())
|
| 53 |
+
pred_class, confidence = predict(img)
|
| 54 |
+
response = {
|
| 55 |
+
"filename": file.filename,
|
| 56 |
+
"title": "Prediction Result",
|
| 57 |
+
"message": f"The item is classified as: {pred_class}",
|
| 58 |
+
"confidence": f"{confidence:.2f}",
|
| 59 |
+
"details": [f"Class: {pred_class}", f"Confidence: {confidence:.2f}%"]
|
| 60 |
+
}
|
| 61 |
+
return JSONResponse(content=response)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 64 |
+
|
| 65 |
+
# Komentar atau hapus bagian run uvicorn karena Spaces otomatis menjalankan app
|
| 66 |
+
# if __name__ == "__main__":
|
| 67 |
+
# import uvicorn
|
| 68 |
+
# uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|
model_cnn.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6b9ec1e00e08442f5f94e95379b8040afe19c7ea218e00fab0096e7a679d09fb
|
| 3 |
+
size 5539336
|
requirements.txt
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
absl-py==2.3.0
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.9.0
|
| 4 |
+
appnope==0.1.4
|
| 5 |
+
asttokens==3.0.0
|
| 6 |
+
astunparse==1.6.3
|
| 7 |
+
certifi==2025.4.26
|
| 8 |
+
charset-normalizer==3.4.2
|
| 9 |
+
chex==0.1.89
|
| 10 |
+
click==8.2.1
|
| 11 |
+
comm==0.2.2
|
| 12 |
+
debugpy==1.8.14
|
| 13 |
+
decorator==5.2.1
|
| 14 |
+
etils==1.12.2
|
| 15 |
+
executing==2.2.0
|
| 16 |
+
fastapi==0.115.12
|
| 17 |
+
flatbuffers==25.2.10
|
| 18 |
+
flax==0.10.4
|
| 19 |
+
fsspec==2025.5.1
|
| 20 |
+
gast==0.6.0
|
| 21 |
+
google-pasta==0.2.0
|
| 22 |
+
grpcio==1.72.1
|
| 23 |
+
h11==0.16.0
|
| 24 |
+
h5py==3.13.0
|
| 25 |
+
humanize==4.12.3
|
| 26 |
+
idna==3.10
|
| 27 |
+
importlib_resources==6.5.2
|
| 28 |
+
ipykernel==6.29.5
|
| 29 |
+
ipython==9.3.0
|
| 30 |
+
ipython_pygments_lexers==1.1.1
|
| 31 |
+
jax==0.4.34
|
| 32 |
+
jaxlib==0.4.34
|
| 33 |
+
jedi==0.19.2
|
| 34 |
+
jupyter_client==8.6.3
|
| 35 |
+
jupyter_core==5.8.1
|
| 36 |
+
keras==3.10.0
|
| 37 |
+
libclang==18.1.1
|
| 38 |
+
Markdown==3.8
|
| 39 |
+
markdown-it-py==3.0.0
|
| 40 |
+
MarkupSafe==3.0.2
|
| 41 |
+
matplotlib-inline==0.1.7
|
| 42 |
+
mdurl==0.1.2
|
| 43 |
+
ml-dtypes==0.3.2
|
| 44 |
+
msgpack==1.1.0
|
| 45 |
+
namex==0.1.0
|
| 46 |
+
nest-asyncio==1.6.0
|
| 47 |
+
numpy==1.26.4
|
| 48 |
+
opt_einsum==3.4.0
|
| 49 |
+
optax==0.2.4
|
| 50 |
+
optree==0.16.0
|
| 51 |
+
orbax-checkpoint==0.11.5
|
| 52 |
+
packaging==23.2
|
| 53 |
+
pandas==2.3.0
|
| 54 |
+
parso==0.8.4
|
| 55 |
+
pexpect==4.9.0
|
| 56 |
+
pillow==11.2.1
|
| 57 |
+
platformdirs==4.3.8
|
| 58 |
+
prompt_toolkit==3.0.51
|
| 59 |
+
protobuf==4.25.8
|
| 60 |
+
psutil==7.0.0
|
| 61 |
+
ptyprocess==0.7.0
|
| 62 |
+
pure_eval==0.2.3
|
| 63 |
+
pydantic==2.11.5
|
| 64 |
+
pydantic_core==2.33.2
|
| 65 |
+
Pygments==2.19.1
|
| 66 |
+
python-dateutil==2.9.0.post0
|
| 67 |
+
python-multipart==0.0.20
|
| 68 |
+
pytz==2025.2
|
| 69 |
+
PyYAML==6.0.2
|
| 70 |
+
pyzmq==26.4.0
|
| 71 |
+
requests==2.32.3
|
| 72 |
+
rich==14.0.0
|
| 73 |
+
scipy==1.15.3
|
| 74 |
+
simplejson==3.20.1
|
| 75 |
+
six==1.17.0
|
| 76 |
+
sniffio==1.3.1
|
| 77 |
+
stack-data==0.6.3
|
| 78 |
+
starlette==0.46.2
|
| 79 |
+
tensorboard==2.16.2
|
| 80 |
+
tensorboard-data-server==0.7.2
|
| 81 |
+
tensorflow==2.16.1
|
| 82 |
+
tensorflow-hub==0.16.1
|
| 83 |
+
tensorflow-io-gcs-filesystem==0.37.1
|
| 84 |
+
tensorflow_decision_forests==1.9.0
|
| 85 |
+
tensorflowjs==4.22.0
|
| 86 |
+
tensorstore==0.1.74
|
| 87 |
+
termcolor==3.1.0
|
| 88 |
+
tf_keras==2.16.0
|
| 89 |
+
toolz==1.0.0
|
| 90 |
+
tornado==6.5.1
|
| 91 |
+
traitlets==5.14.3
|
| 92 |
+
treescope==0.1.9
|
| 93 |
+
typing-inspection==0.4.1
|
| 94 |
+
typing_extensions==4.14.0
|
| 95 |
+
tzdata==2025.2
|
| 96 |
+
urllib3==2.4.0
|
| 97 |
+
uvicorn==0.34.3
|
| 98 |
+
wcwidth==0.2.13
|
| 99 |
+
Werkzeug==3.1.3
|
| 100 |
+
wrapt==1.17.2
|
| 101 |
+
wurlitzer==3.1.1
|
| 102 |
+
zipp==3.22.0
|