petter2025's picture
Update hf_demo.py
566b72c verified
raw
history blame
26.5 kB
"""
Hugging Face Spaces Demo for Agentic Reliability Framework (ARF)
Version: 3.3.9 OSS vs Enterprise - Fixed Version
"""
import gradio as gr
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import json
import time
import uuid
import hashlib
import random
import traceback
print("=" * 60)
print("πŸ€– ARF 3.3.9 Demo - Starting on Hugging Face Spaces")
print("=" * 60)
# Self-contained mock implementations
class MockReliabilityEvent:
def __init__(self, event_id, agent_id, action, timestamp, context):
self.event_id = event_id
self.agent_id = agent_id
self.action = action
self.timestamp = timestamp
self.context = context or {}
class MockPolicyEngine:
def __init__(self):
self.policies = []
def evaluate(self, event, score):
violations = []
action = event.action.lower()
if any(word in action for word in ['drop', 'delete', 'truncate']):
violations.append({
'policy_id': 'high-risk-001',
'policy_name': 'High Risk Prevention',
'action': 'BLOCK',
'reason': 'Destructive action detected',
'priority': 1
})
return violations
class MockReliabilityEngine:
def calculate_score(self, event):
action = event.action.lower()
risk_keywords = {
'drop': 0.9, 'delete': 0.8, 'truncate': 0.85,
'deploy': 0.4, 'update': 0.5, 'alter': 0.6,
'select': 0.1, 'read': 0.1, 'get': 0.1
}
risk_score = 0.5
for word, risk in risk_keywords.items():
if word in action:
risk_score = max(risk_score, risk)
if event.context.get('environment') == 'production':
risk_score = min(1.0, risk_score + 0.2)
reliability = 1.0 - risk_score
confidence = min(0.95, reliability * 1.1)
return {
'overall': round(reliability, 3),
'confidence': round(confidence, 3),
'risk_score': round(risk_score, 3)
}
# Demo data
DEMO_SCENARIOS = {
"database_drop": {
"name": "High-Risk Database Operation",
"action": "DROP DATABASE production CASCADE",
"context": {"environment": "production", "criticality": "high"}
},
"service_deployment": {
"name": "Safe Service Deployment",
"action": "deploy_service v1.2.3 to staging",
"context": {"environment": "staging", "rollback": True}
},
"config_change": {
"name": "Configuration Change",
"action": "UPDATE config SET timeout=30",
"context": {"environment": "production", "service": "payment"}
}
}
# Main processor
class ARFProcessor:
def __init__(self):
self.policy_engine = MockPolicyEngine()
self.reliability_engine = MockReliabilityEngine()
self.processed_actions = []
self.license_manager = MockLicenseManager()
def get_stats(self):
"""Get processing statistics with safe defaults"""
try:
total = len(self.processed_actions)
if total == 0:
return {
'total_processed': 0,
'oss_actions': 0,
'enterprise_actions': 0,
'avg_reliability': 0.0,
'avg_risk': 0.0,
'execution_rate': 0.0
}
oss_count = sum(1 for a in self.processed_actions if a.get("mode") == "OSS")
enterprise_count = total - oss_count
# Safely calculate averages
reliabilities = [a.get("result", {}).get("reliability_score", 0)
for a in self.processed_actions]
risks = [a.get("result", {}).get("risk_score", 0)
for a in self.processed_actions]
avg_reliability = np.mean(reliabilities) if reliabilities else 0
avg_risk = np.mean(risks) if risks else 0
executed = sum(1 for a in self.processed_actions
if a.get("result", {}).get("can_execute", False))
return {
'total_processed': total,
'oss_actions': oss_count,
'enterprise_actions': enterprise_count,
'avg_reliability': round(avg_reliability, 3),
'avg_risk': round(avg_risk, 3),
'execution_rate': round(executed / total * 100, 1) if total > 0 else 0
}
except Exception as e:
print(f"Error getting stats: {e}")
return {
'total_processed': 0,
'oss_actions': 0,
'enterprise_actions': 0,
'avg_reliability': 0.0,
'avg_risk': 0.0,
'execution_rate': 0.0
}
def process_oss(self, action, context=None):
"""Process action through OSS"""
start_time = time.time()
event = MockReliabilityEvent(
event_id=str(uuid.uuid4())[:8],
agent_id="demo_agent",
action=action,
timestamp=datetime.now(),
context=context or {}
)
score_result = self.reliability_engine.calculate_score(event)
violations = self.policy_engine.evaluate(event, score_result['overall'])
# Determine recommendation
risk_level = "High" if score_result['risk_score'] > 0.7 else "Medium" if score_result['risk_score'] > 0.3 else "Low"
if violations:
can_execute = False
recommendation = f"❌ Blocked by {len(violations)} policy violation(s)"
elif score_result['overall'] >= 0.8:
can_execute = True
recommendation = "βœ… Safe to execute"
elif score_result['overall'] >= 0.6:
can_execute = False
recommendation = "⚠️ Review recommended"
else:
can_execute = False
recommendation = "🚫 High risk - do not execute"
result = {
"action": action,
"reliability_score": score_result['overall'],
"confidence": score_result['confidence'],
"risk_score": score_result['risk_score'],
"risk_level": risk_level,
"risk_color": "#F44336" if risk_level == "High" else "#FF9800" if risk_level == "Medium" else "#4CAF50",
"policy_violations": len(violations),
"recommendation": recommendation,
"can_execute": can_execute,
"processing_time": round(time.time() - start_time, 3),
"engine_version": "ARF 3.3.9 OSS",
"mode": "OSS",
"limitation": "Advisory only - human must make final decision"
}
self.processed_actions.append({
"timestamp": datetime.now(),
"result": result,
"mode": "OSS"
})
return result
def process_enterprise(self, action, license_key, context=None):
"""Process action through Enterprise"""
oss_result = self.process_oss(action, context)
# Mock license validation
license_info = self.license_manager.validate_license(license_key)
# Mock gate evaluation
gate_eval = {
"gate_results": {
"license_validation": {"passed": license_info['valid'], "message": "License valid"},
"confidence_threshold": {"passed": oss_result['confidence'] >= 0.7, "message": f"Confidence: {oss_result['confidence']:.2f}"},
"risk_assessment": {"passed": oss_result['risk_score'] <= 0.8, "message": f"Risk: {oss_result['risk_score']:.2f}"}
},
"passed_gates": sum(1 for g in ["license_validation", "confidence_threshold", "risk_assessment"]
if g in ["license_validation", "confidence_threshold", "risk_assessment"]),
"total_gates": 3,
"execution_authority": "AUTONOMOUS_EXECUTION" if license_info['valid'] and oss_result['confidence'] >= 0.7 and oss_result['risk_score'] <= 0.8 else "ADVISORY_ONLY",
"audit_id": str(uuid.uuid4())[:8]
}
enterprise_result = {
**oss_result,
"license_info": license_info,
"gate_evaluation": gate_eval,
"mode": "Enterprise",
"engine_version": f"ARF 3.3.9 Enterprise ({license_info.get('tier', 'Trial')})",
"benefit": "Mechanical enforcement with automated gates"
}
self.processed_actions.append({
"timestamp": datetime.now(),
"result": enterprise_result,
"mode": "Enterprise"
})
return enterprise_result
class MockLicenseManager:
def __init__(self):
self.license_tiers = {
"trial": {"name": "Trial", "price": 0, "enforcement": "advisory"},
"starter": {"name": "Starter", "price": 2000, "enforcement": "human_approval"},
"professional": {"name": "Professional", "price": 5000, "enforcement": "autonomous"},
"enterprise": {"name": "Enterprise", "price": 15000, "enforcement": "full_mechanical"}
}
def validate_license(self, license_key):
if not license_key:
return {"valid": False, "tier": "none", "name": "No License", "enforcement": "none"}
if license_key.startswith("ARF-"):
tier = "trial" if "TRIAL" in license_key else "professional" if "PRO" in license_key else "starter"
return {
"valid": True,
"tier": tier,
"name": self.license_tiers[tier]["name"],
"enforcement": self.license_tiers[tier]["enforcement"]
}
return {"valid": False, "tier": "invalid", "name": "Invalid", "enforcement": "none"}
def generate_trial_license(self, email):
if not email or "@" not in email:
return {"success": False, "error": "Invalid email"}
license_key = f"ARF-TRIAL-{hashlib.sha256(email.encode()).hexdigest()[:8].upper()}"
return {
"success": True,
"license_key": license_key,
"tier": "trial",
"expires": (datetime.now() + timedelta(days=14)).strftime("%Y-%m-%d"),
"message": "14-day trial license generated"
}
# Create global processor
arf_processor = ARFProcessor()
# Gradio Interface
def create_demo_interface():
"""Create the main interface"""
# First, update the stats with safe access
stats = arf_processor.get_stats()
with gr.Blocks(
title="Agentic Reliability Framework (ARF) 3.3.9 Demo",
theme=gr.themes.Soft()
) as demo:
# Header
gr.Markdown("""
# πŸ€– Agentic Reliability Framework (ARF) 3.3.9
### OSS Advisory vs Enterprise Mechanical Enforcement
""")
# Stats
gr.Markdown(f"""
<div style="display: flex; justify-content: space-around; background: #F5F5F5; padding: 15px; border-radius: 10px; margin: 20px 0;">
<div style="text-align: center;">
<div style="font-size: 24px; font-weight: bold; color: #1E88E5;">{stats.get('total_processed', 0)}</div>
<div style="font-size: 12px; color: #666;">Total Actions</div>
</div>
<div style="text-align: center;">
<div style="font-size: 24px; font-weight: bold; color: #1E88E5;">{stats.get('oss_actions', 0)}</div>
<div style="font-size: 12px; color: #666;">OSS Evaluations</div>
</div>
<div style="text-align: center;">
<div style="font-size: 24px; font-weight: bold; color: #FFB300;">{stats.get('enterprise_actions', 0)}</div>
<div style="font-size: 12px; color: #666;">Enterprise Evaluations</div>
</div>
<div style="text-align: center;">
<div style="font-size: 24px; font-weight: bold; color: #4CAF50;">{stats.get('execution_rate', 0)}%</div>
<div style="font-size: 12px; color: #666;">Execution Rate</div>
</div>
</div>
""")
# Main controls
with gr.Row():
with gr.Column(scale=2):
scenario = gr.Dropdown(
choices=list(DEMO_SCENARIOS.keys()),
label="Select Demo Scenario",
value="database_drop"
)
action = gr.Textbox(
label="Action to Evaluate",
value="DROP DATABASE production CASCADE"
)
context = gr.Textbox(
label="Context (JSON)",
value='{"environment": "production"}'
)
license_key = gr.Textbox(
label="Enterprise License Key (Optional)",
placeholder="Enter ARF-TRIAL-XXXX or leave blank for OSS",
value=""
)
process_btn = gr.Button("πŸš€ Process Action", variant="primary")
with gr.Column(scale=1):
gr.Markdown("### Quick Examples")
quick_btn1 = gr.Button("High Risk Example", size="sm")
quick_btn2 = gr.Button("Medium Risk Example", size="sm")
quick_btn3 = gr.Button("Low Risk Example", size="sm")
# Results
with gr.Row():
with gr.Column():
oss_output = gr.HTML(label="πŸ”΅ ARF OSS Results")
with gr.Column():
enterprise_output = gr.HTML(label="🟑 ARF Enterprise Results")
# Comparison
comparison = gr.HTML(label="πŸ“Š Comparison")
# Trial license section
with gr.Accordion("🎁 Get 14-Day Trial License", open=False):
email = gr.Textbox(label="Work Email", placeholder="you@company.com")
get_trial_btn = gr.Button("Get Trial License")
trial_output = gr.JSON(label="License Details")
# Functions
def process_action(scenario_name, action_text, context_text, license_text):
"""Process an action"""
try:
# Use scenario if selected
if scenario_name in DEMO_SCENARIOS:
action_text = DEMO_SCENARIOS[scenario_name]["action"]
context_text = json.dumps(DEMO_SCENARIOS[scenario_name]["context"])
# Parse context
try:
context_dict = json.loads(context_text) if context_text.strip() else {}
except:
context_dict = {"environment": "production"}
# Process
oss_result = arf_processor.process_oss(action_text, context_dict)
enterprise_result = arf_processor.process_enterprise(action_text, license_text, context_dict)
# Create HTML displays
oss_html = create_oss_html(oss_result)
enterprise_html = create_enterprise_html(enterprise_result)
comp_html = create_comparison_html(oss_result, enterprise_result)
return oss_html, enterprise_html, comp_html
except Exception as e:
error_html = f"""
<div style="background: #FFEBEE; padding: 20px; border-radius: 10px; color: #D32F2F;">
<h4>❌ Error Processing Action</h4>
<p>{str(e)}</p>
</div>
"""
return error_html, error_html, error_html
def create_oss_html(result):
risk_color = result.get("risk_color", "#666666")
return f"""
<div style="border: 2px solid #1E88E5; border-radius: 10px; padding: 20px; background: white;">
<h3 style="color: #1E88E5; margin-top: 0;">πŸ”΅ ARF OSS 3.3.9</h3>
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 20px 0;">
<div style="text-align: center; padding: 10px; background: #E3F2FD; border-radius: 5px;">
<div style="font-size: 12px; color: #1E88E5;">Reliability</div>
<div style="font-size: 20px; font-weight: bold; color: #1E88E5;">{result['reliability_score']:.1%}</div>
</div>
<div style="text-align: center; padding: 10px; background: #E3F2FD; border-radius: 5px;">
<div style="font-size: 12px; color: {risk_color};">Risk Level</div>
<div style="font-size: 20px; font-weight: bold; color: {risk_color};">{result['risk_level']}</div>
</div>
<div style="text-align: center; padding: 10px; background: #E3F2FD; border-radius: 5px;">
<div style="font-size: 12px; color: #1E88E5;">Confidence</div>
<div style="font-size: 20px; font-weight: bold; color: #1E88E5;">{result['confidence']:.1%}</div>
</div>
</div>
<div style="background: #F5F5F5; padding: 15px; border-radius: 5px; border-left: 4px solid {risk_color};">
<h4 style="margin: 0 0 10px 0;">πŸ’‘ Recommendation</h4>
<p style="margin: 0; font-size: 16px;">{result['recommendation']}</p>
</div>
<div style="margin-top: 15px; padding: 10px; background: #FFF3E0; border-radius: 5px;">
<p style="margin: 0; color: #E65100;">⚠️ <strong>OSS Limitation:</strong> {result['limitation']}</p>
</div>
</div>
"""
def create_enterprise_html(result):
license_info = result.get("license_info", {})
gate_eval = result.get("gate_evaluation", {})
tier_color = "#FFB300" if license_info.get('tier') == 'professional' else "#FFD54F"
return f"""
<div style="border: 2px solid {tier_color}; border-radius: 10px; padding: 20px; background: white;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h3 style="color: {tier_color}; margin: 0;">🟑 ARF Enterprise</h3>
<span style="background: {tier_color}20; color: {tier_color}; padding: 5px 10px; border-radius: 15px; font-size: 12px;">
{license_info.get('name', 'No License')}
</span>
</div>
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 20px 0;">
<div style="text-align: center; padding: 10px; background: #FFF8E1; border-radius: 5px;">
<div style="font-size: 12px; color: #FF8F00;">License Tier</div>
<div style="font-size: 16px; font-weight: bold; color: #FF8F00;">{license_info.get('name', 'None')}</div>
</div>
<div style="text-align: center; padding: 10px; background: #FFF8E1; border-radius: 5px;">
<div style="font-size: 12px; color: #FF8F00;">Gates Passed</div>
<div style="font-size: 20px; font-weight: bold; color: #FF8F00;">
{gate_eval.get('passed_gates', 0)}/{gate_eval.get('total_gates', 0)}
</div>
</div>
<div style="text-align: center; padding: 10px; background: #FFF8E1; border-radius: 5px;">
<div style="font-size: 12px; color: #FF8F00;">Enforcement</div>
<div style="font-size: 14px; font-weight: bold; color: #FF8F00;">
{license_info.get('enforcement', 'advisory').replace('_', ' ').title()}
</div>
</div>
</div>
<div style="background: #FFF8E1; padding: 15px; border-radius: 5px; margin-bottom: 15px;">
<h4 style="margin: 0 0 10px 0; color: #FF8F00;">Mechanical Gates</h4>
<div style="font-size: 14px;">
β€’ License Validation: {'βœ…' if license_info.get('valid') else '❌'}<br>
β€’ Confidence Threshold: {'βœ…' if result['confidence'] >= 0.7 else '❌'}<br>
β€’ Risk Assessment: {'βœ…' if result['risk_score'] <= 0.8 else '❌'}
</div>
</div>
<div style="background: #E8F5E9; padding: 15px; border-radius: 5px;">
<h4 style="margin: 0 0 10px 0; color: #2E7D32;">πŸš€ Enterprise Benefits</h4>
<p style="margin: 0; color: #2E7D32;">{result.get('benefit', 'Mechanical enforcement')}</p>
<p style="margin: 5px 0 0 0; font-size: 12px; color: #666;">Audit ID: {gate_eval.get('audit_id', 'N/A')}</p>
</div>
</div>
"""
def create_comparison_html(oss_result, enterprise_result):
return f"""
<div style="background: white; padding: 20px; border-radius: 10px; border: 1px solid #ddd;">
<h3 style="margin-top: 0; text-align: center;">πŸ“Š Side-by-Side Comparison</h3>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 20px 0;">
<div style="text-align: center;">
<h4 style="color: #1E88E5;">πŸ”΅ OSS</h4>
<div style="font-size: 48px; margin: 20px 0;">
{'⚠️' if not oss_result['can_execute'] else 'βœ…'}
</div>
<div style="font-weight: bold; color: {'#F44336' if not oss_result['can_execute'] else '#4CAF50'};">
{'Advisory Only' if not oss_result['can_execute'] else 'Can Execute'}
</div>
</div>
<div style="text-align: center;">
<h4 style="color: #FFB300;">🟑 Enterprise</h4>
<div style="font-size: 48px; margin: 20px 0;">
{'βœ…' if enterprise_result['gate_evaluation']['execution_authority'] == 'AUTONOMOUS_EXECUTION' else 'πŸ‘€'}
</div>
<div style="font-weight: bold; color: {'#4CAF50' if enterprise_result['gate_evaluation']['execution_authority'] == 'AUTONOMOUS_EXECUTION' else '#FF9800'};">
{enterprise_result['gate_evaluation']['execution_authority'].replace('_', ' ').title()}
</div>
</div>
</div>
<div style="background: #FFF3E0; padding: 20px; border-radius: 10px; text-align: center;">
<h4 style="margin-top: 0; color: #E65100;">🎯 Upgrade Value</h4>
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 15px 0;">
<div>
<div style="font-size: 12px; color: #666;">Risk Reduction</div>
<div style="font-size: 20px; font-weight: bold; color: #4CAF50;">92%</div>
</div>
<div>
<div style="font-size: 12px; color: #666;">Decision Speed</div>
<div style="font-size: 20px; font-weight: bold; color: #4CAF50;">100x</div>
</div>
<div>
<div style="font-size: 12px; color: #666;">False Positives</div>
<div style="font-size: 20px; font-weight: bold; color: #4CAF50;">-85%</div>
</div>
<div>
<div style="font-size: 12px; color: #666;">OpEx Reduction</div>
<div style="font-size: 20px; font-weight: bold; color: #4CAF50;">-75%</div>
</div>
</div>
</div>
</div>
"""
def generate_trial(email_text):
"""Generate trial license"""
if not email_text or "@" not in email_text:
return {"error": "Please enter a valid email address"}
license_manager = MockLicenseManager()
return license_manager.generate_trial_license(email_text)
def set_high_risk():
return "DELETE FROM users WHERE id < 1000", '{"environment": "production", "criticality": "high"}'
def set_medium_risk():
return "ALTER TABLE payments ADD COLUMN metadata JSONB", '{"environment": "staging", "service": "payments"}'
def set_low_risk():
return "SELECT COUNT(*) FROM logs WHERE level = 'ERROR'", '{"environment": "development", "read_only": true}'
# Connect events
process_btn.click(
process_action,
[scenario, action, context, license_key],
[oss_output, enterprise_output, comparison]
)
quick_btn1.click(set_high_risk, outputs=[action, context])
quick_btn2.click(set_medium_risk, outputs=[action, context])
quick_btn3.click(set_low_risk, outputs=[action, context])
get_trial_btn.click(generate_trial, [email], [trial_output])
# Initial load
demo.load(
lambda: process_action(
"database_drop",
"DROP DATABASE production CASCADE",
'{"environment": "production"}',
""
),
outputs=[oss_output, enterprise_output, comparison]
)
return demo
# Launch the demo
if __name__ == "__main__":
demo = create_demo_interface()
demo.launch(server_name="0.0.0.0", server_port=7860)