Bikas0 commited on
Commit
5fc6a9d
·
1 Parent(s): 4d273c8

contract verification

Browse files
Files changed (2) hide show
  1. app.py +146 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import openai
4
+ import json
5
+ import pandas as pd
6
+ from docx import Document
7
+ from concurrent.futures import ThreadPoolExecutor, as_completed
8
+ from dotenv import load_dotenv
9
+
10
+ # Load the OpenAI API key from environment variables
11
+ load_dotenv()
12
+ api_key = os.getenv("OPENAI_API_KEY")
13
+ openai.api_key = api_key
14
+
15
+ def extract_text_from_docx(docx_path):
16
+ doc = Document(docx_path)
17
+ return "\n".join([para.text for para in doc.paragraphs])
18
+
19
+ def extract_terms_from_contract(contract_text):
20
+ prompt = (
21
+ "You are an AI tasked with analyzing a contract and extracting key terms and constraints. The contract contains "
22
+ "various sections and subsections with terms related to budget constraints, types of allowable work, timelines, "
23
+ "penalties, responsibilities, and other conditions for work execution. Your job is to extract these key terms and "
24
+ "structure them in a clear JSON format, reflecting the hierarchy of sections and subsections. "
25
+ "Ensure to capture all important constraints and conditions specified in the contract text. If a section or subsection "
26
+ "contains multiple terms, list them all.\n\n"
27
+ "Contract text:\n"
28
+ f"{contract_text}\n\n"
29
+ "Provide the extracted terms in JSON format."
30
+ )
31
+
32
+ response = openai.ChatCompletion.create(
33
+ model="gpt-4",
34
+ messages=[
35
+ {"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
36
+ {"role": "user", "content": prompt},
37
+ ],
38
+ max_tokens=4096,
39
+ n=1,
40
+ stop=None,
41
+ temperature=0.1,
42
+ )
43
+ return response.choices[0].message["content"]
44
+
45
+ def analyze_task_compliance(task_description, cost_estimate, contract_terms):
46
+ prompt = (
47
+ "You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
48
+ "Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
49
+ "Your job is to analyze the task description and specify if it violates any conditions from the contract. "
50
+ "If there are violations, list the reasons for each violation.\n\n"
51
+ f"Contract terms:\n{json.dumps(contract_terms, indent=4)}\n\n"
52
+ f"Task description:\n{task_description}\n"
53
+ f"Cost estimate:\n{cost_estimate}\n\n"
54
+ "Provide the compliance analysis in a clear JSON format."
55
+ )
56
+
57
+ response = openai.ChatCompletion.create(
58
+ model="gpt-4",
59
+ messages=[
60
+ {"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
61
+ {"role": "user", "content": prompt},
62
+ ],
63
+ max_tokens=4096,
64
+ n=1,
65
+ stop=None,
66
+ temperature=0.1,
67
+ )
68
+
69
+ # Parse the response to extract structured explanations
70
+ response_content = response.choices[0].message["content"]
71
+ compliance_analysis = json.loads(response_content)
72
+
73
+ return compliance_analysis
74
+
75
+ def main():
76
+ st.title("Contract Compliance Analyzer")
77
+
78
+ # File upload buttons in the same row
79
+ col1, col2 = st.columns(2)
80
+
81
+ with col1:
82
+ docx_file = st.file_uploader("Upload Contract Document (DOCX)", type="docx")
83
+
84
+ with col2:
85
+ xlsx_file = st.file_uploader("Upload Task Descriptions (XLSX)", type="xlsx")
86
+
87
+ if docx_file and xlsx_file:
88
+ # Extract contract text and terms
89
+ contract_text = extract_text_from_docx(docx_file)
90
+ extracted_terms_json = extract_terms_from_contract(contract_text)
91
+
92
+ try:
93
+ contract_terms = json.loads(extracted_terms_json)
94
+ except json.JSONDecodeError as e:
95
+ st.error(f"JSON decoding error: {e}")
96
+ return
97
+
98
+ # Read task descriptions and cost estimates from XLSX
99
+ tasks_df = pd.read_excel(xlsx_file)
100
+
101
+ compliance_results = []
102
+ futures = []
103
+
104
+ # Use ThreadPoolExecutor to analyze tasks concurrently
105
+ with ThreadPoolExecutor(max_workers=10) as executor: # Adjust max_workers as needed
106
+ for _, row in tasks_df.iterrows():
107
+ task_description = row['Task Description']
108
+ cost_estimate = row['Amount']
109
+ futures.append(executor.submit(analyze_task_compliance, task_description, cost_estimate, contract_terms))
110
+
111
+ for future in as_completed(futures):
112
+ try:
113
+ result = future.result()
114
+ compliance_results.append(result)
115
+ except Exception as e:
116
+ st.error(f"An error occurred: {e}")
117
+
118
+ col1, col2 = st.columns(2)
119
+
120
+ with col1:
121
+ st.write("Extracted Contract Terms:")
122
+ st.json(contract_terms)
123
+
124
+ # Download button for contract terms
125
+ st.download_button(
126
+ label="Download Contract Terms",
127
+ data=json.dumps(contract_terms, indent=4),
128
+ file_name="contract_terms.json",
129
+ mime="application/json"
130
+ )
131
+
132
+ with col2:
133
+ st.write("Compliance Results:")
134
+ st.json(compliance_results)
135
+
136
+ # Download button for compliance results
137
+ compliance_results_json = json.dumps(compliance_results, indent=4)
138
+ st.download_button(
139
+ label="Download Compliance Results",
140
+ data=compliance_results_json,
141
+ file_name="compliance_results.json",
142
+ mime="application/json"
143
+ )
144
+
145
+ if __name__ == "__main__":
146
+ main()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ openai==0.28
2
+ python-dotenv
3
+ flask
4
+ python-docx
5
+ pandas
6
+ streamlit