import gradio as gr, json, yaml from pathlib import Path def load_health(): try: # Try the expected path first if Path('state/health.json').exists(): return json.loads(Path('state/health.json').read_text()) # Fall back to the existing state.json file elif Path('state.json').exists(): return json.loads(Path('state.json').read_text()) else: return {'overall':'unknown'} except: return {'overall':'unknown'} def load_verticals(): try: cfg = yaml.safe_load(Path('business/impact.yaml').read_text()) return list(cfg['verticals'].keys()), cfg except: # Fallback configuration if file doesn't exist default_cfg = { 'verticals': { 'fraud_detection': { 'kpis': ['precision', 'recall', 'f1_score'], 'scenarios': ['payment_fraud', 'account_takeover', 'identity_theft'] }, 'content_moderation': { 'kpis': ['accuracy', 'response_time', 'false_positive_rate'], 'scenarios': ['spam_detection', 'hate_speech', 'inappropriate_content'] }, 'supply_chain_risk': { 'kpis': ['risk_score', 'coverage', 'detection_rate'], 'scenarios': ['vendor_risk', 'logistics_disruption', 'quality_issues'] } } } return list(default_cfg['verticals'].keys()), default_cfg def score(vertical, freshness): h = load_health() if h.get('overall') != 'pass': return f'❌ Policy block. Health={h}' base = { 'fraud_detection': 0.87, 'content_moderation': 0.12, 'supply_chain_risk': 0.42 }.get(vertical, 0.5) if float(freshness) > 60: return '❌ Freshness>60 → block' return f'✅ Allowed. score≈{base:.2f}, health={h}' def business(vertical): _, cfg = load_verticals() v = cfg['verticals'][vertical] return json.dumps({'kpis': v['kpis'], 'scenarios': v['scenarios']}, indent=2) # Initialize verts, _ = load_verticals() with gr.Blocks() as demo: gr.Markdown('# Amazon-Style Risk Pipeline — Multi-Vertical Demo') v = gr.Dropdown(choices=verts, value=verts[0], label="Vertical") f = gr.Number(value=12, label='freshness_sec') out = gr.Textbox(label="Score Result") biz = gr.Code(language='json', label="Business Config") with gr.Row(): score_btn = gr.Button('Score') business_btn = gr.Button('Load Business') score_btn.click(score, [v, f], out) business_btn.click(business, v, biz) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)