Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,10 @@ import io
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
# `device=-1` forces the model to run on the CPU
|
| 10 |
classifier = pipeline("zero-shot-image-classification",
|
| 11 |
-
model="
|
| 12 |
-
device=-1)
|
| 13 |
|
| 14 |
@app.get("/")
|
| 15 |
def read_root():
|
|
@@ -17,20 +16,20 @@ def read_root():
|
|
| 17 |
|
| 18 |
@app.post("/detect")
|
| 19 |
async def detect(file: UploadFile = File(...)):
|
| 20 |
-
#
|
| 21 |
contents = await file.read()
|
| 22 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
candidate_labels = [
|
| 26 |
"a photo of a fish",
|
| 27 |
"a photo that does not contain a fish"
|
| 28 |
]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
predictions = classifier(image, candidate_labels=candidate_labels)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
if predictions[0]['label'] == "a photo of a fish":
|
| 35 |
return "fish"
|
| 36 |
else:
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Load a compatible, lightweight zero-shot image classification model
|
|
|
|
| 9 |
classifier = pipeline("zero-shot-image-classification",
|
| 10 |
+
model="google/siglip2-base-patch16-224",
|
| 11 |
+
device=-1) # -1 forces CPU usage
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
def read_root():
|
|
|
|
| 16 |
|
| 17 |
@app.post("/detect")
|
| 18 |
async def detect(file: UploadFile = File(...)):
|
| 19 |
+
# Read the uploaded image
|
| 20 |
contents = await file.read()
|
| 21 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 22 |
|
| 23 |
+
# Define the candidate labels for the model to choose from
|
| 24 |
candidate_labels = [
|
| 25 |
"a photo of a fish",
|
| 26 |
"a photo that does not contain a fish"
|
| 27 |
]
|
| 28 |
|
| 29 |
+
# Perform zero-shot classification
|
| 30 |
predictions = classifier(image, candidate_labels=candidate_labels)
|
| 31 |
|
| 32 |
+
# Check if the top prediction is the fish label
|
| 33 |
if predictions[0]['label'] == "a photo of a fish":
|
| 34 |
return "fish"
|
| 35 |
else:
|