RubenVierstraete commited on
Commit
17105d7
·
1 Parent(s): 13b71db

changed main.py completly

Browse files
Files changed (1) hide show
  1. main.py +22 -36
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 instellen
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
- HF_REPO_ID = "RubenVierstraete/mlops2025"
23
- HF_FILENAME = "model.keras"
 
 
24
 
25
- # Zorg dat HF Hub cache volledig in /tmp staat
26
- os.environ["HF_HOME"] = "/tmp/hf_hub"
27
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_hub"
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
- try:
43
- # Afbeelding verwerken
44
- image = Image.open(img.file).convert("RGB")
45
- image = image.resize((64, 64))
46
- image_array = np.array(image).astype(np.float32)
47
- image_array = np.expand_dims(image_array, axis=0)
48
-
49
- # Voorspelling
50
- predictions = loaded_model.predict(image_array)
51
- classification_index = predictions.argmax(axis=1)[0]
52
- classification_label = ANIMALS[classification_index]
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}