Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
custom_css = """
|
| 6 |
.gradio-container { background-color: #06090f !important; color: white !important; }
|
| 7 |
.fake-sidebar {
|
|
@@ -18,23 +23,82 @@ custom_css = """
|
|
| 18 |
background: #111827; padding: 20px; border-radius: 12px; text-align: center;
|
| 19 |
border-top: 3px solid #3b82f6; margin-bottom: 10px;
|
| 20 |
}
|
| 21 |
-
.establish-btn { background: linear-gradient(90deg, #3b82f6 0%, #00d4ff 100%) !important; color: white !important; }
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
with gr.Blocks(css=custom_css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
-
# ---
|
| 27 |
with gr.Column(scale=1, elem_classes="fake-sidebar"):
|
| 28 |
gr.HTML("<div style='font-size: 40px; text-align:center;'>π‘οΈ</div>")
|
| 29 |
gr.Markdown("## **INTELLIGUARD**\nSECURITY PLATFORM")
|
| 30 |
gr.HTML("<div class='status-badge'>β INTELLIGUARD PROTECTED</div>")
|
| 31 |
|
| 32 |
-
gr.Markdown("### π AUDIT LOG
|
|
|
|
|
|
|
| 33 |
gr.Markdown("---")
|
| 34 |
gr.Markdown("### π DOCUMENT UPLOAD")
|
| 35 |
gr.File(label=None)
|
| 36 |
|
| 37 |
-
# ---
|
| 38 |
with gr.Column(scale=4):
|
| 39 |
gr.HTML("""
|
| 40 |
<div style="text-align: center; margin-top: 20px;">
|
|
@@ -43,20 +107,35 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 43 |
</div>
|
| 44 |
""")
|
| 45 |
|
|
|
|
| 46 |
with gr.Row():
|
| 47 |
-
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>")
|
| 48 |
-
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>")
|
| 49 |
-
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>")
|
| 50 |
|
| 51 |
with gr.Accordion("π§ Enterprise Email Gateway", open=True):
|
| 52 |
-
email = gr.Textbox(label="Target Email", value="security-ops@techcorp.com")
|
| 53 |
connect_btn = gr.Button("π Establish Secure Connection", elem_classes="establish-btn")
|
| 54 |
|
| 55 |
-
gr.Markdown("<div style='height:
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
chat = gr.Textbox(show_label=False, placeholder="Ask about company policies...", scale=10)
|
| 59 |
submit = gr.Button("β", scale=1, variant="primary")
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# --- 1. DYNAMIC API ROUTING ---
|
| 6 |
+
# Pulls the live AMD IP from Hugging Face Settings
|
| 7 |
+
API_URL = os.environ.get("INTELLIGUARD_API", "http://127.0.0.1:8000/scan")
|
| 8 |
+
|
| 9 |
+
# Custom CSS
|
| 10 |
custom_css = """
|
| 11 |
.gradio-container { background-color: #06090f !important; color: white !important; }
|
| 12 |
.fake-sidebar {
|
|
|
|
| 23 |
background: #111827; padding: 20px; border-radius: 12px; text-align: center;
|
| 24 |
border-top: 3px solid #3b82f6; margin-bottom: 10px;
|
| 25 |
}
|
| 26 |
+
.establish-btn { background: linear-gradient(90deg, #3b82f6 0%, #00d4ff 100%) !important; color: white !important; font-weight: bold !important;}
|
| 27 |
+
/* Fix for dark mode inputs */
|
| 28 |
+
input, textarea { color: white !important; background-color: #161b22 !important; }
|
| 29 |
"""
|
| 30 |
|
| 31 |
+
# --- 2. THE BRAIN LOGIC ---
|
| 32 |
+
def process_query(query, tot_count, thr_count, saf_count, current_log):
|
| 33 |
+
if not query.strip():
|
| 34 |
+
return tot_count, thr_count, saf_count, "", "Please enter a query.", current_log, current_log, gr.update(), gr.update(), gr.update()
|
| 35 |
+
|
| 36 |
+
tot_count += 1
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Send payload to AMD Server
|
| 40 |
+
res = requests.post(API_URL, json={"text": query}, timeout=15)
|
| 41 |
+
data = res.json()
|
| 42 |
+
verdict = data.get("verdict", "ERROR")
|
| 43 |
+
category = data.get("attack_category", "Unknown")
|
| 44 |
+
score = data.get("score", 0.0)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
verdict = "ERROR"
|
| 47 |
+
category = "Connection Failed"
|
| 48 |
+
score = 0.0
|
| 49 |
+
|
| 50 |
+
# Logic for metrics and UI updating
|
| 51 |
+
if verdict == "INJECTION":
|
| 52 |
+
thr_count += 1
|
| 53 |
+
color = "#ef4444" # Red
|
| 54 |
+
ui_status = f"π THREAT BLOCKED: {category} (Confidence: {score:.2f})"
|
| 55 |
+
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>"
|
| 56 |
+
elif verdict == "ERROR":
|
| 57 |
+
color = "#eab308" # Yellow
|
| 58 |
+
ui_status = f"β οΈ CONNECTION ERROR: Ensure AMD backend is running and Port 8000 is open."
|
| 59 |
+
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>"
|
| 60 |
+
else:
|
| 61 |
+
saf_count += 1
|
| 62 |
+
color = "#10b981" # Green
|
| 63 |
+
ui_status = f"β
SAFE: Query permitted to internal LLM."
|
| 64 |
+
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>"
|
| 65 |
+
|
| 66 |
+
# Compile the updated HTML components
|
| 67 |
+
updated_log = new_log + current_log
|
| 68 |
+
|
| 69 |
+
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>"
|
| 70 |
+
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>"
|
| 71 |
+
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>"
|
| 72 |
+
|
| 73 |
+
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>"
|
| 74 |
+
|
| 75 |
+
# Return order MUST match the outputs list below
|
| 76 |
+
return tot_count, thr_count, saf_count, "", out_box, updated_log, updated_log, tot_html, thr_html, saf_html
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# --- 3. THE UI WIRING ---
|
| 80 |
with gr.Blocks(css=custom_css) as demo:
|
| 81 |
+
# State variables to track numbers
|
| 82 |
+
tot_state = gr.State(0)
|
| 83 |
+
thr_state = gr.State(0)
|
| 84 |
+
saf_state = gr.State(0)
|
| 85 |
+
log_state = gr.State("")
|
| 86 |
+
|
| 87 |
with gr.Row():
|
| 88 |
+
# --- SIDEBAR ---
|
| 89 |
with gr.Column(scale=1, elem_classes="fake-sidebar"):
|
| 90 |
gr.HTML("<div style='font-size: 40px; text-align:center;'>π‘οΈ</div>")
|
| 91 |
gr.Markdown("## **INTELLIGUARD**\nSECURITY PLATFORM")
|
| 92 |
gr.HTML("<div class='status-badge'>β INTELLIGUARD PROTECTED</div>")
|
| 93 |
|
| 94 |
+
gr.Markdown("### π AUDIT LOG")
|
| 95 |
+
audit_log_ui = gr.HTML("<p style='color: #6b7280;'>No activity recorded.</p>")
|
| 96 |
+
|
| 97 |
gr.Markdown("---")
|
| 98 |
gr.Markdown("### π DOCUMENT UPLOAD")
|
| 99 |
gr.File(label=None)
|
| 100 |
|
| 101 |
+
# --- MAIN CONTENT ---
|
| 102 |
with gr.Column(scale=4):
|
| 103 |
gr.HTML("""
|
| 104 |
<div style="text-align: center; margin-top: 20px;">
|
|
|
|
| 107 |
</div>
|
| 108 |
""")
|
| 109 |
|
| 110 |
+
# Dynamic Metric Cards
|
| 111 |
with gr.Row():
|
| 112 |
+
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>")
|
| 113 |
+
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>")
|
| 114 |
+
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>")
|
| 115 |
|
| 116 |
with gr.Accordion("π§ Enterprise Email Gateway", open=True):
|
| 117 |
+
email = gr.Textbox(label="Target Email", value="security-ops@techcorp.com", interactive=True)
|
| 118 |
connect_btn = gr.Button("π Establish Secure Connection", elem_classes="establish-btn")
|
| 119 |
|
| 120 |
+
gr.Markdown("<div style='height: 80px;'></div>")
|
| 121 |
+
|
| 122 |
+
# Action Area
|
| 123 |
+
status_box = gr.HTML("<div style='padding: 15px; text-align: center; color: #6b7280;'>Awaiting Payload...</div>")
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
+
chat = gr.Textbox(show_label=False, placeholder="Ask about company policies...", scale=10, interactive=True)
|
| 127 |
submit = gr.Button("β", scale=1, variant="primary")
|
| 128 |
|
| 129 |
+
# Connect the UI to the Python Function
|
| 130 |
+
submit.click(
|
| 131 |
+
fn=process_query,
|
| 132 |
+
inputs=[chat, tot_state, thr_state, saf_state, log_state],
|
| 133 |
+
outputs=[tot_state, thr_state, saf_state, chat, status_box, log_state, audit_log_ui, tot_ui, thr_ui, saf_ui]
|
| 134 |
+
)
|
| 135 |
+
chat.submit(
|
| 136 |
+
fn=process_query,
|
| 137 |
+
inputs=[chat, tot_state, thr_state, saf_state, log_state],
|
| 138 |
+
outputs=[tot_state, thr_state, saf_state, chat, status_box, log_state, audit_log_ui, tot_ui, thr_ui, saf_ui]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
demo.launch()
|