Spaces:
Runtime error
Runtime error
AnishKumbhar
commited on
Commit
•
fbb5992
1
Parent(s):
800536e
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,39 @@
|
|
1 |
-
|
2 |
from transformers import pipeline
|
3 |
-
import
|
4 |
-
from pydantic import BaseModel, Field
|
5 |
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
|
|
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 |
-
|
17 |
-
image: np.ndarray = Field(...)
|
18 |
-
|
19 |
@app.post("/predict")
|
20 |
-
def predict_endpoint(image:
|
21 |
# Preprocess the image
|
22 |
-
image = preprocess_image(image
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
|
|
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)
|