Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,43 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
-
import torch
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# Load the
|
| 10 |
-
# This
|
| 11 |
classifier = pipeline("zero-shot-image-classification",
|
| 12 |
-
model="
|
| 13 |
-
device=
|
| 14 |
-
|
| 15 |
-
# These are the candidate labels for your task.
|
| 16 |
-
# The model will assign a score to each one.
|
| 17 |
-
candidate_labels = [
|
| 18 |
-
"a photo of a fish",
|
| 19 |
-
"a photo of a person", # Adding a non-fish example can help
|
| 20 |
-
"a photo of a tree"
|
| 21 |
-
]
|
| 22 |
|
| 23 |
@app.get("/")
|
| 24 |
-
def
|
| 25 |
-
return {"message": "Fish Detector API is running.
|
| 26 |
|
| 27 |
@app.post("/detect")
|
| 28 |
async def detect(file: UploadFile = File(...)):
|
| 29 |
-
# 1. Read the image file
|
| 30 |
contents = await file.read()
|
| 31 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 32 |
|
| 33 |
-
# 2.
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
predictions = classifier(image, candidate_labels=candidate_labels)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
is_fish = predictions[0]['label'] == 'a photo of a fish'
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if is_fish:
|
| 44 |
return "fish"
|
| 45 |
else:
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
from fastapi import FastAPI, File, UploadFile
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Load the model pipeline only once when the Space starts.
|
| 10 |
+
# This makes it ready for all incoming requests.
|
| 11 |
classifier = pipeline("zero-shot-image-classification",
|
| 12 |
+
model="google/vit-base-patch16-224",
|
| 13 |
+
device=-1) # -1 forces the model to use the CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
+
def root():
|
| 17 |
+
return {"message": "Fish Detector API is running. Use POST /detect"}
|
| 18 |
|
| 19 |
@app.post("/detect")
|
| 20 |
async def detect(file: UploadFile = File(...)):
|
| 21 |
+
# 1. Read and open the image from the uploaded file
|
| 22 |
contents = await file.read()
|
| 23 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 24 |
|
| 25 |
+
# 2. Define the labels for zero-shot classification.
|
| 26 |
+
# These are the categories our model will choose from.
|
| 27 |
+
candidate_labels = [
|
| 28 |
+
"a photo of a fish, with fins and scales",
|
| 29 |
+
"a photo of a person, an animal, or an object that is not a fish",
|
| 30 |
+
"an empty photo with no fish in it"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# 3. Run the image through the pipeline to get predictions
|
| 34 |
predictions = classifier(image, candidate_labels=candidate_labels)
|
| 35 |
|
| 36 |
+
# 4. The top prediction is the one with the highest score.
|
| 37 |
+
# We check if its label is the fish-related one.
|
| 38 |
+
is_fish = predictions[0]['label'] == candidate_labels[0]
|
|
|
|
| 39 |
|
| 40 |
+
# 5. Return the result as a plain text string
|
| 41 |
if is_fish:
|
| 42 |
return "fish"
|
| 43 |
else:
|