File size: 6,974 Bytes
8e86148
f64c1bc
 
8e86148
f64c1bc
 
 
 
 
2cf9064
ae30584
 
 
 
 
 
2cf9064
 
ae30584
 
2cf9064
 
ae30584
 
2cf9064
f64c1bc
 
 
2cf9064
8e86148
f64c1bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae30584
f64c1bc
 
 
 
 
 
ae30584
f64c1bc
ae30584
 
 
2cf9064
 
f64c1bc
 
 
2cf9064
 
ae30584
2cf9064
f64c1bc
ae30584
 
 
 
 
 
 
2cf9064
f64c1bc
2cf9064
f64c1bc
 
 
8e86148
ae30584
f64c1bc
ae30584
8e86148
f64c1bc
 
 
 
ae30584
 
f64c1bc
ae30584
8e86148
f64c1bc
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os

# --- 1. DYNAMIC API ROUTING ---
# Pulls the live AMD IP from Hugging Face Settings
API_URL = os.environ.get("INTELLIGUARD_API", "http://127.0.0.1:8000/scan")

# Custom CSS
custom_css = """
.gradio-container { background-color: #06090f !important; color: white !important; }
.fake-sidebar { 
    background: #0b0f19 !important; 
    border-right: 1px solid #1f2937 !important; 
    padding: 20px !important;
    min-width: 250px !important;
}
.status-badge { 
    background: rgba(16, 185, 129, 0.1); border: 1px solid #10b981; color: #10b981; 
    padding: 8px; border-radius: 20px; text-align: center; margin-bottom: 30px;
}
.card { 
    background: #111827; padding: 20px; border-radius: 12px; text-align: center;
    border-top: 3px solid #3b82f6; margin-bottom: 10px;
}
.establish-btn { background: linear-gradient(90deg, #3b82f6 0%, #00d4ff 100%) !important; color: white !important; font-weight: bold !important;}
/* Fix for dark mode inputs */
input, textarea { color: white !important; background-color: #161b22 !important; }
"""

# --- 2. THE BRAIN LOGIC ---
def process_query(query, tot_count, thr_count, saf_count, current_log):
    if not query.strip():
        return tot_count, thr_count, saf_count, "", "Please enter a query.", current_log, current_log, gr.update(), gr.update(), gr.update()

    tot_count += 1
    
    try:
        # Send payload to AMD Server
        res = requests.post(API_URL, json={"text": query}, timeout=15)
        data = res.json()
        verdict = data.get("verdict", "ERROR")
        category = data.get("attack_category", "Unknown")
        score = data.get("score", 0.0)
    except Exception as e:
        verdict = "ERROR"
        category = "Connection Failed"
        score = 0.0

    # Logic for metrics and UI updating
    if verdict == "INJECTION":
        thr_count += 1
        color = "#ef4444" # Red
        ui_status = f"πŸ›‘ THREAT BLOCKED: {category} (Confidence: {score:.2f})"
        new_log = f"<div style='border-left: 3px solid #ef4444; padding-left: 10px; margin-bottom: 10px; font-size: 12px;'><b>Threat:</b> {category}<br><span style='color: #9ca3af;'>{query[:30]}...</span></div>"
    elif verdict == "ERROR":
        color = "#eab308" # Yellow
        ui_status = f"⚠️ CONNECTION ERROR: Ensure AMD backend is running and Port 8000 is open."
        new_log = f"<div style='border-left: 3px solid #eab308; padding-left: 10px; margin-bottom: 10px; font-size: 12px;'><b>Error:</b> API Timeout</div>"
    else:
        saf_count += 1
        color = "#10b981" # Green
        ui_status = f"βœ… SAFE: Query permitted to internal LLM."
        new_log = f"<div style='border-left: 3px solid #10b981; padding-left: 10px; margin-bottom: 10px; font-size: 12px;'><b>Safe Query</b><br><span style='color: #9ca3af;'>{query[:30]}...</span></div>"

    # Compile the updated HTML components
    updated_log = new_log + current_log
    
    tot_html = f"<div class='card' style='border-color:#3b82f6'><div style='font-size:24px'>{tot_count}</div><div style='font-size:10px; color:#9ca3af'>Total Queries</div></div>"
    thr_html = f"<div class='card' style='border-color:#ef4444'><div style='font-size:24px'>{thr_count}</div><div style='font-size:10px; color:#9ca3af'>Threats Blocked</div></div>"
    saf_html = f"<div class='card' style='border-color:#10b981'><div style='font-size:24px'>{saf_count}</div><div style='font-size:10px; color:#9ca3af'>Safe Queries</div></div>"
    
    out_box = f"<div style='padding: 15px; border-radius: 8px; background: #161b22; border: 1px solid {color}; color: {color}; text-align: center; font-weight: bold;'>{ui_status}</div>"

    # Return order MUST match the outputs list below
    return tot_count, thr_count, saf_count, "", out_box, updated_log, updated_log, tot_html, thr_html, saf_html


