File size: 11,493 Bytes
d08ea58
3069766
2d14f55
3069766
4fad6f0
343d99f
3069766
c47cf30
 
3069766
 
 
c47cf30
 
 
 
 
343d99f
c47cf30
 
 
 
 
 
 
 
 
 
 
421fef3
c47cf30
 
 
 
 
 
 
 
 
 
 
 
558eda0
c47cf30
 
 
 
 
 
 
 
 
421fef3
c47cf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421fef3
c47cf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558eda0
c47cf30
 
 
 
 
 
 
 
 
 
 
343d99f
3069766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168be29
3069766
 
 
 
 
 
d08ea58
3069766
 
 
 
 
 
 
 
 
 
 
 
3f8b262
3069766
 
 
 
 
 
 
 
 
 
86f775d
3069766
 
 
86f775d
3069766
 
 
 
 
 
2d14f55
c47cf30
 
421fef3
c47cf30
 
 
 
 
421fef3
c47cf30
 
421fef3
 
c47cf30
 
 
 
421fef3
c47cf30
 
 
 
 
 
 
 
 
 
421fef3
c47cf30
343d99f
c47cf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d14f55
3069766
 
f31ff15
3069766
 
f31ff15
3069766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47cf30
 
f31ff15
3069766
 
 
 
 
 
c47cf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3069766
 
343d99f
c47cf30
 
 
 
 
3069766
f31ff15
c47cf30
 
 
 
 
 
 
f31ff15
3069766
 
c47cf30
 
 
 
 
 
f31ff15
 
c47cf30
3069766
c47cf30
f31ff15
c47cf30
 
 
3069766
 
 
c47cf30
 
3069766
 
c47cf30
 
3069766
c47cf30
3069766
 
 
 
c47cf30
 
3069766
f31ff15
 
343d99f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import gradio as gr
import pdfplumber
import matplotlib.pyplot as plt
import numpy as np
from word2number import w2n
import re
from typing import Tuple, List, Dict
from io import BytesIO
import base64

# Custom CSS for styling
css = """
:root {
    --low-color: #28a745;
    --medium-color: #ffc107;
    --high-color: #dc3545;
    --inactive-color: #e9ecef;
}
.risk-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 25px;
}
.risk-row {
    display: flex;
    align-items: center;
    background: white;
    border-radius: 8px;
    padding: 15px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    transition: all 0.3s ease;
}
.risk-row.active {
    transform: scale(1.02);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}
.risk-label {
    width: 100px;
    font-weight: 600;
    font-size: 16px;
    color: #495057;
}
.risk-score {
    width: 80px;
    font-size: 20px;
    font-weight: 700;
    text-align: center;
}
.risk-low { color: var(--low-color); }
.risk-medium { color: var(--medium-color); }
.risk-high { color: var(--high-color); }
.heatmap-container {
    flex-grow: 1;
    height: 30px;
    border-radius: 15px;
    overflow: hidden;
    position: relative;
}
.heatmap-bar {
    height: 100%;
    border-radius: 15px;
    transition: width 0.5s ease;
}
.risk-meter {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    font-weight: 600;
    color: white;
    text-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.result-section {
    background: white;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.result-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 15px;
    color: #343a40;
    display: flex;
    align-items: center;
    gap: 8px;
}
.clause-item {
    margin-bottom: 8px;
    padding-left: 15px;
    position: relative;
}
.clause-item:before {
    content: "β€’";
    position: absolute;
    left: 0;
    color: #6c757d;
}
.penalty-amount {
    font-family: monospace;
    background: #f8f9fa;
    padding: 2px 6px;
    border-radius: 4px;
    margin-left: 5px;
}
.example-clause {
    background: #f8f9fa;
    padding: 12px;
    border-radius: 6px;
    margin-bottom: 10px;
    border-left: 3px solid #6c757d;
}
.example-number {
    font-weight: 600;
    margin-right: 8px;
    color: #6c757d;
}
"""

def extract_text_from_pdf(pdf_path: str) -> str:
    """Extract text from PDF using pdfplumber"""
    text = ""
    with pdfplumber.open(pdf_path) as pdf:
        for page in pdf.pages:
            text += page.extract_text() or ""
    return text

