Spaces:
Sleeping
Sleeping
O S I H
commited on
Commit
·
8439d88
1
Parent(s):
01b05f7
upload flask api
Browse files- Dockerfile +15 -0
- api.py +32 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
WORKDIR /code
|
| 7 |
+
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
EXPOSE 5000
|
| 15 |
+
CMD ["python", "api.py"]
|
api.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from huggingface_hub import from_pretrained_keras
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Load the model
|
| 10 |
+
model = from_pretrained_keras("MissingBreath/recycle-garbage-model")
|
| 11 |
+
|
| 12 |
+
# Class labels
|
| 13 |
+
# class_labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
|
| 14 |
+
|
| 15 |
+
@app.route('/classify', methods=['POST'])
|
| 16 |
+
def classify():
|
| 17 |
+
file = request.files['image']
|
| 18 |
+
if file:
|
| 19 |
+
img = Image.open(io.BytesIO(file.read()))
|
| 20 |
+
img = img.resize((128, 128))
|
| 21 |
+
img_array = np.array(img) / 255.0
|
| 22 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 23 |
+
predictions = model.predict(img_array)
|
| 24 |
+
predicted_class_idx = np.argmax(predictions)
|
| 25 |
+
# predicted_class = class_labels[predicted_class_idx]
|
| 26 |
+
# return jsonify({'prediction': predicted_class})
|
| 27 |
+
return jsonify({'prediction': predicted_class_idx})
|
| 28 |
+
else:
|
| 29 |
+
return jsonify({'error': 'No image provided'}), 400
|
| 30 |
+
|
| 31 |
+
if __name__ == '__main__':
|
| 32 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
numpy
|
| 3 |
+
pillow
|
| 4 |
+
huggingface_hub
|