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"
Threat: {category}
{query[:30]}...
" elif verdict == "ERROR": color = "#eab308" # Yellow ui_status = f"⚠️ CONNECTION ERROR: Ensure AMD backend is running and Port 8000 is open." new_log = f"
Error: API Timeout
" else: saf_count += 1 color = "#10b981" # Green ui_status = f"✅ SAFE: Query permitted to internal LLM." new_log = f"
Safe Query
{query[:30]}...
" # Compile the updated HTML components updated_log = new_log + current_log tot_html = f"
{tot_count}
Total Queries
" thr_html = f"
{thr_count}
Threats Blocked
" saf_html = f"
{saf_count}
Safe Queries
" out_box = f"
{ui_status}
" # 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("
🛡️
") gr.Markdown("## **INTELLIGUARD**\nSECURITY PLATFORM") gr.HTML("
● INTELLIGUARD PROTECTED
") gr.Markdown("### 📋 AUDIT LOG") audit_log_ui = gr.HTML("

No activity recorded.

") gr.Markdown("---") gr.Markdown("### 📄 DOCUMENT UPLOAD") gr.File(label=None) # --- MAIN CONTENT --- with gr.Column(scale=4): gr.HTML("""

TechCorp Employee Portal

Secured by IntelliGuard

""") # Dynamic Metric Cards with gr.Row(): tot_ui = gr.HTML("
0
Total Queries
") thr_ui = gr.HTML("
0
Threats Blocked
") saf_ui = gr.HTML("
0
Safe Queries
") 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("
") # Action Area status_box = gr.HTML("
Awaiting Payload...
") 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()