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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import random
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from typing import Dict, Any, Optional, Tuple
|
| 7 |
+
import hashlib
|
| 8 |
+
|
| 9 |
+
# Import utility functions
|
| 10 |
+
from utils import (
|
| 11 |
+
simulate_secure_processing,
|
| 12 |
+
get_agents_data,
|
| 13 |
+
process_with_agent,
|
| 14 |
+
SecurityMetrics
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Initialize global state
|
| 18 |
+
security_metrics = SecurityMetrics()
|
| 19 |
+
|
| 20 |
+
def create_agent_selector():
|
| 21 |
+
"""Create agent selection UI with all 6 agents"""
|
| 22 |
+
agents = get_agents_data()
|
| 23 |
+
|
| 24 |
+
agent_info = ""
|
| 25 |
+
for agent in agents:
|
| 26 |
+
caps = ", ".join(agent["capabilities"][:3])
|
| 27 |
+
agent_info += f"**{agent['name']}** ({agent['type']})\n- Status: {agent['status']}\n- Capabilities: {caps}\n\n"
|
| 28 |
+
|
| 29 |
+
return agent_info
|
| 30 |
+
|
| 31 |
+
def process_securely(agent_id: str, operation: str, text_input: str, file_input) -> Tuple[str, str]:
|
| 32 |
+
"""
|
| 33 |
+
Main processing function that handles secure operations
|
| 34 |
+
"""
|
| 35 |
+
if not agent_id:
|
| 36 |
+
return "β Please select an agent first", ""
|
| 37 |
+
|
| 38 |
+
if not text_input and not file_input:
|
| 39 |
+
return "β Please provide text input or upload a file", ""
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# Process the input based on agent and operation
|
| 43 |
+
result = process_with_agent(
|
| 44 |
+
agent_id=agent_id,
|
| 45 |
+
operation=operation,
|
| 46 |
+
text_input=text_input,
|
| 47 |
+
file_input=file_input,
|
| 48 |
+
metrics=security_metrics
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Format output
|
| 52 |
+
output_text = f"""β
**Processing Successful**
|
| 53 |
+
|
| 54 |
+
**Agent:** {result['agent_name']}
|
| 55 |
+
**Operation:** {operation.title()}
|
| 56 |
+
**Security:** {result['security']['encryption']}
|
| 57 |
+
**Timestamp:** {result['security']['timestamp']}
|
| 58 |
+
|
| 59 |
+
**Result:**
|
| 60 |
+
{json.dumps(result['data'], indent=2)}"""
|
| 61 |
+
|
| 62 |
+
security_details = f"""π **Security Details**
|
| 63 |
+
|
| 64 |
+
- **Hash:** {result['security']['hash'][:32]}...
|
| 65 |
+
- **Encryption:** {result['security']['encryption']}
|
| 66 |
+
- **Agent ID:** {result['security']['agent_id']}
|
| 67 |
+
- **Threats Blocked:** {security_metrics.threats_blocked}
|
| 68 |
+
- **Data Units Processed:** {security_metrics.data_processed}"""
|
| 69 |
+
|
| 70 |
+
return output_text, security_details
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
error_msg = f"β Processing failed: {str(e)}"
|
| 74 |
+
return error_msg, ""
|
| 75 |
+
|
| 76 |
+
def update_dashboard():
|
| 77 |
+
"""Update privacy dashboard metrics"""
|
| 78 |
+
metrics = security_metrics.get_metrics()
|
| 79 |
+
|
| 80 |
+
dashboard_text = f"""### π **Security Metrics**
|
| 81 |
+
|
| 82 |
+
- **Encryption Level:** {metrics['encryption_level']}
|
| 83 |
+
- **Threats Blocked:** {metrics['threats_blocked']}
|
| 84 |
+
- **Data Processed:** {metrics['data_processed']} units
|
| 85 |
+
- **Uptime:** {metrics['uptime']}
|
| 86 |
+
|
| 87 |
+
### π‘οΈ **Privacy Guarantees**
|
| 88 |
+
β End-to-end encryption
|
| 89 |
+
β Zero data retention
|
| 90 |
+
β Local processing priority
|
| 91 |
+
β Anonymous usage tracking
|
| 92 |
+
|
| 93 |
+
### π **Live Activity**
|
| 94 |
+
{metrics['recent_activity']}"""
|
| 95 |
+
|
| 96 |
+
return dashboard_text
|
| 97 |
+
|
| 98 |
+
def on_agent_select(agent_name: str) -> str:
|
| 99 |
+
"""Handle agent selection"""
|
| 100 |
+
if not agent_name:
|
| 101 |
+
return "No agent selected"
|
| 102 |
+
|
| 103 |
+
agents = get_agents_data()
|
| 104 |
+
agent = next((a for a in agents if a['name'] == agent_name), None)
|
| 105 |
+
|
| 106 |
+
if agent:
|
| 107 |
+
caps = "\n".join([f"- {cap}" for cap in agent['capabilities']])
|
| 108 |
+
return f"""**{agent['name']}** ({agent['type']})
|
| 109 |
+
**Status:** {agent['status']}
|
| 110 |
+
|
| 111 |
+
**Capabilities:**
|
| 112 |
+
{caps}"""
|
| 113 |
+
return ""
|
| 114 |
+
|
| 115 |
+
# Build the Gradio interface
|
| 116 |
+
with gr.Blocks() as demo:
|
| 117 |
+
# Header with title and anycoder link
|
| 118 |
+
gr.Markdown(
|
| 119 |
+
"""
|
| 120 |
+
# π Multi-Agent Secure Media Platform
|
| 121 |
+
### Protecting your digital footprint with military-grade security, AI-powered analysis, and creative problem-solving capabilities. Your data never leaves your control.
|
| 122 |
+
|
| 123 |
+
**[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)**
|
| 124 |
+
""",
|
| 125 |
+
elem_classes="header"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Main layout
|
| 129 |
+
with gr.Row():
|
| 130 |
+
# Left column: Agent Selection
|
| 131 |
+
with gr.Column(scale=1):
|
| 132 |
+
gr.Markdown("### π€ Select Security Agent")
|
| 133 |
+
|
| 134 |
+
agents = get_agents_data()
|
| 135 |
+
agent_names = [agent['name'] for agent in agents]
|
| 136 |
+
agent_names.insert(0, "β Choose an agent")
|
| 137 |
+
|
| 138 |
+
agent_dropdown = gr.Dropdown(
|
| 139 |
+
choices=agent_names,
|
| 140 |
+
value="β Choose an agent",
|
| 141 |
+
label="Available Agents",
|
| 142 |
+
info="Each agent specializes in different security tasks",
|
| 143 |
+
interactive=True
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
agent_info_box = gr.Markdown(
|
| 147 |
+
value="Select an agent to view capabilities",
|
| 148 |
+
label="Agent Details"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# Bind agent selection
|
| 152 |
+
agent_dropdown.change(
|
| 153 |
+
fn=on_agent_select,
|
| 154 |
+
inputs=agent_dropdown,
|
| 155 |
+
outputs=agent_info_box,
|
| 156 |
+
api_visibility="private"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Right column: Secure Processing
|
| 160 |
+
with gr.Column(scale=2):
|
| 161 |
+
gr.Markdown("### π Secure Processing")
|
| 162 |
+
|
| 163 |
+
with gr.Row():
|
| 164 |
+
operation_dropdown = gr.Dropdown(
|
| 165 |
+
choices=["analyze", "encrypt", "sanitize", "process"],
|
| 166 |
+
value="analyze",
|
| 167 |
+
label="Operation Type",
|
| 168 |
+
info="Choose how to process your data",
|
| 169 |
+
scale=1
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
text_input = gr.Textbox(
|
| 173 |
+
label="Input Text",
|
| 174 |
+
placeholder="Enter text to process securely...",
|
| 175 |
+
lines=4,
|
| 176 |
+
max_lines=10,
|
| 177 |
+
autoscroll=True
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
file_input = gr.File(
|
| 181 |
+
label="Or Upload File",
|
| 182 |
+
file_types=["image", "audio", "video", "text", ".pdf", ".doc", ".docx"],
|
| 183 |
+
file_count="single",
|
| 184 |
+
interactive=True
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
process_btn = gr.Button(
|
| 188 |
+
"π‘οΈ Process Securely",
|
| 189 |
+
variant="primary",
|
| 190 |
+
size="lg"
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
with gr.Row():
|
| 194 |
+
output_text = gr.Textbox(
|
| 195 |
+
label="Processing Result",
|
| 196 |
+
lines=10,
|
| 197 |
+
max_lines=15,
|
| 198 |
+
interactive=False,
|
| 199 |
+
show_copy_button=True
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
security_details = gr.Textbox(
|
| 203 |
+
label="Security Details",
|
| 204 |
+
lines=10,
|
| 205 |
+
max_lines=15,
|
| 206 |
+
interactive=False,
|
| 207 |
+
show_copy_button=True
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# Bind processing button
|
| 211 |
+
process_btn.click(
|
| 212 |
+
fn=process_securely,
|
| 213 |
+
inputs=[agent_dropdown, operation_dropdown, text_input, file_input],
|
| 214 |
+
outputs=[output_text, security_details],
|
| 215 |
+
api_visibility="public",
|
| 216 |
+
show_progress="full"
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# Bottom section: Privacy Dashboard
|
| 220 |
+
gr.Markdown("### π Privacy & Security Dashboard")
|
| 221 |
+
|
| 222 |
+
dashboard_output = gr.Textbox(
|
| 223 |
+
label="Live Security Metrics",
|
| 224 |
+
lines=12,
|
| 225 |
+
interactive=False,
|
| 226 |
+
every=2.0 # Auto-update every 2 seconds
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
# Timer for live dashboard updates
|
| 230 |
+
timer = gr.Timer(value=2.0, active=True)
|
| 231 |
+
timer.tick(
|
| 232 |
+
fn=update_dashboard,
|
| 233 |
+
outputs=dashboard_output,
|
| 234 |
+
api_visibility="private",
|
| 235 |
+
show_progress="hidden"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# Footer
|
| 239 |
+
gr.Markdown(
|
| 240 |
+
"""
|
| 241 |
+
---
|
| 242 |
+
Β© 2024 SecureAgent Platform. All data processed with maximum privacy protection.
|
| 243 |
+
Built with advanced encryption and zero-knowledge architecture.
|
| 244 |
+
""",
|
| 245 |
+
elem_classes="footer"
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Launch with Gradio 6 theming
|
| 249 |
+
demo.launch(
|
| 250 |
+
theme=gr.themes.Soft(
|
| 251 |
+
primary_hue="blue",
|
| 252 |
+
secondary_hue="indigo",
|
| 253 |
+
neutral_hue="slate",
|
| 254 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 255 |
+
text_size="lg",
|
| 256 |
+
spacing_size="lg",
|
| 257 |
+
radius_size="md"
|
| 258 |
+
).set(
|
| 259 |
+
button_primary_background_fill="*primary_600",
|
| 260 |
+
button_primary_background_fill_hover="*primary_700",
|
| 261 |
+
block_title_text_weight="600",
|
| 262 |
+
block_background_fill="*neutral_50",
|
| 263 |
+
block_border_width="1px",
|
| 264 |
+
block_border_color="*neutral_200"
|
| 265 |
+
),
|
| 266 |
+
css="""
|
| 267 |
+
.header { text-align: center; margin-bottom: 2rem; }
|
| 268 |
+
.footer { text-align: center; margin-top: 2rem; font-size: 0.875rem; }
|
| 269 |
+
.gradio-container { max-width: 1400px; margin: 0 auto; }
|
| 270 |
+
#component-0 { border-bottom: 2px solid #e2e8f0; padding-bottom: 1rem; }
|
| 271 |
+
""",
|
| 272 |
+
footer_links=[
|
| 273 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 274 |
+
],
|
| 275 |
+
show_error=True,
|
| 276 |
+
allowed_paths=["./uploads"]
|
| 277 |
+
)
|