Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
import base64
|
| 5 |
+
import pdfplumber
|
| 6 |
+
|
| 7 |
+
# Initialize FastAPI app
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Load BERT-based model for clause classification
|
| 11 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
| 12 |
+
|
| 13 |
+
# Function to extract text from PDF
|
| 14 |
+
def extract_text_from_pdf(pdf_data):
|
| 15 |
+
with pdfplumber.open(pdf_data) as pdf:
|
| 16 |
+
text = ""
|
| 17 |
+
for page in pdf.pages:
|
| 18 |
+
text += page.extract_text() or ""
|
| 19 |
+
return text
|
| 20 |
+
|
| 21 |
+
@app.post("/analyze_contract")
|
| 22 |
+
async def analyze_contract(file: str):
|
| 23 |
+
# Decode base64 PDF
|
| 24 |
+
pdf_data = base64.b64decode(file)
|
| 25 |
+
|
| 26 |
+
# Extract text
|
| 27 |
+
contract_text = extract_text_from_pdf(pdf_data)
|
| 28 |
+
|
| 29 |
+
# Split into clauses (simple split for demo; use regex for production)
|
| 30 |
+
clauses = contract_text.split(". ")
|
| 31 |
+
|
| 32 |
+
# Analyze each clause
|
| 33 |
+
results = []
|
| 34 |
+
for clause in clauses:
|
| 35 |
+
if clause.strip():
|
| 36 |
+
result = classifier(clause)
|
| 37 |
+
risk_score = result[0]["score"] if result[0]["label"] == "POSITIVE" else 1 - result[0]["score"]
|
| 38 |
+
results.append({
|
| 39 |
+
"clause": clause,
|
| 40 |
+
"risk_level": "High" if risk_score > 0.7 else "Medium" if risk_score > 0.4 else "Low",
|
| 41 |
+
"risk_score": risk_score
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
# Calculate overall risk score
|
| 45 |
+
overall_score = sum(r["risk_score"] for r in results) / len(results) if results else 0
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
"clauses": results,
|
| 49 |
+
"overall_score": overall_score
|
| 50 |
+
}
|