Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load model + processor (auto cached inside Spaces)
|
7 |
+
processor = AutoImageProcessor.from_pretrained("prithivMLmods/Realistic-Gender-Classification")
|
8 |
+
model = AutoModelForImageClassification.from_pretrained("prithivMLmods/Realistic-Gender-Classification")
|
9 |
+
|
10 |
+
def predict(image):
|
11 |
+
# Preprocess
|
12 |
+
inputs = processor(images=image, return_tensors="pt")
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].cpu().numpy()
|
16 |
+
|
17 |
+
# Labels
|
18 |
+
labels = list(model.config.id2label.values())
|
19 |
+
|
20 |
+
# Clean dict for FlutterFlow
|
21 |
+
result = {
|
22 |
+
"female": float(probs[labels.index("female portrait")]),
|
23 |
+
"male": float(probs[labels.index("male portrait")])
|
24 |
+
}
|
25 |
+
return result
|
26 |
+
|
27 |
+
# Gradio interface (Spaces auto-hosts this)
|
28 |
+
demo = gr.Interface(
|
29 |
+
fn=predict,
|
30 |
+
inputs=gr.Image(type="pil"),
|
31 |
+
outputs=gr.JSON()
|
32 |
+
)
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
demo.launch()
|