File size: 4,345 Bytes
6d598a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Gradio interface for Build 3: Explainable Decision Support Tool."""

import gradio as gr

try:
    import spaces
except ImportError:
    # Local/Colab fallback. Hugging Face ZeroGPU supplies the real package.
    class _SpacesFallback:
        @staticmethod
        def GPU(*args, **kwargs):
            def decorator(function):
                return function

            return decorator

    spaces = _SpacesFallback()

from inference_engine import infer, render_report
from knowledge_base import NO, UNKNOWN, YES


CHOICES = [UNKNOWN, YES, NO]


@spaces.GPU(duration=1)
def zerogpu_startup_probe() -> str:
    """Register one bounded GPU function so the free ZeroGPU runtime can start.

    The application itself is a CPU-only rule engine and never calls this
    function. Keeping the probe separate prevents ordinary analyses from
    entering a GPU queue or consuming the user's ZeroGPU interaction quota.
    """

    return "ZeroGPU runtime registered"


def analyze(
    backend_running: str,
    health_check_passed: str,
    request_reached_server: str,
    cors_error_visible: str,
    api_key_present: str,
    status_code: str,
) -> tuple[str, dict]:
    facts = {
        "backend_running": backend_running,
        "health_check_passed": health_check_passed,
        "request_reached_server": request_reached_server,
        "cors_error_visible": cors_error_visible,
        "api_key_present": api_key_present,
        "status_code": status_code,
    }
    result = infer(facts)
    visible_facts = {
        **result.input_facts,
        **{f"derived:{key}": value for key, value in result.derived_facts.items()},
    }
    return render_report(result), visible_facts


def clear_form():
    return (UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, "", "", {})


with gr.Blocks(title="Developer Connection Troubleshooter") as demo:
    gr.Markdown(
        """
        # Developer Connection Troubleshooter

        A small knowledge-based decision-support system. It represents observations
        as facts, applies explicit rules, and shows an inference trace. Do not enter
        an actual API key; report only whether a required credential is present.
        """
    )

    with gr.Row():
        with gr.Column():
            backend_running = gr.Dropdown(
                CHOICES, value=UNKNOWN, label="Is the backend running?"
            )
            health_check_passed = gr.Dropdown(
                CHOICES, value=UNKNOWN, label="Does the health endpoint respond?"
            )
            request_reached_server = gr.Dropdown(
                CHOICES, value=UNKNOWN, label="Did the request reach the server?"
            )
        with gr.Column():
            cors_error_visible = gr.Dropdown(
                CHOICES, value=UNKNOWN, label="Is a CORS error visible?"
            )
            api_key_present = gr.Dropdown(
                CHOICES, value=UNKNOWN, label="Is the required credential present?"
            )
            status_code = gr.Textbox(
                label="HTTP status code",
                placeholder="For example: 200, 401, 404, or 500",
                max_lines=1,
            )

    with gr.Row():
        analyze_button = gr.Button("Analyze evidence", variant="primary")
        clear_button = gr.Button("Clear")

    with gr.Row():
        report = gr.Markdown(label="Reasoning report")
        represented_facts = gr.JSON(label="Represented facts")

    gr.Examples(
        examples=[
            [YES, YES, YES, NO, NO, "401"],
            [YES, YES, NO, YES, UNKNOWN, ""],
            [NO, YES, UNKNOWN, UNKNOWN, UNKNOWN, ""],
            [YES, YES, YES, NO, YES, "200"],
        ],
        inputs=[
            backend_running,
            health_check_passed,
            request_reached_server,
            cors_error_visible,
            api_key_present,
            status_code,
        ],
        label="Test cases",
    )

    inputs = [
        backend_running,
        health_check_passed,
        request_reached_server,
        cors_error_visible,
        api_key_present,
        status_code,
    ]
    analyze_button.click(analyze, inputs=inputs, outputs=[report, represented_facts])
    clear_button.click(
        clear_form,
        outputs=[*inputs, report, represented_facts],
        queue=False,
    )


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