Upload Gradio_Contract_Analysis_App.ipynb
Browse files
Gradio_Contract_Analysis_App.ipynb
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 5,
|
| 4 |
+
"metadata": {},
|
| 5 |
+
"cells": [
|
| 6 |
+
{
|
| 7 |
+
"id": "e88d538f",
|
| 8 |
+
"cell_type": "markdown",
|
| 9 |
+
"source": "# 🏠 Building a Gradio Contract Analysis App\nThis notebook builds a **contract metadata extractor** (heuristic) and wraps it in a **Gradio** UI.",
|
| 10 |
+
"metadata": {}
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"id": "bff6f9f8",
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"execution_count": null,
|
| 17 |
+
"source": "!pip -q install gradio dateparser",
|
| 18 |
+
"outputs": []
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"id": "7bd73692",
|
| 22 |
+
"cell_type": "markdown",
|
| 23 |
+
"source": "## 1) Heuristic Extractors",
|
| 24 |
+
"metadata": {}
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"id": "f7e2faf6",
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"source": "\nimport re, json\nfrom typing import Dict, Any, List, Optional\nfrom dateparser.search import search_dates\n\ndef _clean(s: str) -> str:\n import re\n return re.sub(r\"\\s+\", \" \", s).strip()\n\ndef extract_parties(text: str):\n m = re.search(r\"\\bbetween\\s+(.+?)\\s+and\\s+(.+?)(?:[,.;\\n]| for\\b)\", text, re.I | re.S)\n if m:\n p1, p2 = _clean(m.group(1)), _clean(m.group(2))\n p1 = re.sub(r\"^(?:the\\s+)?(landlord|lessor|owner)\\s*[:\\-]*\\s*\", \"\", p1, flags=re.I)\n p2 = re.sub(r\"^(?:the\\s+)?(tenant|lessee|occupant)\\s*[:\\-]*\\s*\", \"\", p2, flags=re.I)\n return [p1, p2]\n return None\n\ndef extract_metadata(text: str) -> Dict[str, Any]:\n # Add more extractors as needed; minimal demo\n parties = extract_parties(text)\n dates = search_dates(text) or []\n start_date = None\n if dates:\n start_date = sorted([d for _, d in dates])[0].strftime(\"%Y-%m-%d\")\n return {\"parties\": parties, \"start_date\": start_date}\n",
|
| 32 |
+
"outputs": []
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"id": "ae18d154",
|
| 36 |
+
"cell_type": "markdown",
|
| 37 |
+
"source": "## 2) Gradio UI",
|
| 38 |
+
"metadata": {}
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"id": "4bdf7c47",
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"execution_count": null,
|
| 45 |
+
"source": "\nimport gradio as gr\n\nEXAMPLE = \"This Lease Agreement is made between Landlord X and Tenant Y for the premises at 123 Main St. Commencing on June 1, 2025.\"\n\ndef run_pipeline(t):\n meta = extract_metadata(t or \"\")\n return meta\n\nwith gr.Blocks() as demo:\n gr.Markdown(\"# 🏠 Contract Analysis Demo\")\n inp = gr.Textbox(value=EXAMPLE, label=\"Contract Text\", lines=8)\n out = gr.JSON(label=\"Extracted Metadata\")\n btn = gr.Button(\"Analyze\")\n btn.click(run_pipeline, inputs=inp, outputs=out)\n\ndemo.launch(share=False)\n",
|
| 46 |
+
"outputs": []
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"id": "f8a36080",
|
| 50 |
+
"cell_type": "markdown",
|
| 51 |
+
"source": "> Tip: move the Python into `app.py` for a Hugging Face Space and add a YAML config to your README.",
|
| 52 |
+
"metadata": {}
|
| 53 |
+
}
|
| 54 |
+
]
|
| 55 |
+
}
|