# --- 3. THE UI WIRING ---
with gr.Blocks(css=custom_css) as demo:
    # State variables to track numbers
    tot_state = gr.State(0)
    thr_state = gr.State(0)
    saf_state = gr.State(0)
    log_state = gr.State("")

    with gr.Row():
        # --- SIDEBAR ---
        with gr.Column(scale=1, elem_classes="fake-sidebar"):
            gr.HTML("<div style='font-size: 40px; text-align:center;'>πŸ›‘οΈ</div>")
            gr.Markdown("## **INTELLIGUARD**\nSECURITY PLATFORM")
            gr.HTML("<div class='status-badge'>● INTELLIGUARD PROTECTED</div>")
            
            gr.Markdown("### πŸ“‹ AUDIT LOG")
            audit_log_ui = gr.HTML("<p style='color: #6b7280;'>No activity recorded.</p>")
            
            gr.Markdown("---")
            gr.Markdown("### πŸ“„ DOCUMENT UPLOAD")
            gr.File(label=None)

        # --- MAIN CONTENT ---
        with gr.Column(scale=4):
            gr.HTML("""
                <div style="text-align: center; margin-top: 20px;">
                    <h1 style="font-size: 32px;">TechCorp Employee Portal</h1>
                    <p style="color: #6b7280;">Secured by <span style="color: #10b981;">IntelliGuard</span></p>
                </div>
            """)

            # Dynamic Metric Cards
            with gr.Row():
                tot_ui = gr.HTML("<div class='card' style='border-color:#3b82f6'><div style='font-size:24px'>0</div><div style='font-size:10px; color:#9ca3af'>Total Queries</div></div>")
                thr_ui = gr.HTML("<div class='card' style='border-color:#ef4444'><div style='font-size:24px'>0</div><div style='font-size:10px; color:#9ca3af'>Threats Blocked</div></div>")
                saf_ui = gr.HTML("<div class='card' style='border-color:#10b981'><div style='font-size:24px'>0</div><div style='font-size:10px; color:#9ca3af'>Safe Queries</div></div>")

            with gr.Accordion("πŸ“§ Enterprise Email Gateway", open=True):
                email = gr.Textbox(label="Target Email", value="security-ops@techcorp.com", interactive=True)
                connect_btn = gr.Button("πŸ”— Establish Secure Connection", elem_classes="establish-btn")

            gr.Markdown("<div style='height: 80px;'></div>")
            
            # Action Area
            status_box = gr.HTML("<div style='padding: 15px; text-align: center; color: #6b7280;'>Awaiting Payload...</div>")
            
            with gr.Row():
                chat = gr.Textbox(show_label=False, placeholder="Ask about company policies...", scale=10, interactive=True)
                submit = gr.Button("↑", scale=1, variant="primary")

    # Connect the UI to the Python Function
    submit.click(
        fn=process_query,
        inputs=[chat, tot_state, thr_state, saf_state, log_state],
        outputs=[tot_state, thr_state, saf_state, chat, status_box, log_state, audit_log_ui, tot_ui, thr_ui, saf_ui]
    )
    chat.submit(
        fn=process_query,
        inputs=[chat, tot_state, thr_state, saf_state, log_state],
        outputs=[tot_state, thr_state, saf_state, chat, status_box, log_state, audit_log_ui, tot_ui, thr_ui, saf_ui]
    )

demo.launch()