shekzee commited on
Commit
6e77308
·
verified ·
1 Parent(s): d2a46a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -1,24 +1,25 @@
1
- import gradio as gr
2
- import numpy as np
3
  from tensorflow.keras.models import load_model
4
  from PIL import Image
 
 
5
 
6
- # Load the model
 
 
7
  model = load_model("hf_keras_model.keras")
8
  class_names = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street']
9
 
10
- def predict_image(img):
11
- img = img.resize((150, 150))
12
- img_array = np.array(img) / 255.0
 
 
 
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(6)}
16
- return confidences
17
 
18
- gr.Interface(
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)