allfahad commited on
Commit
31f6ea8
·
verified ·
1 Parent(s): 419df97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -5,11 +5,10 @@ import io
5
 
6
  app = FastAPI()
7
 
8
- # Load the lightweight zeroshot image classification model
9
- # `device=-1` forces the model to run on the CPU
10
  classifier = pipeline("zero-shot-image-classification",
11
- model="sachin/tiny_clip",
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
- # 1. Read and open the uploaded image
21
  contents = await file.read()
22
  image = Image.open(io.BytesIO(contents)).convert("RGB")
23
 
24
- # 2. Define the two possible labels for the model to choose from
25
  candidate_labels = [
26
  "a photo of a fish",
27
  "a photo that does not contain a fish"
28
  ]
29
 
30
- # 3. Run the zeroshot classifier
31
  predictions = classifier(image, candidate_labels=candidate_labels)
32
 
33
- # 4. The top prediction is the one with the highest score
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: