Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,9 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
-
from fastapi import FastAPI
|
| 6 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
-
from fastapi.responses import HTMLResponse
|
| 8 |
-
import threading
|
| 9 |
-
|
| 10 |
|
|
|
|
| 11 |
model = load_model("chest_xray_model.h5")
|
| 12 |
-
threshold = 0.3
|
| 13 |
|
| 14 |
def predict_image(img):
|
| 15 |
img = img.convert("RGB")
|
|
@@ -21,31 +16,11 @@ def predict_image(img):
|
|
| 21 |
confidences = {"NORMAL": float(1 - proba), "PNEUMONIA": float(proba)}
|
| 22 |
return confidences, img
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
fn=predict_image,
|
| 27 |
inputs=gr.Image(type="pil"),
|
| 28 |
outputs=[gr.Label(num_top_classes=2), gr.Image(type="pil")],
|
| 29 |
title="Chest X-ray Pneumonia Classifier",
|
| 30 |
-
description="Upload
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# ===== FastAPI setup =====
|
| 34 |
-
app = FastAPI()
|
| 35 |
-
app.add_middleware(
|
| 36 |
-
CORSMiddleware,
|
| 37 |
-
allow_origins=["*"],
|
| 38 |
-
allow_methods=["*"],
|
| 39 |
-
allow_headers=["*"],
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
@app.get("/", response_class=HTMLResponse)
|
| 44 |
-
def home():
|
| 45 |
-
return gradio_interface.launch(inline=True, share=False, prevent_thread_lock=True)
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
def run_gradio():
|
| 49 |
-
gradio_interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 50 |
-
|
| 51 |
-
threading.Thread(target=run_gradio, daemon=True).start()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
model = load_model("chest_xray_model.h5")
|
|
|
|
| 8 |
|
| 9 |
def predict_image(img):
|
| 10 |
img = img.convert("RGB")
|
|
|
|
| 16 |
confidences = {"NORMAL": float(1 - proba), "PNEUMONIA": float(proba)}
|
| 17 |
return confidences, img
|
| 18 |
|
| 19 |
+
# Gradio app
|
| 20 |
+
gr.Interface(
|
| 21 |
fn=predict_image,
|
| 22 |
inputs=gr.Image(type="pil"),
|
| 23 |
outputs=[gr.Label(num_top_classes=2), gr.Image(type="pil")],
|
| 24 |
title="Chest X-ray Pneumonia Classifier",
|
| 25 |
+
description="Upload a Chest X-ray image to predict NORMAL or PNEUMONIA."
|
| 26 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|