import json import gradio as gr from sovereign_core import ( ENGINE_NAME, SovereignFingerprint, generate_attestation, generate_integrity_certificate, verify_attestation, verify_certificate, ) def run_sentinel(engine_name: str, parent_model: str, model_version: str, data_tags_str: str, notes: str): """ Main pipeline with internal error capture so that exceptions are shown in the UI instead of Gradio 'Error' bubble. """ try: # Normalize inputs engine = (engine_name or "").strip() or ENGINE_NAME parent_model = (parent_model or "").strip() or None model_version = (model_version or "").strip() or "1.0" tags = [] if data_tags_str: tags = [t.strip() for t in data_tags_str.split(",") if t.strip()] notes_clean = (notes or "").strip() or None # 1) Fingerprint fpgen = SovereignFingerprint(engine) _, fp_value = fpgen.export() # 2) Attestation att = generate_attestation(fp_value) # 3) Certificate (includes lineage inside) cert = generate_integrity_certificate( fp_value, att, parent_model=parent_model, model_version=model_version, data_tags=tags, notes=notes_clean, ) # 4) Verification att_ok = verify_attestation(att) cert_ok = verify_certificate(cert) snapshot = { "engine": engine, "fingerprint": fp_value, "attestation": att, "certificate": cert, "verify_attestation": att_ok, "verify_certificate": cert_ok, } lineage = cert.get("lineage", {}) snapshot_json = json.dumps(snapshot, indent=4) att_json = json.dumps(att, indent=4) cert_json = json.dumps(cert, indent=4) lineage_json = json.dumps(lineage, indent=4) att_status = "✅ VALID" if att_ok else "❌ INVALID" cert_status = "✅ VALID (attestation + lineage)" if cert_ok else "❌ INVALID" return ( snapshot_json, fp_value, att_json, cert_json, lineage_json, att_status, cert_status, ) except Exception as e: # اگر هر جایی خطا بخورد، به جای Error قرمز، # اینجا متن خطا را داخل خروجی برمی‌گردانیم error_info = { "error": str(e), "type": e.__class__.__name__, } snapshot_json = json.dumps(error_info, indent=4) return ( snapshot_json, # Full snapshot پنجره‌ی اول "", # fingerprint خالی "", # attestation JSON "", # certificate JSON "", # lineage JSON f"❌ ERROR: {e}", # وضعیت attestation "❌ ERROR", # وضعیت certificate ) with gr.Blocks(title="AI Sovereign Sentinel – Integrity Engine") as demo: gr.Markdown( """ # AI Sovereign Sentinel – Integrity Engine Generates a sovereign fingerprint, attestation, and integrity certificate with a cryptographic lineage record, then verifies the full trust chain. Use this panel to produce **enterprise-grade model integrity evidence**. """ ) with gr.Row(): with gr.Column(): engine_name = gr.Textbox( label="Engine name", value=ENGINE_NAME, placeholder="AI_Sovereign_Sentinel_Core_v1", ) parent_model = gr.Textbox( label="Parent model (optional)", placeholder="e.g. meta-llama/Meta-Llama-3-70B", ) model_version = gr.Textbox( label="Model version", value="1.0", placeholder="1.0.0", ) data_tags = gr.Textbox( label="Data / domain tags (comma-separated)", placeholder="finance, healthcare, internal, pii-redacted", ) notes = gr.Textbox( label="Internal notes (optional)", lines=3, placeholder="Short description of this model build / deployment.", ) run_btn = gr.Button("Generate Sovereign Integrity Snapshot", variant="primary") with gr.Column(): snapshot_out = gr.Code( label="Full Sovereign Security Snapshot (JSON)", language="json", ) with gr.Row(): with gr.Column(): fp_out = gr.Textbox( label="Fingerprint", interactive=False, ) att_json_out = gr.Code( label="Attestation (JSON)", language="json", ) with gr.Column(): cert_json_out = gr.Code( label="Integrity Certificate (JSON)", language="json", ) lineage_json_out = gr.Code( label="Lineage Record (JSON)", language="json", ) with gr.Row(): att_status_out = gr.Textbox( label="Attestation verification", interactive=False, ) cert_status_out = gr.Textbox( label="Certificate + lineage verification", interactive=False, ) run_btn.click( fn=run_sentinel, inputs=[engine_name, parent_model, model_version, data_tags, notes], outputs=[ snapshot_out, fp_out, att_json_out, cert_json_out, lineage_json_out, att_status_out, cert_status_out, ], ) if __name__ == "__main__": demo.launch()