CoolFace
Apppublic

noorSaleem9645/binary_classification

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.py51 linesDownload Raw Back to root
1import numpy as np
2import gradio as gr
3from tensorflow.keras.models import load_model
4from tensorflow.keras.preprocessing import image
5
6# Load trained malaria model
7model = load_model("malaria_model.h5")   # <-- apna trained model ka name yahan likho
8
9print("Model Loaded Successfully!")
10
11def predict(img):
12    # Resize image (same as training size)
13    img = img.resize((150, 150))
14    
15    # Convert to array
16    img_array = image.img_to_array(img) / 255.0
17    img_array = np.expand_dims(img_array, axis=0)
18
19    # Prediction
20    pred = model.predict(img_array)[0][0]
21
22    # Probabilities
23    infected_prob = float(pred)
24    uninfected_prob = 1 - infected_prob
25
26    # Label decision
27    if pred >= 0.5:
28        label = "Parasitized (Malaria Infected)"
29        confidence = infected_prob
30    else:
31        label = "Uninfected"
32        confidence = uninfected_prob
33
34    return {
35        "Uninfected Probability": f"{uninfected_prob * 100:.2f}%",
36        "Parasitized Probability": f"{infected_prob * 100:.2f}%",
37        "Prediction": label,
38        "Confidence": f"{confidence * 100:.2f}%"
39    }
40
41
42# Gradio UI
43app = gr.Interface(
44    fn=predict,
45    inputs=gr.Image(type="pil"),
46    outputs="json",
47    title="Malaria Detection System",
48    description="Upload a blood cell image to detect whether it is infected with malaria or not"
49)
50
51app.launch()