divyarspoton commited on
Commit
25139b3
·
1 Parent(s): e9b1c23

Initial age classifier API

Browse files
Files changed (2) hide show
  1. app/main.py +21 -0
  2. requirements.txt +5 -0
app/main.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ from transformers import pipeline
4
+ from PIL import Image
5
+ import io
6
+
7
+ app = FastAPI()
8
+
9
+ # Load the model once when the server starts
10
+ pipe = pipeline("image-classification", model="nateraw/vit-age-classifier")
11
+
12
+ @app.post("/predict")
13
+ async def predict(file: UploadFile = File(...)):
14
+ # Read image file
15
+ image_data = await file.read()
16
+ image = Image.open(io.BytesIO(image_data)).convert("RGB")
17
+
18
+ # Run prediction
19
+ results = pipe(image)
20
+
21
+ return JSONResponse(content={"results": results})
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ fastapi
4
+ uvicorn
5
+ pillow