Spaces:
Runtime error
Runtime error
AnishKumbhar
commited on
Commit
•
8b7cda3
1
Parent(s):
9396581
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,49 @@
|
|
1 |
import fastapi
|
2 |
-
|
|
|
3 |
from transformers import AutoModelForImageClassification
|
|
|
|
|
4 |
app = FastAPI()
|
|
|
|
|
5 |
# Load the model from the local file
|
6 |
-
model = AutoModelForImageClassification.from_pretrained(
|
|
|
|
|
|
|
7 |
|
8 |
# Define a function to preprocess the image
|
9 |
-
def preprocess_image(image):
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
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(
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
# Return the prediction
|
32 |
-
return {"prediction": prediction}
|
33 |
|
34 |
# Start the FastAPI app
|
35 |
if __name__ == "__main__":
|
36 |
-
|
37 |
|
38 |
-
|
|
|
1 |
import fastapi
|
2 |
+
|
3 |
+
from fastapi import FastAPI, UploadFile, File
|
4 |
from transformers import AutoModelForImageClassification
|
5 |
+
|
6 |
+
|
7 |
app = FastAPI()
|
8 |
+
|
9 |
+
|
10 |
# Load the model from the local file
|
11 |
+
model = AutoModelForImageClassification.from_pretrained(
|
12 |
+
"andupets/real-estate-image-classification-30classes"
|
13 |
+
)
|
14 |
+
|
15 |
|
16 |
# Define a function to preprocess the image
|
17 |
+
def preprocess_image(image: UploadFile):
|
18 |
+
# Resize the image to a fixed size
|
19 |
+
image = image.resize((224, 224))
|
20 |
|
21 |
+
# Convert the image to a NumPy array
|
22 |
+
image = np.array(image)
|
23 |
|
24 |
+
# Normalize the image
|
25 |
+
image = image / 255.0
|
26 |
+
|
27 |
+
# Return the image
|
28 |
+
return image
|
29 |
|
|
|
|
|
30 |
|
31 |
# Define an endpoint to predict the output
|
32 |
@app.post("/predict")
|
33 |
+
async def predict_endpoint(
|
34 |
+
image: UploadFile = File(...)
|
35 |
+
): # Preprocess the image
|
36 |
+
image = preprocess_image(image)
|
37 |
+
|
38 |
+
# Make a prediction
|
39 |
+
prediction = model(image)
|
40 |
|
41 |
+
# Return the prediction
|
42 |
+
return {"prediction": prediction}
|
43 |
|
|
|
|
|
44 |
|
45 |
# Start the FastAPI app
|
46 |
if __name__ == "__main__":
|
47 |
+
import uvicorn
|
48 |
|
49 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|