Spaces:
Runtime error
Runtime error
create project
Browse files- .env +1 -0
- Dockerfile +10 -0
- app.py +47 -0
- requirements.txt +9 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
MODEL_PATH=animal-classification/INPUT_model_path/animal-cnn/model.keras
|
Dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR .
|
| 4 |
+
COPY . .
|
| 5 |
+
|
| 6 |
+
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
EXPOSE 8000
|
| 9 |
+
|
| 10 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import os
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# Make sure it allows all the requests for the CORSMiddleware.
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
ANIMALS = ["Cat", "Dog", "Panda"]
|
| 24 |
+
|
| 25 |
+
# It would've been better to use an environment variable to fix this line actually...
|
| 26 |
+
model_path = os.getenv("MODEL_PATH")
|
| 27 |
+
print(model_path)
|
| 28 |
+
model = tf.keras.models.load_model(model_path)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.post('/upload/image')
|
| 32 |
+
async def uploadImage(img: UploadFile = File(...)):
|
| 33 |
+
original_image = Image.open(img.file)
|
| 34 |
+
resized_image = original_image.resize((64,64))
|
| 35 |
+
images_to_predict = np.expand_dims(np.array(resized_image), axis=0)
|
| 36 |
+
predictions = model.predict(images_to_predict)
|
| 37 |
+
prediction_probabilities = predictions
|
| 38 |
+
classifications = prediction_probabilities.argmax(axis=1)
|
| 39 |
+
|
| 40 |
+
print(predictions)
|
| 41 |
+
print(classifications)
|
| 42 |
+
print(classifications.tolist()[0])
|
| 43 |
+
|
| 44 |
+
return ANIMALS[classifications.tolist()[0]]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# python --version 3.12.9
|
| 2 |
+
|
| 3 |
+
fastapi==0.115.12
|
| 4 |
+
numpy==2.1.3
|
| 5 |
+
tensorflow==2.19.0
|
| 6 |
+
pillow==11.2.1
|
| 7 |
+
uvicorn==0.34.2
|
| 8 |
+
python-multipart==0.0.20
|
| 9 |
+
python-dotenv==1.1.0
|