Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,25 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
from tensorflow.keras.models import load_model
|
4 |
from PIL import Image
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
7 |
model = load_model("hf_keras_model.keras")
|
8 |
class_names = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street']
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
14 |
preds = model.predict(img_array)[0]
|
15 |
-
confidences = {class_names[i]: float(preds[i]) for i in range(
|
16 |
-
return confidences
|
17 |
|
18 |
-
|
19 |
-
fn=predict_image,
|
20 |
-
inputs=gr.Image(type="pil"),
|
21 |
-
outputs=gr.Label(num_top_classes=3),
|
22 |
-
title="Intel Image Classifier",
|
23 |
-
description="Upload a landscape image and get predictions (buildings, forest, glacier, etc.)"
|
24 |
-
).launch()
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
from tensorflow.keras.models import load_model
|
4 |
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
import io
|
7 |
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Load model and class names
|
11 |
model = load_model("hf_keras_model.keras")
|
12 |
class_names = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street']
|
13 |
|
14 |
+
@app.post("/predict/")
|
15 |
+
async def predict(file: UploadFile = File(...)):
|
16 |
+
contents = await file.read()
|
17 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
18 |
+
image = image.resize((150, 150))
|
19 |
+
img_array = np.array(image) / 255.0
|
20 |
img_array = np.expand_dims(img_array, axis=0)
|
21 |
+
|
22 |
preds = model.predict(img_array)[0]
|
23 |
+
confidences = {class_names[i]: float(preds[i]) for i in range(len(class_names))}
|
|
|
24 |
|
25 |
+
return JSONResponse(content=confidences)
|
|
|
|
|
|
|
|
|
|
|
|