AnishKumbhar commited on
Commit
fbb5992
1 Parent(s): 800536e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,32 +1,39 @@
1
- from fastapi import FastAPI
2
  from transformers import pipeline
3
- import numpy as np
4
- from pydantic import BaseModel, Field
5
 
6
- app = FastAPI(docs_url="/")
 
 
7
 
8
- def preprocess_image(image: np.ndarray):
 
9
  # Resize the image to a fixed size
10
  image = image.resize((224, 224))
 
 
 
 
11
  # Normalize the image
12
  image = image / 255.0
 
13
  # Return the image
14
  return image
15
 
16
- class Image(BaseModel):
17
- image: np.ndarray = Field(...)
18
-
19
  @app.post("/predict")
20
- def predict_endpoint(image: Image):
21
  # Preprocess the image
22
- image = preprocess_image(image.image)
23
 
24
- # Create a pipeline for image classification
25
- classifier = pipeline("image-classification")
26
 
27
- # Classify the image
28
- result = classifier(image)
29
 
30
- # Return the classification result
31
- return result
 
32
 
 
 
1
+ import fastapi
2
  from transformers import pipeline
3
+ import pickle
 
4
 
5
+ # Load the model from the pickle file
6
+ with open("model.pkl", "rb") as f:
7
+ model = pickle.load(f)
8
 
9
+ # Define a function to preprocess the image
10
+ def preprocess_image(image):
11
  # Resize the image to a fixed size
12
  image = image.resize((224, 224))
13
+
14
+ # Convert the image to a NumPy array
15
+ image = np.array(image)
16
+
17
  # Normalize the image
18
  image = image / 255.0
19
+
20
  # Return the image
21
  return image
22
 
23
+ # Define an endpoint to predict the output
 
 
24
  @app.post("/predict")
25
+ async def predict_endpoint(image: fastapi.File):
26
  # Preprocess the image
27
+ image = preprocess_image(image)
28
 
29
+ # Make a prediction
30
+ prediction = model(image)
31
 
32
+ # Return the prediction
33
+ return {"prediction": prediction}
34
 
35
+ # Start the FastAPI app
36
+ if _name_ == "_main_":
37
+ import uvicorn
38
 
39
+ uvicorn.run(app, host="0.0.0.0", port=8000)