def count_keywords(text: str, keywords: List[str]) -> Dict[str, int]:
    """Count occurrences of keywords in text"""
    counts = {}
    for keyword in keywords:
        counts[keyword] = len(re.findall(r'\b' + re.escape(keyword) + r'\b', text, flags=re.IGNORECASE))
    return counts

def find_penalty_values(text: str) -> List[float]:
    """Find penalty amounts in the text"""
    patterns = [
        r'\$\s*[\d,]+(?:\.\d+)?',
        r'(?:USD|usd)\s*[\d,]+(?:\.\d+)?',
        r'\d+\s*(?:percent|%)',
        r'(?:\b[a-z]+\s*)+dollars',
    ]
    
    penalties = []
    for pattern in patterns:
        matches = re.finditer(pattern, text, flags=re.IGNORECASE)
        for match in matches:
            penalty_text = match.group()
            try:
                if any(word in penalty_text.lower() for word in ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'hundred', 'thousand', 'million']):
                    penalty_value = w2n.word_to_num(penalty_text.split('dollars')[0].strip())
                else:
                    penalty_value = float(re.sub(r'[^\d.]', '', penalty_text))
                penalties.append(penalty_value)
            except:
                continue
    return penalties

def calculate_risk_score(penalty_count: int, penalty_values: List[float], obligation_count: int, delay_count: int) -> Tuple[float, str]:
    """Calculate risk score based on various factors"""
    score = 0
    score += min(penalty_count * 5, 30)
    
    if penalty_values:
        avg_penalty = sum(penalty_values) / len(penalty_values)
        if avg_penalty > 1000000:
            score += 40
        elif avg_penalty > 100000:
            score += 25
        elif avg_penalty > 10000:
            score += 15
        else:
            score += 5
    
    score += min(obligation_count * 2, 20)
    score += min(delay_count * 10, 30)
    score = min(score, 100)
    
    if score < 30:
        return score, "Low"
    elif score < 70:
        return score, "Medium"
    else:
        return score, "High"

def create_risk_display(risk_score: float, risk_level: str) -> str:
    """Create HTML display for all three risk levels"""
    risk_levels = ["Low", "Medium", "High"]
    colors = {
        "Low": "var(--low-color)",
        "Medium": "var(--medium-color)",
        "High": "var(--high-color)"
    }
    
    html_parts = []
    html_parts.append("<div class='risk-container'>")
    
    for level in risk_levels:
        active = level == risk_level
        score = risk_score if active else 0
        color = colors[level] if active else "var(--inactive-color)"
        opacity = "1" if active else "0.6"
        
        html_parts.append(f"""
        <div class='risk-row {'active' if active else ''}'>
            <div class='risk-label risk-{level.lower()}'>{level} Risk</div>
            <div class='risk-score risk-{level.lower()}'>{score:.1f}%</div>
            <div class='heatmap-container'>
                <div class='heatmap-bar" 
                     style="width: {score}%; background: {color}; opacity: {opacity}">
                    <span class='risk-meter'>{score:.1f}%</span>
                </div>
            </div>
        </div>
        """)
    
    html_parts.append("</div>")
    return "\n".join(html_parts)

def format_clauses(counts: Dict[str, int]) -> str:
    """Format clause counts into HTML"""
    return "\n".join([f"<div class='clause-item'>{kw}: <strong>{count}</strong></div>" for kw, count in counts.items()])

def format_penalty_amounts(amounts: List[float]) -> str:
    """Format penalty amounts into HTML"""
    if not amounts:
        return "<div style='color: #6c757d;'>No specific penalty amounts found</div>"
    return "\n".join([f"<div class='clause-item'><span class='penalty-amount'>${amt:,.2f}</span></div>" for amt in amounts[:5]])

def format_examples(sentences: List[str]) -> str:
    """Format example sentences into HTML"""
    if not sentences:
        return "<div style='color: #6c757d;'>No penalty clauses found</div>"
    return "\n".join([f"""
    <div class='example-clause'>
        <span class='example-number'>{i+1}.</span> {sent}
    </div>
    """ for i, sent in enumerate(sentences[:3])])

