Spaces:
Runtime error
Runtime error
AnishKumbhar
commited on
Commit
•
29c50fd
1
Parent(s):
d8d951f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fastapi
|
2 |
+
from transformers import pipeline
|
3 |
+
import pickle
|
4 |
+
|
5 |
+
# Load the model from the pickle file
|
6 |
+
with open("model_finetuned.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)
|