Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ app = FastAPI()
|
|
15 |
pipe = pipeline(
|
16 |
"image-to-text",
|
17 |
model="HuggingFaceTB/SmolVLM-256M-Instruct",
|
18 |
-
device=-1 # CPU
|
19 |
)
|
20 |
|
21 |
@app.get("/")
|
@@ -41,20 +41,21 @@ def upload_form():
|
|
41 |
@app.post("/predict")
|
42 |
async def predict_gender(file: UploadFile = File(...)):
|
43 |
try:
|
44 |
-
# Read image
|
45 |
image_bytes = await file.read()
|
46 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
47 |
|
48 |
-
#
|
49 |
prompt = "Is the person on this ID male or female?"
|
50 |
-
result = pipe(image, prompt, max_new_tokens=32)
|
51 |
|
52 |
-
#
|
|
|
|
|
|
|
53 |
answer = result[0]["generated_text"].strip()
|
54 |
|
55 |
return JSONResponse({"gender": answer})
|
56 |
|
57 |
except Exception as e:
|
58 |
-
# Print error to logs
|
59 |
traceback.print_exc()
|
60 |
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
15 |
pipe = pipeline(
|
16 |
"image-to-text",
|
17 |
model="HuggingFaceTB/SmolVLM-256M-Instruct",
|
18 |
+
device=-1 # CPU for free tier
|
19 |
)
|
20 |
|
21 |
@app.get("/")
|
|
|
41 |
@app.post("/predict")
|
42 |
async def predict_gender(file: UploadFile = File(...)):
|
43 |
try:
|
44 |
+
# Read uploaded image
|
45 |
image_bytes = await file.read()
|
46 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
47 |
|
48 |
+
# Instruction for the model
|
49 |
prompt = "Is the person on this ID male or female?"
|
|
|
50 |
|
51 |
+
# Run model (pipeline handles image + prompt via generate_kwargs)
|
52 |
+
result = pipe(image, generate_kwargs={"max_new_tokens": 32, "prompt": prompt})
|
53 |
+
|
54 |
+
# Extract model output
|
55 |
answer = result[0]["generated_text"].strip()
|
56 |
|
57 |
return JSONResponse({"gender": answer})
|
58 |
|
59 |
except Exception as e:
|
|
|
60 |
traceback.print_exc()
|
61 |
return JSONResponse({"error": str(e)}, status_code=500)
|