def analyze_pdf(file_obj) -> List:
    """Main analysis function for Gradio interface"""
    try:
        # Extract text from the uploaded file
        text = extract_text_from_pdf(file_obj.name)
        
        # Define keywords to search for
        penalty_keywords = ["penalty", "fine", "forfeit", "liquidated damages", "breach"]
        obligation_keywords = ["shall", "must", "required to", "obligated to", "duty"]
        delay_keywords = ["delay", "late", "overdue", "extension", "time is of the essence"]
        
        # Count keyword occurrences
        penalty_counts = count_keywords(text, penalty_keywords)
        obligation_counts = count_keywords(text, obligation_keywords)
        delay_counts = count_keywords(text, delay_keywords)
        
        # Find penalty values
        penalty_values = find_penalty_values(text)
        
        # Calculate total counts
        total_penalties = sum(penalty_counts.values())
        total_obligations = sum(obligation_counts.values())
        total_delays = sum(delay_counts.values())
        
        # Calculate risk score
        risk_score, risk_level = calculate_risk_score(
            total_penalties, penalty_values, total_obligations, total_delays
        )
        
        # Generate risk display
        risk_display = create_risk_display(risk_score, risk_level)
        
        # Find example sentences with penalties
        penalty_sentences = []
        for sentence in re.split(r'(?<=[.!?])\s+', text):
            if any(kw.lower() in sentence.lower() for kw in penalty_keywords):
                penalty_sentences.append(sentence.strip())
        
        # Format all results
        penalty_html = f"""
        <div class='result-section'>
            <div class='result-title'>πŸ“Š Penalty Clauses: <strong>{total_penalties}</strong> found</div>
            {format_clauses(penalty_counts)}
        </div>
        """
        
        amounts_html = f"""
        <div class='result-section'>
            <div class='result-title'>πŸ’° Penalty Amounts: <strong>{len(penalty_values)}</strong> found</div>
            {format_penalty_amounts(penalty_values)}
        </div>
        """
        
        obligation_html = f"""
        <div class='result-section'>
            <div class='result-title'>βš–οΈ Obligation Clauses: <strong>{total_obligations}</strong> found</div>
            {format_clauses(obligation_counts)}
        </div>
        """
        
        delay_html = f"""
        <div class='result-section'>
            <div class='result-title'>⏱️ Delay Clauses: <strong>{total_delays}</strong> found</div>
            {format_clauses(delay_counts)}
        </div>
        """
        
        examples_html = f"""
        <div class='result-section'>
            <div class='result-title'>πŸ” Example Penalty Clauses</div>
            {format_examples(penalty_sentences)}
        </div>
        """
        
        return [
            risk_display,
            penalty_html,
            amounts_html,
            obligation_html,
            delay_html,
            examples_html
        ]
    except Exception as e:
        error_html = f"""
        <div class='result-section' style='background: #fff3cd;'>
            <div class='result-title'>❌ Error</div>
            <div>{str(e)}</div>
        </div>
        """
        return [error_html] * 6

# Create Gradio interface
with gr.Blocks(css=css, title="PDF Contract Risk Analyzer") as demo:
    gr.Markdown("""
    <div style='text-align: center; margin-bottom: 30px;'>
        <h1 style='margin-bottom: 10px;'>πŸ“„ PDF Contract Risk Analyzer</h1>
        <p style='color: #6c757d;'>Upload a contract PDF to analyze penalties, obligations, and delays</p>
    </div>
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            file_input = gr.File(label="Upload PDF", file_types=[".pdf"])
            submit_btn = gr.Button("Analyze Contract", variant="primary")
        
        with gr.Column(scale=3):
            gr.Markdown("### πŸ” Risk Assessment Summary")
            risk_display = gr.HTML()
    
    with gr.Row():
        with gr.Column():
            penalty_count = gr.HTML()
            penalty_amounts = gr.HTML()
        
        with gr.Column():
            obligation_count = gr.HTML()
            delay_count = gr.HTML()
    
    penalty_examples = gr.HTML()
    
    submit_btn.click(
        fn=analyze_pdf,
        inputs=file_input,
        outputs=[risk_display, penalty_count, penalty_amounts, 
                obligation_count, delay_count, penalty_examples]
    )

if __name__ == "__main__":
    demo.launch()