Spaces:
Build error
Build error
thijs commited on
Commit ·
c683c65
1
Parent(s): 3830990
add project
Browse files- Dockerfile +11 -0
- main.py +42 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install -r requirements.txt --no-cache-dir
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
ENTRYPOINT ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
from keras.layers import TFSMLayer
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
import os
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
|
| 11 |
+
repo_id = "ThijsVan/MyRepo"
|
| 12 |
+
hf_hub_download(repo_id, filename="config.json", repo_type="model", local_dir="./")
|
| 13 |
+
hf_hub_download(repo_id, filename="metadata.json", repo_type="model", local_dir="./")
|
| 14 |
+
hf_hub_download(repo_id, filename="model.weights.h5", repo_type="model", local_dir="./")
|
| 15 |
+
|
| 16 |
+
model = tf.keras.models.load_model("./")
|
| 17 |
+
|
| 18 |
+
app = FastAPI()
|
| 19 |
+
|
| 20 |
+
app.add_middleware(
|
| 21 |
+
CORSMiddleware,
|
| 22 |
+
allow_origins=["*"],
|
| 23 |
+
allow_credentials=True,
|
| 24 |
+
allow_methods=["*"],
|
| 25 |
+
allow_headers=["*"],
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
ANIMALS = ['Cat', 'Dog', 'Panda']
|
| 29 |
+
|
| 30 |
+
# model_path = os.path.join("INPUT_model_path", "animal-cnn", "model.keras")
|
| 31 |
+
# model = keras.models.load_model(model_path)
|
| 32 |
+
|
| 33 |
+
@app.post('/upload/image')
|
| 34 |
+
async def uploadImage(img: UploadFile = File(...)):
|
| 35 |
+
original_image = Image.open(img.file) # Read the bytes and process as an image
|
| 36 |
+
resized_image = original_image.resize((64, 64)) # Resize
|
| 37 |
+
images_to_predict = np.expand_dims(np.array(resized_image), axis=0) # Our AI Model wanted a list of images, but we only have one, so we expand it's dimension
|
| 38 |
+
predictions = model.predict(images_to_predict) # The result will be a list with predictions in the one-hot encoded format: [ [0 1 0] ]
|
| 39 |
+
prediction_probabilities = predictions
|
| 40 |
+
classifications = prediction_probabilities.argmax(axis=1) # We try to fetch the index of the highest value in this list [ [1] ]
|
| 41 |
+
|
| 42 |
+
return ANIMALS[classifications.tolist()[0]] # Fetch the first item in our classifications array, format it as a list first, result will be e.g.: "Dog"
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
FastApi
|
| 3 |
+
Pillow
|
| 4 |
+
numpy
|
| 5 |
+
uvicorn
|
| 6 |
+
python-multipart
|
| 7 |
+
huggingface_hub
|