Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
from tensorflow.keras.models import load_model
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
import numpy as np
|
|
@@ -8,46 +7,38 @@ import io
|
|
| 8 |
|
| 9 |
app = FastAPI(title="OralScan Model API")
|
| 10 |
|
| 11 |
-
# Load the model
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model = load_model("model.keras")
|
| 19 |
-
print("✅ MobileNetV2 model loaded successfully!")
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"❌ Error loading model: {e}")
|
| 22 |
|
| 23 |
@app.get("/")
|
| 24 |
def home():
|
|
|
|
|
|
|
| 25 |
return {"message": "OralScan Model API is running! Upload image to /predict"}
|
| 26 |
|
| 27 |
@app.post("/predict")
|
| 28 |
async def predict(file: UploadFile = File(...)):
|
| 29 |
if model is None:
|
| 30 |
-
raise HTTPException(status_code=500, detail="Model
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
# Read uploaded image
|
| 34 |
contents = await file.read()
|
| 35 |
img = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 36 |
-
|
| 37 |
-
# Resize to 224x224
|
| 38 |
img = img.resize((224, 224))
|
| 39 |
|
| 40 |
-
# Preprocess
|
| 41 |
img_array = image.img_to_array(img)
|
| 42 |
img_array = np.expand_dims(img_array, axis=0)
|
| 43 |
img_array = img_array / 255.0
|
| 44 |
|
| 45 |
-
|
| 46 |
-
predictions = model.predict(img_array)
|
| 47 |
predicted_class = int(np.argmax(predictions[0]))
|
| 48 |
confidence = float(np.max(predictions[0]) * 100)
|
| 49 |
|
| 50 |
-
# Your actual class names
|
| 51 |
class_names = [
|
| 52 |
"Oral Homogenous Leukoplakia",
|
| 53 |
"Oral Non-Homogenous Leukoplakia",
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
|
|
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
from tensorflow.keras.preprocessing import image
|
| 4 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI(title="OralScan Model API")
|
| 9 |
|
| 10 |
+
# Load the model globally (simpler method for HF Spaces)
|
| 11 |
+
try:
|
| 12 |
+
model = load_model("model.keras")
|
| 13 |
+
print("✅ MobileNetV2 model loaded successfully!")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"❌ Failed to load model: {e}")
|
| 16 |
+
model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
def home():
|
| 20 |
+
if model is None:
|
| 21 |
+
return {"message": "API is running but model failed to load"}
|
| 22 |
return {"message": "OralScan Model API is running! Upload image to /predict"}
|
| 23 |
|
| 24 |
@app.post("/predict")
|
| 25 |
async def predict(file: UploadFile = File(...)):
|
| 26 |
if model is None:
|
| 27 |
+
raise HTTPException(status_code=500, detail="Model failed to load. Please check logs.")
|
| 28 |
|
| 29 |
try:
|
|
|
|
| 30 |
contents = await file.read()
|
| 31 |
img = Image.open(io.BytesIO(contents)).convert("RGB")
|
|
|
|
|
|
|
| 32 |
img = img.resize((224, 224))
|
| 33 |
|
|
|
|
| 34 |
img_array = image.img_to_array(img)
|
| 35 |
img_array = np.expand_dims(img_array, axis=0)
|
| 36 |
img_array = img_array / 255.0
|
| 37 |
|
| 38 |
+
predictions = model.predict(img_array, verbose=0)
|
|
|
|
| 39 |
predicted_class = int(np.argmax(predictions[0]))
|
| 40 |
confidence = float(np.max(predictions[0]) * 100)
|
| 41 |
|
|
|
|
| 42 |
class_names = [
|
| 43 |
"Oral Homogenous Leukoplakia",
|
| 44 |
"Oral Non-Homogenous Leukoplakia",
|