| from fastapi import FastAPI, File, UploadFile |
| from transformers import pipeline |
| from PIL import Image |
| import io |
|
|
| app = FastAPI() |
|
|
| |
| classifier = pipeline("zero-shot-image-classification", |
| model="google/siglip2-base-patch16-224", |
| device=-1) |
|
|
| @app.get("/") |
| def read_root(): |
| return {"message": "Fish Detector API is running. Use POST /detect"} |
|
|
| @app.post("/detect") |
| async def detect(file: UploadFile = File(...)): |
| |
| contents = await file.read() |
| image = Image.open(io.BytesIO(contents)).convert("RGB") |
|
|
| |
| candidate_labels = [ |
| "a clear photo of a real fish", |
| "a photo with absolutely no fish in it" |
| ] |
|
|
| |
| predictions = classifier(image, candidate_labels=candidate_labels) |
|
|
| |
| if predictions[0]['label'] == candidate_labels[0]: |
| return "fish" |
| else: |
| return "not a fish" |