Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fastapi
|
2 |
+
from transformers import pipeline
|
3 |
+
import h5py
|
4 |
+
|
5 |
+
# Load the model from the h5 file
|
6 |
+
model = h5py.File("model_finetuned.h5", "r")["model"]
|
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 |
+
# Convert the image to a NumPy array
|
13 |
+
image = np.array(image)
|
14 |
+
# Normalize the image
|
15 |
+
image = image / 255.0
|
16 |
+
# Return the image
|
17 |
+
return image
|
18 |
+
|
19 |
+
# Define an endpoint to predict the output
|
20 |
+
@app.post("/predict")
|
21 |
+
async def predict_endpoint(image: fastapi.File):
|
22 |
+
# Preprocess the image
|
23 |
+
image = preprocess_image(image)
|
24 |
+
# Make a prediction
|
25 |
+
prediction = model.predict(image)
|
26 |
+
# Return the prediction
|
27 |
+
return {"prediction": prediction}
|
28 |
+
|
29 |
+
# Start the FastAPI app
|
30 |
+
if __name__ == "__main__":
|
31 |
+
import uvicorn
|
32 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|