Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +10 -0
- README.md +13 -5
- app.py +45 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY requirements.txt ./
|
6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
7 |
+
|
8 |
+
COPY . .
|
9 |
+
|
10 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
@@ -1,10 +1,18 @@
|
|
1 |
---
|
2 |
-
title: Realistic Gender
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Realistic Gender Classification API
|
3 |
+
emoji: 🖼️
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: purple
|
6 |
sdk: docker
|
7 |
+
app_file: app.py
|
8 |
pinned: false
|
9 |
---
|
10 |
|
11 |
+
# Realistic Gender Classification API
|
12 |
+
|
13 |
+
This is a FastAPI service that uses [prithivMLmods/Realistic-Gender-Classification](https://huggingface.co/prithivMLmods/Realistic-Gender-Classification) to classify gender from images.
|
14 |
+
|
15 |
+
## Endpoints
|
16 |
+
|
17 |
+
- `/` → Upload form (HTML)
|
18 |
+
- `/predict` → POST an image and get gender probabilities (JSON)
|
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
3 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
4 |
+
|
5 |
+
import io
|
6 |
+
import torch
|
7 |
+
from fastapi import FastAPI, File, UploadFile
|
8 |
+
from fastapi.responses import JSONResponse, HTMLResponse
|
9 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
# Load model and processor
|
13 |
+
processor = AutoImageProcessor.from_pretrained("prithivMLmods/Realistic-Gender-Classification")
|
14 |
+
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Realistic-Gender-Classification")
|
15 |
+
|
16 |
+
# FastAPI app
|
17 |
+
app = FastAPI()
|
18 |
+
|
19 |
+
@app.get("/", response_class=HTMLResponse)
|
20 |
+
async def home():
|
21 |
+
return '''
|
22 |
+
<html>
|
23 |
+
<body>
|
24 |
+
<h2>Upload an Image for Gender Detection</h2>
|
25 |
+
<form action="/predict" enctype="multipart/form-data" method="post">
|
26 |
+
<input name="file" type="file" accept="image/*">
|
27 |
+
<input type="submit" value="Upload">
|
28 |
+
</form>
|
29 |
+
</body>
|
30 |
+
</html>
|
31 |
+
'''
|
32 |
+
|
33 |
+
@app.post("/predict")
|
34 |
+
async def predict(file: UploadFile = File(...)):
|
35 |
+
image = Image.open(io.BytesIO(await file.read())).convert("RGB")
|
36 |
+
inputs = processor(images=image, return_tensors="pt")
|
37 |
+
|
38 |
+
with torch.no_grad():
|
39 |
+
logits = model(**inputs).logits
|
40 |
+
probs = torch.nn.functional.softmax(logits, dim=-1).cpu().numpy()[0]
|
41 |
+
|
42 |
+
labels = model.config.id2label
|
43 |
+
result = {labels[i]: float(probs[i]) for i in range(len(labels))}
|
44 |
+
|
45 |
+
return JSONResponse(content=result)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
pillow
|
6 |
+
python-multipart
|