Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
from
|
| 2 |
from huggingface_hub import from_pretrained_keras
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import tensorflow as tf
|
| 7 |
|
| 8 |
-
app =
|
| 9 |
|
| 10 |
# Load the model
|
| 11 |
# model = from_pretrained_keras("MissingBreath/recycle-garbage-model")
|
|
@@ -14,21 +14,21 @@ model = tf.keras.models.load_model('_9217')
|
|
| 14 |
# Class labels
|
| 15 |
# class_labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 16 |
|
| 17 |
-
@app.
|
| 18 |
-
def classify():
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
img = Image.open(io.BytesIO(file.read()))
|
| 22 |
img = img.resize((128, 128))
|
| 23 |
img_array = np.array(img) / 255.0
|
| 24 |
img_array = np.expand_dims(img_array, axis=0)
|
| 25 |
predictions = model.predict(img_array)
|
| 26 |
predicted_class_idx = np.argmax(predictions)
|
| 27 |
# predicted_class = class_labels[predicted_class_idx]
|
| 28 |
-
# return
|
| 29 |
-
return
|
| 30 |
else:
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
if __name__ ==
|
| 34 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from huggingface_hub import from_pretrained_keras
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import tensorflow as tf
|
| 7 |
|
| 8 |
+
app = FastAPI()
|
| 9 |
|
| 10 |
# Load the model
|
| 11 |
# model = from_pretrained_keras("MissingBreath/recycle-garbage-model")
|
|
|
|
| 14 |
# Class labels
|
| 15 |
# class_labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 16 |
|
| 17 |
+
@app.post("/classify")
|
| 18 |
+
async def classify(image: UploadFile = File(...)):
|
| 19 |
+
if image is not None:
|
| 20 |
+
img = Image.open(io.BytesIO(await image.read()))
|
|
|
|
| 21 |
img = img.resize((128, 128))
|
| 22 |
img_array = np.array(img) / 255.0
|
| 23 |
img_array = np.expand_dims(img_array, axis=0)
|
| 24 |
predictions = model.predict(img_array)
|
| 25 |
predicted_class_idx = np.argmax(predictions)
|
| 26 |
# predicted_class = class_labels[predicted_class_idx]
|
| 27 |
+
# return {"prediction": predicted_class}
|
| 28 |
+
return {"prediction": predicted_class_idx}
|
| 29 |
else:
|
| 30 |
+
return {"error": "No image provided"}
|
| 31 |
|
| 32 |
+
# if __name__ == "__main__":
|
| 33 |
+
# import uvicorn
|
| 34 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|