Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
| 4 |
|
| 5 |
from fastapi import FastAPI, UploadFile, File
|
| 6 |
-
from transformers import
|
| 7 |
from PIL import Image
|
| 8 |
-
import torch.nn.functional as F
|
| 9 |
import torch
|
|
|
|
| 10 |
import io
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
@app.post("/classify/")
|
| 18 |
async def classify_gender(image: UploadFile = File(...)):
|
|
@@ -21,10 +21,11 @@ async def classify_gender(image: UploadFile = File(...)):
|
|
| 21 |
inputs = processor(images=img, return_tensors="pt")
|
| 22 |
|
| 23 |
with torch.no_grad():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
return
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["HF_HOME"] = "/tmp/huggingface"
|
|
|
|
| 3 |
|
| 4 |
from fastapi import FastAPI, UploadFile, File
|
| 5 |
+
from transformers import SiglipForImageClassification, AutoImageProcessor
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
import torch
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
import io
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
model_name = "prithivMLmods/Gender-Classifier-Mini"
|
| 14 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
| 15 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 16 |
|
| 17 |
@app.post("/classify/")
|
| 18 |
async def classify_gender(image: UploadFile = File(...)):
|
|
|
|
| 21 |
inputs = processor(images=img, return_tensors="pt")
|
| 22 |
|
| 23 |
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
logits = outputs.logits
|
| 26 |
+
probs = F.softmax(logits, dim=1).squeeze().tolist()
|
| 27 |
+
|
| 28 |
+
labels = {"0": "Female ♀", "1": "Male ♂"}
|
| 29 |
+
predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
|
| 30 |
|
| 31 |
+
return predictions
|