File size: 1,366 Bytes
d4f7659
 
95cca76
 
b8053d8
d4f7659
 
ac6674b
95cca76
ac6674b
95cca76
 
bd2cbcd
95cca76
d4f7659
bd2cbcd
ac6674b
 
bd2cbcd
 
 
95cca76
 
 
 
 
ac6674b
bd2cbcd
b575aee
bd2cbcd
ac6674b
95cca76
b8053d8
bd2cbcd
 
 
 
 
 
d4f7659
95cca76
f199b95
c56efff
95cca76
c56efff
95cca76
 
c56efff
bd2cbcd
 
 
 
 
 
f199b95
c56efff
bd2cbcd
 
 
 
 
95cca76
 
bd2cbcd
95cca76
bd2cbcd
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
import numpy as np
from app.predictor import predict_disease, compute_ai_risk
from app.gradcam import generate_gradcam


def predict(image):
    if image is None:
        return "Upload image first", None

    label, confidence, tensor, original = predict_disease(image)

    # GradCAM
    heatmap = generate_gradcam(tensor, label)

    # ensure numpy
    heatmap = np.array(heatmap)

    # safe normalization
    heatmap = heatmap.astype(np.float32)

    if heatmap.max() > 1:
        heatmap = heatmap / 255.0

    heatmap = np.clip(heatmap, 0, 1)
    heatmap = (heatmap * 255).astype(np.uint8)

    # ensure RGB format
    if len(heatmap.shape) == 2:
        heatmap = np.stack([heatmap]*3, axis=-1)

    risk = compute_ai_risk(label, confidence, heatmap)

    report = (
        "🫀 CardioGuard AI Report\n\n"
        f"Disease: {label}\n"
        f"Confidence: {confidence*100:.2f}%\n"
        f"Risk: {risk}"
    )

    return report, heatmap


demo = gr.Blocks()

with demo:
    gr.Markdown("# 🫀 CardioGuard AI")

    with gr.Row():
        img = gr.Image(type="pil")
        out2 = gr.Image()

    out1 = gr.Textbox(label="Report")

    btn = gr.Button("Analyze")

    btn.click(
        fn=predict,
        inputs=img,
        outputs=[out1, out2]
    )


demo.queue()

demo.launch(
    server_name="0.0.0.0",
    server_port=7860
)