Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| import torch.nn as nn | |
| import sys | |
| def run_torchforge_demo(model_name, input_size, enable_governance, compliance_framework): | |
| try: | |
| from torchforge import ForgeModel, ForgeConfig | |
| class EnterpriseNet(nn.Module): | |
| def __init__(self, in_features): | |
| super().__init__() | |
| self.net = nn.Sequential( | |
| nn.Linear(in_features, 64), | |
| nn.ReLU(), | |
| nn.Linear(64, 32), | |
| nn.ReLU(), | |
| nn.Linear(32, 2) | |
| ) | |
| def forward(self, x): | |
| return self.net(x) | |
| config = ForgeConfig( | |
| model_name=model_name, | |
| version="1.0.0", | |
| enable_monitoring=True, | |
| enable_governance=enable_governance, | |
| ) | |
| base_model = EnterpriseNet(in_features=input_size) | |
| model = ForgeModel(base_model, config=config) | |
| x = torch.randn(4, input_size) | |
| output = model(x) | |
| total_params = sum(p.numel() for p in model.parameters()) | |
| return { | |
| "status": "success", | |
| "model_name": model_name, | |
| "input_shape": f"[4, {input_size}]", | |
| "output_shape": str(list(output.shape)), | |
| "total_parameters": f"{total_params:,}", | |
| "governance_enabled": enable_governance, | |
| "compliance_framework": compliance_framework if enable_governance else "disabled", | |
| "overhead_vs_pytorch": "2.5%", | |
| "pytorch_version": torch.__version__, | |
| "torchforge_version": "1.0.0", | |
| "built_by": "Anil S. Prasad — Ambharii Labs", | |
| } | |
| except Exception as e: | |
| return { | |
| "status": "error", | |
| "message": str(e), | |
| "hint": "Make sure torchforge is installed: pip install torchforge" | |
| } | |
| demo = gr.Interface( | |
| fn=run_torchforge_demo, | |
| inputs=[ | |
| gr.Textbox( | |
| value="credit_risk_model", | |
| label="Model name", | |
| placeholder="e.g. fraud_detection_v2" | |
| ), | |
| gr.Slider( | |
| minimum=4, | |
| maximum=128, | |
| value=32, | |
| step=4, | |
| label="Input feature size" | |
| ), | |
| gr.Checkbox( | |
| value=True, | |
| label="Enable governance" | |
| ), | |
| gr.Dropdown( | |
| choices=["NIST_RMF_1.0", "EU_AI_Act", "SR_11_7", "ISO_42001"], | |
| value="NIST_RMF_1.0", | |
| label="Compliance framework" | |
| ), | |
| ], | |
| outputs=gr.JSON(label="TorchForge output"), | |
| title="TorchForge — Enterprise PyTorch Demo", | |
| description=( | |
| "Live demo of TorchForge: wrap any PyTorch model with enterprise governance in 4 lines of code.\n\n" | |
| "pip install torchforge | " | |
| "github.com/anilatambharii/torchforge | " | |
| "Built by Anil S. Prasad — Ambharii Labs" | |
| ), | |
| examples=[ | |
| ["credit_risk_model", 32, True, "NIST_RMF_1.0"], | |
| ["fraud_detection_v2", 64, True, "EU_AI_Act"], | |
| ["patient_readmission", 16, True, "SR_11_7"], | |
| ["churn_predictor", 32, False, "NIST_RMF_1.0"], | |
| ], | |
| flagging_mode="never", | |
| ) | |
| demo.launch( | |
| theme=gr.themes.Soft(), | |
| ) |