allfahad commited on
Commit
d1cafde
·
verified ·
1 Parent(s): 67a7e1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -23
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 CLIP model and processor directly using the pipeline
10
- # This happens once when your Space starts up
11
  classifier = pipeline("zero-shot-image-classification",
12
- model="openai/clip-vit-base-patch32",
13
- device= -1) # The '-1' forces it to use the CPU
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 read_root():
25
- return {"message": "Fish Detector API is running. Send POST to /detect"}
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. Use the pipeline to run zero-shot classification
34
- # We use the 'candidate_labels' we defined above
 
 
 
 
 
 
 
35
  predictions = classifier(image, candidate_labels=candidate_labels)
36
 
37
- # 3. Determine the result
38
- # The top prediction is the one with the highest score.
39
- # We check if its label is 'a photo of a fish'.
40
- is_fish = predictions[0]['label'] == 'a photo of a fish'
41
 
42
- # 4. Return the appropriate text response
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: