File size: 1,954 Bytes
fa2cb8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import time
from src.pipeline import generate_report
from src.tools_loader import get_tools

# Pre-load models/tools once to avoid cold start delays
_ = get_tools()

def process_inputs(target_variable: str, image_path: str):
    """Gradio callback to generate SHAP explanation report."""
    if not image_path:
        return "**Please upload a SHAP summary plot image to begin.**"
    if not target_variable.strip():
        return "**Please enter a target variable (e.g., life expectancy).**"

    start = time.time()
    report = generate_report(target_variable.strip(), image_path)
    elapsed = time.time() - start

    return f"""### SHAP Explanation Report for **{target_variable.strip()}**

{report}

---
*Generated in {elapsed:.1f} seconds*
"""

# Gradio App Interface
with gr.Blocks(
    theme=gr.themes.Soft(),
    title="SHAP Summary Plot Explainer",
    css="""
    .input-section { max-width: 600px; margin: 0 auto; }
    .report-output { margin-top: 30px; }
    """
) as demo:

    # Header
    gr.Markdown("# SHAP Summary Plot Explainer\n\nUpload a SHAP plot and specify your prediction target to get a detailed explanation.")

    with gr.Column(elem_classes=["input-section"]):
        target_input = gr.Textbox(
            label="Target Variable",
            placeholder="e.g., life expectancy, credit score, disease risk..."
        )
        shap_image = gr.Image(
            type="filepath",
            label="Upload SHAP Summary Plot Image",
            height=350
        )
        generate_button = gr.Button("Generate Explanation", variant="primary")

    with gr.Column(elem_classes=["report-output"]):
        report_output = gr.Markdown("**Awaiting input...**")

    # Link inputs to callback
    generate_button.click(
        fn=process_inputs,
        inputs=[target_input, shap_image],
        outputs=report_output,
        show_progress="full"
    )

if __name__ == "__main__":
    demo.launch()