Spaces:
Running
Running
Upload 3 files
Browse files- README.md +26 -11
- app.py +26 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,11 +1,26 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Gender Classification API (Docker)
|
2 |
+
|
3 |
+
This is a Hugging Face Space API using FastAPI + Docker to classify gender from an image using `prithivMLmods/Gender-Classifier-Mini`.
|
4 |
+
|
5 |
+
## Usage
|
6 |
+
|
7 |
+
POST to:
|
8 |
+
|
9 |
+
```
|
10 |
+
https://benstaf-gender-api-fastapi.hf.space/classify/
|
11 |
+
```
|
12 |
+
|
13 |
+
## Example (Python)
|
14 |
+
|
15 |
+
```python
|
16 |
+
import requests
|
17 |
+
|
18 |
+
with open("face.jpg", "rb") as f:
|
19 |
+
r = requests.post("https://benstaf-gender-api-fastapi.hf.space/classify/", files={"image": f})
|
20 |
+
print(r.json())
|
21 |
+
```
|
22 |
+
|
23 |
+
## Deployment
|
24 |
+
|
25 |
+
- SDK: Docker
|
26 |
+
- Port: 7860 (required)
|
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
3 |
+
from PIL import Image
|
4 |
+
import torch.nn.functional as F
|
5 |
+
import torch
|
6 |
+
import io
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Gender-Classifier-Mini")
|
11 |
+
processor = AutoImageProcessor.from_pretrained("prithivMLmods/Gender-Classifier-Mini")
|
12 |
+
|
13 |
+
@app.post("/classify/")
|
14 |
+
async def classify_gender(image: UploadFile = File(...)):
|
15 |
+
contents = await image.read()
|
16 |
+
img = Image.open(io.BytesIO(contents)).convert("RGB")
|
17 |
+
inputs = processor(images=img, return_tensors="pt")
|
18 |
+
|
19 |
+
with torch.no_grad():
|
20 |
+
logits = model(**inputs).logits
|
21 |
+
probs = F.softmax(logits, dim=1)
|
22 |
+
pred = torch.argmax(probs).item()
|
23 |
+
confidence = probs[0][pred].item()
|
24 |
+
label = model.config.id2label[pred]
|
25 |
+
|
26 |
+
return {"label": label, "confidence": confidence}
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
pillow
|