AnishKumbhar commited on
Commit
7722141
1 Parent(s): cb17d2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -1,39 +1,28 @@
1
- import fastapi
 
 
2
 
3
- from fastapi import FastAPI, UploadFile, File
4
- from transformers import AutoModelForImageClassification
5
 
6
-
7
- app = FastAPI()
8
-
9
-
10
- app.add_route("/", lambda scope: {"message": "Hello, world!"})
11
-
12
- def preprocess_image(image: UploadFile):
13
  # Resize the image to a fixed size
14
  image = image.resize((224, 224))
15
-
16
- # Convert the image to a NumPy array
17
- image = np.array(image)
18
-
19
  # Normalize the image
20
  image = image / 255.0
21
-
22
  # Return the image
23
  return image
24
 
25
-
26
  @app.post("/predict")
27
- async def predict_endpoint(
28
- image: UploadFile = File(...)
29
- ):
30
  image = preprocess_image(image)
31
 
 
 
32
 
33
- return JSONResponse({"prediction": "0.67 %"})
34
-
35
 
36
- if __name__ == "__main__":
37
- import uvicorn
38
 
39
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI
2
+ from transformers import pipeline
3
+ import numpy as np
4
 
5
+ app = FastAPI(docs_url="/")
 
6
 
7
+ def preprocess_image(image: np.ndarray):
 
 
 
 
 
 
8
  # Resize the image to a fixed size
9
  image = image.resize((224, 224))
 
 
 
 
10
  # Normalize the image
11
  image = image / 255.0
 
12
  # Return the image
13
  return image
14
 
 
15
  @app.post("/predict")
16
+ def predict_endpoint(image: np.ndarray):
17
+ # Preprocess the image
 
18
  image = preprocess_image(image)
19
 
20
+ # Create a pipeline for image classification
21
+ classifier = pipeline("andupets/real-estate-image-classification-30classes")
22
 
23
+ # Classify the image
24
+ result = classifier(image)
25
 
26
+ # Return the classification result
27
+ return result
28