| import gradio as gr |
| import requests |
| import os |
|
|
| |
| |
| API_URL = os.environ.get("INTELLIGUARD_API", "http://127.0.0.1:8000/scan") |
|
|
| |
| 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; } |
| """ |
|
|
| |
| 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: |
| |
| 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 |
|
|
| |
| if verdict == "INJECTION": |
| thr_count += 1 |
| color = "#ef4444" |
| 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" |
| 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" |
| 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>" |
|
|
| |
| 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 tot_count, thr_count, saf_count, "", out_box, updated_log, updated_log, tot_html, thr_html, saf_html |
|
|
|
|
| |
| with gr.Blocks(css=custom_css) as demo: |
| |
| tot_state = gr.State(0) |
| thr_state = gr.State(0) |
| saf_state = gr.State(0) |
| log_state = gr.State("") |
|
|
| with gr.Row(): |
| |
| 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) |
|
|
| |
| 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> |
| """) |
|
|
| |
| 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>") |
| |
| |
| 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") |
|
|
| |
| 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() |