Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
# Initialize Flask app
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Load pre-trained model and tokenizer
|
| 13 |
+
MODEL_PATH = "path/to/your/model" # Update with your Hugging Face model path
|
| 14 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
|
| 15 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 16 |
+
|
| 17 |
+
# Function to process contract text and classify clauses
|
| 18 |
+
def classify_clause(contract_text):
|
| 19 |
+
inputs = tokenizer(contract_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
logits = outputs.logits
|
| 23 |
+
predicted_class = torch.argmax(logits, dim=-1).item() # Get predicted class (risk tag)
|
| 24 |
+
|
| 25 |
+
# Define risk labels (assuming 3 risk levels: low, medium, high)
|
| 26 |
+
risk_labels = ["low", "medium", "high"]
|
| 27 |
+
predicted_risk = risk_labels[predicted_class]
|
| 28 |
+
|
| 29 |
+
# Get confidence score (softmax output)
|
| 30 |
+
softmax = torch.nn.Softmax(dim=-1)
|
| 31 |
+
confidence = softmax(logits).squeeze().tolist()[predicted_class]
|
| 32 |
+
|
| 33 |
+
return {"predicted_risk": predicted_risk, "confidence_score": confidence}
|
| 34 |
+
|
| 35 |
+
# Define route to handle file uploads
|
| 36 |
+
@app.route("/upload_contract", methods=["POST"])
|
| 37 |
+
def upload_contract():
|
| 38 |
+
# Extract file from the request
|
| 39 |
+
if 'file' not in request.files:
|
| 40 |
+
return jsonify({"error": "No file part"}), 400
|
| 41 |
+
|
| 42 |
+
file = request.files['file']
|
| 43 |
+
|
| 44 |
+
if file.filename == '':
|
| 45 |
+
return jsonify({"error": "No selected file"}), 400
|
| 46 |
+
|
| 47 |
+
contract_text = file.read().decode('utf-8') # Assuming the file is a text-based contract
|
| 48 |
+
|
| 49 |
+
# Classify the contract text
|
| 50 |
+
result = classify_clause(contract_text)
|
| 51 |
+
|
| 52 |
+
# Prepare JSON response
|
| 53 |
+
response_data = {
|
| 54 |
+
"contract_title": "Sample Contract", # Placeholder, can be parsed from the file
|
| 55 |
+
"overall_risk_score": result["predicted_risk"], # Risk classification
|
| 56 |
+
"high_risk_clauses": ["Termination Clause", "Penalty Clause"], # Example (this should be dynamically extracted)
|
| 57 |
+
"risk_map_url": "https://example.com/risk_map", # Placeholder (use actual URL for visualization)
|
| 58 |
+
"evaluation_date": datetime.now().strftime("%Y-%m-%d")
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# Return response as JSON
|
| 62 |
+
return jsonify(response_data)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
app.run(debug=True, host="0.0.0.0", port=5000)
|