File size: 1,854 Bytes
b7dec7f
 
 
 
 
 
 
 
 
ae26848
 
 
 
 
 
 
b7dec7f
 
 
ae26848
 
b7dec7f
 
 
 
 
ae26848
b7dec7f
 
 
 
 
 
 
 
 
 
ae26848
b7dec7f
 
 
d7dffb3
 
 
 
 
b7dec7f
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from fastapi import FastAPI, UploadFile, File, HTTPException
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image
import io

app = FastAPI(title="OralScan Model API")

# Load the model globally (simpler method for HF Spaces)
try:
    model = load_model("model.keras")
    print("✅ MobileNetV2 model loaded successfully!")
except Exception as e:
    print(f"❌ Failed to load model: {e}")
    model = None

@app.get("/")
def home():
    if model is None:
        return {"message": "API is running but model failed to load"}
    return {"message": "OralScan Model API is running! Upload image to /predict"}

@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    if model is None:
        raise HTTPException(status_code=500, detail="Model failed to load. Please check logs.")

    try:
        contents = await file.read()
        img = Image.open(io.BytesIO(contents)).convert("RGB")
        img = img.resize((224, 224))
        
        img_array = image.img_to_array(img)
        img_array = np.expand_dims(img_array, axis=0)
        img_array = img_array / 255.0

        predictions = model.predict(img_array, verbose=0)
        predicted_class = int(np.argmax(predictions[0]))
        confidence = float(np.max(predictions[0]) * 100)

        class_names = [
            "Oral Homogenous Leukoplakia",
            "Oral Non-Homogenous Leukoplakia",
            "Other Oral White Lesions"
        ]

        return {
            "predicted_class": predicted_class,
            "class_name": class_names[predicted_class],
            "confidence": round(confidence, 2),
            "message": "Prediction successful"
        }

    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Error processing image: {str(e)}")