Spaces:
Sleeping
Sleeping
Add Flask Docker API
Browse files- Dockerfile +18 -0
- README.md +13 -11
- app.py +50 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set work directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install dependencies
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy app files
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Expose API port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Run Flask app
|
| 18 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# Skin Type Classifier API
|
| 2 |
+
|
| 3 |
+
A Flask API for predicting skin type (dry or oily) from images.
|
| 4 |
+
|
| 5 |
+
## Endpoints
|
| 6 |
+
|
| 7 |
+
- `GET /` - Test if API is running
|
| 8 |
+
- `POST /predict` - Upload an image for prediction
|
| 9 |
+
|
| 10 |
+
## Example POST request
|
| 11 |
+
Send a multipart form data with `file` as the key for the image.
|
| 12 |
+
|
| 13 |
+
## Hosted on Hugging Face Spaces
|
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from transformers import AutoModelForImageClassification
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
MODEL_NAME = "anismizi/skin-type-classifier"
|
| 9 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# Define preprocessing
|
| 13 |
+
transform = transforms.Compose([
|
| 14 |
+
transforms.Resize((224, 224)),
|
| 15 |
+
transforms.ToTensor(),
|
| 16 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 17 |
+
std=[0.229, 0.224, 0.225])
|
| 18 |
+
])
|
| 19 |
+
|
| 20 |
+
app = Flask(__name__)
|
| 21 |
+
|
| 22 |
+
@app.route("/")
|
| 23 |
+
def home():
|
| 24 |
+
return jsonify({"message": "Skin Type Classifier API is running!"})
|
| 25 |
+
|
| 26 |
+
@app.route("/predict", methods=["POST"])
|
| 27 |
+
def predict():
|
| 28 |
+
if 'file' not in request.files:
|
| 29 |
+
return jsonify({"error": "No file provided"}), 400
|
| 30 |
+
file = request.files['file']
|
| 31 |
+
try:
|
| 32 |
+
image = Image.open(file.stream).convert("RGB")
|
| 33 |
+
input_tensor = transform(image).unsqueeze(0)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(input_tensor)
|
| 36 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 37 |
+
predicted_class = probabilities.argmax().item()
|
| 38 |
+
confidence = probabilities[0][predicted_class].item()
|
| 39 |
+
|
| 40 |
+
labels = ["dry", "oily"]
|
| 41 |
+
result = {
|
| 42 |
+
"predicted_class": labels[predicted_class],
|
| 43 |
+
"confidence": round(confidence * 100, 2)
|
| 44 |
+
}
|
| 45 |
+
return jsonify(result)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return jsonify({"error": str(e)}), 500
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
Pillow
|
| 5 |
+
transformers
|