contract verification
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ 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()
|
@@ -29,18 +30,29 @@ def extract_terms_from_contract(contract_text):
|
|
29 |
"Provide the extracted terms in JSON format."
|
30 |
)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def analyze_task_compliance(task_description, cost_estimate, contract_terms):
|
46 |
prompt = (
|
@@ -54,23 +66,31 @@ def analyze_task_compliance(task_description, cost_estimate, contract_terms):
|
|
54 |
"Provide the compliance analysis in a clear JSON format."
|
55 |
)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
def main():
|
76 |
st.title("Contract Compliance Analyzer")
|
@@ -85,10 +105,16 @@ def main():
|
|
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:
|
@@ -111,7 +137,8 @@ def main():
|
|
111 |
for future in as_completed(futures):
|
112 |
try:
|
113 |
result = future.result()
|
114 |
-
|
|
|
115 |
except Exception as e:
|
116 |
st.error(f"An error occurred: {e}")
|
117 |
|
@@ -144,3 +171,4 @@ def main():
|
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
main()
|
|
|
|
6 |
from docx import Document
|
7 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
8 |
from dotenv import load_dotenv
|
9 |
+
import time
|
10 |
|
11 |
# Load the OpenAI API key from environment variables
|
12 |
load_dotenv()
|
|
|
30 |
"Provide the extracted terms in JSON format."
|
31 |
)
|
32 |
|
33 |
+
retries = 5
|
34 |
+
for i in range(retries):
|
35 |
+
try:
|
36 |
+
response = openai.ChatCompletion.create(
|
37 |
+
model="gpt-4",
|
38 |
+
messages=[
|
39 |
+
{"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
|
40 |
+
{"role": "user", "content": prompt},
|
41 |
+
],
|
42 |
+
max_tokens=4096,
|
43 |
+
n=1,
|
44 |
+
stop=None,
|
45 |
+
temperature=0.1,
|
46 |
+
)
|
47 |
+
return response.choices[0].message["content"]
|
48 |
+
except openai.error.RateLimitError as e:
|
49 |
+
if i < retries - 1:
|
50 |
+
wait_time = 2 ** i
|
51 |
+
st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
|
52 |
+
time.sleep(wait_time)
|
53 |
+
else:
|
54 |
+
st.error("Rate limit exceeded. Please try again later.")
|
55 |
+
return None
|
56 |
|
57 |
def analyze_task_compliance(task_description, cost_estimate, contract_terms):
|
58 |
prompt = (
|
|
|
66 |
"Provide the compliance analysis in a clear JSON format."
|
67 |
)
|
68 |
|
69 |
+
retries = 5
|
70 |
+
for i in range(retries):
|
71 |
+
try:
|
72 |
+
response = openai.ChatCompletion.create(
|
73 |
+
model="gpt-4",
|
74 |
+
messages=[
|
75 |
+
{"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
|
76 |
+
{"role": "user", "content": prompt},
|
77 |
+
],
|
78 |
+
max_tokens=4096,
|
79 |
+
n=1,
|
80 |
+
stop=None,
|
81 |
+
temperature=0.1,
|
82 |
+
)
|
83 |
+
response_content = response.choices[0].message["content"]
|
84 |
+
compliance_analysis = json.loads(response_content)
|
85 |
+
return compliance_analysis
|
86 |
+
except openai.error.RateLimitError as e:
|
87 |
+
if i < retries - 1:
|
88 |
+
wait_time = 2 ** i
|
89 |
+
st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
|
90 |
+
time.sleep(wait_time)
|
91 |
+
else:
|
92 |
+
st.error("Rate limit exceeded. Please try again later.")
|
93 |
+
return None
|
94 |
|
95 |
def main():
|
96 |
st.title("Contract Compliance Analyzer")
|
|
|
105 |
xlsx_file = st.file_uploader("Upload Task Descriptions (XLSX)", type="xlsx")
|
106 |
|
107 |
if docx_file and xlsx_file:
|
108 |
+
# Clear previous information
|
109 |
+
st.session_state.clear()
|
110 |
+
|
111 |
# Extract contract text and terms
|
112 |
contract_text = extract_text_from_docx(docx_file)
|
113 |
extracted_terms_json = extract_terms_from_contract(contract_text)
|
114 |
|
115 |
+
if extracted_terms_json is None:
|
116 |
+
return
|
117 |
+
|
118 |
try:
|
119 |
contract_terms = json.loads(extracted_terms_json)
|
120 |
except json.JSONDecodeError as e:
|
|
|
137 |
for future in as_completed(futures):
|
138 |
try:
|
139 |
result = future.result()
|
140 |
+
if result is not None:
|
141 |
+
compliance_results.append(result)
|
142 |
except Exception as e:
|
143 |
st.error(f"An error occurred: {e}")
|
144 |
|
|
|
171 |
|
172 |
if __name__ == "__main__":
|
173 |
main()
|
174 |
+
|