import os
import json
import html
import requests
import gradio as gr
import spaces
import torch
AWS_API_URL = os.getenv("API_URL", "https://betaprecision.com/api/v1/chain-of-custody/analyze")
# --- ZeroGPU Accelerated Analysis Function ---
@spaces.GPU(duration=60)
def analyze_on_zerogpu(contract_text, doc_name="Master_Agreement_ZeroGPU.txt"):
if not contract_text or len(contract_text.strip()) < 10:
return "
⚠️ Error: Please enter at least 10 characters of contract text.
", "{}"
if torch.cuda.is_available():
_ = torch.eye(9, device="cuda")
payload = {
"docName": doc_name,
"text": contract_text
}
try:
res = requests.post(AWS_API_URL, json=payload, headers={"Content-Type": "application/json"}, timeout=30)
res.raise_for_status()
data = res.json()
except Exception as e:
return f"
❌ API Error: {str(e)}
", json.dumps({"error": str(e)})
# Generate isolated 3D Three.js + CoC Docket Application
iframe_html = build_isolated_viewer(data)
return iframe_html, json.dumps(data, indent=2)
def build_isolated_viewer(data):
# Safe JSON encoding for embedding in HTML
json_payload = json.dumps(data)
# Standalone HTML application that runs inside the iframe
inner_html = f"""
"""
# Escape HTML to safely embed in iframe srcdoc
escaped_srcdoc = html.escape(inner_html, quote=True)
return f''
DEFAULT_CONTRACT = """ARTICLE 1. APPOINTMENT & SCOPE
Provider shall perform professional technology consulting services as set forth in statements of work executed by both parties.
ARTICLE 2. UNLIMITED SPECIAL LIABILITY
Notwithstanding any other provision herein, Provider's liability under this Agreement is completely uncapped, and Provider expressly waives all exclusions for incidental, indirect, punitive, or consequential commercial damages.
ARTICLE 3. SUDDEN UNILATERAL INDEMNIFICATION
Provider shall defend, indemnify, and hold harmless Customer against any and all losses arising from market fluctuations or Customer's contributory negligence."""
# --- Gradio Interface ---
with gr.Blocks(title="Beta Precision CoC (ZeroGPU)") as demo:
gr.Markdown("# ⟠ Beta Precision: Spatiotemporal Chain-of-Custody (ZeroGPU)")
gr.Markdown("GPU-accelerated geometric tensor evaluation against high-dimensional Hilbert space anchors.")
with gr.Row():
with gr.Column(scale=1):
doc_name_in = gr.Textbox(label="Document Name", value="Master_Agreement_ZeroGPU.txt")
text_in = gr.Textbox(label="Contract Provision Text", value=DEFAULT_CONTRACT, lines=9)
run_btn = gr.Button("⚡ Run ZeroGPU CoC Analysis", variant="primary")
viewer_output = gr.HTML()
with gr.Accordion("📦 Raw JSON Operator Tensors", open=False):
json_output = gr.Code(language="json")
run_btn.click(
fn=analyze_on_zerogpu,
inputs=[text_in, doc_name_in],
outputs=[viewer_output, json_output]
)
if __name__ == "__main__":
demo.launch()