Spaces:
Runtime error
Runtime error
AnishKumbhar
commited on
Commit
•
4499fe6
1
Parent(s):
cc9c350
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,38 @@
|
|
1 |
import fastapi
|
2 |
-
|
3 |
-
import
|
4 |
|
5 |
-
# Load the model from the
|
6 |
-
|
7 |
-
model = pickle.load(f)
|
8 |
|
9 |
# Define a function to preprocess the image
|
10 |
def preprocess_image(image):
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
|
23 |
# Define an endpoint to predict the output
|
24 |
@app.post("/predict")
|
25 |
async def predict_endpoint(image: fastapi.File):
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
|
35 |
# Start the FastAPI app
|
36 |
if _name_ == "_main_":
|
37 |
-
|
38 |
|
39 |
-
|
|
|
1 |
import fastapi
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForImageClassification
|
4 |
|
5 |
+
# Load the model from the local file
|
6 |
+
model = AutoModelForImageClassification.from_pretrained("./model.ckpt")
|
|
|
7 |
|
8 |
# Define a function to preprocess the image
|
9 |
def preprocess_image(image):
|
10 |
+
# Resize the image to a fixed size
|
11 |
+
image = image.resize((224, 224))
|
12 |
|
13 |
+
# Convert the image to a NumPy array
|
14 |
+
image = np.array(image)
|
15 |
|
16 |
+
# Normalize the image
|
17 |
+
image = image / 255.0
|
18 |
|
19 |
+
# Return the image
|
20 |
+
return image
|
21 |
|
22 |
# Define an endpoint to predict the output
|
23 |
@app.post("/predict")
|
24 |
async def predict_endpoint(image: fastapi.File):
|
25 |
+
# Preprocess the image
|
26 |
+
image = preprocess_image(image)
|
27 |
|
28 |
+
# Make a prediction
|
29 |
+
prediction = model(image)
|
30 |
|
31 |
+
# Return the prediction
|
32 |
+
return {"prediction": prediction}
|
33 |
|
34 |
# Start the FastAPI app
|
35 |
if _name_ == "_main_":
|
36 |
+
import uvicorn
|
37 |
|
38 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|