Spaces:
Sleeping
Sleeping
Commit ·
17105d7
1
Parent(s): 13b71db
changed main.py completly
Browse files
main.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
-
import os
|
| 2 |
from fastapi import FastAPI, File, UploadFile
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import tensorflow as tf
|
| 7 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
# CORS
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
@@ -17,41 +17,27 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
-
ANIMALS = ['Cat', 'Dog', 'Panda']
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Download het .keras bestand naar /tmp
|
| 30 |
-
model_path = hf_hub_download(
|
| 31 |
-
repo_id=HF_REPO_ID,
|
| 32 |
-
filename=HF_FILENAME,
|
| 33 |
-
cache_dir="/tmp/hf_cache",
|
| 34 |
-
repo_type="model"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Laad Keras model
|
| 38 |
-
loaded_model = tf.keras.models.load_model(model_path)
|
| 39 |
|
| 40 |
@app.post("/upload/image")
|
| 41 |
async def upload_image(img: UploadFile = File(...)):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return {"classification": classification_label}
|
| 55 |
-
|
| 56 |
-
except Exception as e:
|
| 57 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
import tensorflow as tf
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# CORS-instellingen zodat je frontend makkelijk kan communiceren
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
ANIMALS = ['Cat', 'Dog', 'Panda'] # Labels van je model
|
| 21 |
|
| 22 |
+
# Download en laad het model direct van Hugging Face
|
| 23 |
+
# Vervang 'username/repo_name' met jouw repository naam en 'model_directory' met de folder binnen de repo
|
| 24 |
+
REPO_ID = "RubenVierstraete/masterclass-2025"
|
| 25 |
+
MODEL_DIR = "animal-cnn"
|
| 26 |
|
| 27 |
+
# hf_hub_download zal het model downloaden en pad teruggeven
|
| 28 |
+
model_local_path = hf_hub_download(repo_id=REPO_ID, repo_type="model", subfolder=MODEL_DIR)
|
| 29 |
+
model = tf.keras.models.load_model(model_local_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
@app.post("/upload/image")
|
| 32 |
async def upload_image(img: UploadFile = File(...)):
|
| 33 |
+
# Image openen en preprocessen
|
| 34 |
+
original_image = Image.open(img.file).convert("RGB")
|
| 35 |
+
resized_image = original_image.resize((64, 64))
|
| 36 |
+
image_array = np.expand_dims(np.array(resized_image), axis=0)
|
| 37 |
+
|
| 38 |
+
# Voorspelling
|
| 39 |
+
predictions = model.predict(image_array)
|
| 40 |
+
classification_index = predictions.argmax(axis=1)[0]
|
| 41 |
+
classification_label = ANIMALS[classification_index]
|
| 42 |
+
|
| 43 |
+
return {"prediction": classification_label}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|