Update the code
Browse files- app.py +425 -78
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,12 +1,385 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
import time
|
|
|
10 |
|
11 |
# Load the OpenAI API key from environment variables
|
12 |
load_dotenv()
|
@@ -44,78 +417,57 @@ def extract_terms_from_contract(contract_text):
|
|
44 |
"Provide the extracted terms in JSON format."
|
45 |
)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
wait_time *= 2 # Exponential backoff
|
68 |
-
else:
|
69 |
-
st.error("Rate limit exceeded. Please try again later.")
|
70 |
-
return None
|
71 |
-
|
72 |
-
def analyze_task_compliance(task_description, cost_estimate, contract_terms):
|
73 |
prompt = (
|
74 |
"You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
|
75 |
"Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
|
76 |
"Your job is to analyze the task description and specify if it violates any conditions from the contract. "
|
77 |
"If there are violations, list the reasons for each violation.\n\n"
|
78 |
-
f"Contract terms:\n{
|
79 |
f"Task description:\n{task_description}\n"
|
80 |
f"Cost estimate:\n{cost_estimate}\n\n"
|
81 |
"Provide the compliance analysis in a clear JSON format."
|
82 |
)
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
stop=None,
|
97 |
-
temperature=0.1,
|
98 |
-
stream=True,
|
99 |
-
)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
st.write(chunk_text)
|
106 |
-
st.json(chunk_text)
|
107 |
-
|
108 |
-
return json.loads(compliance_analysis)
|
109 |
-
except openai.error.RateLimitError:
|
110 |
-
if i < retries - 1:
|
111 |
-
st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
|
112 |
-
time.sleep(wait_time)
|
113 |
-
wait_time *= 2 # Exponential backoff
|
114 |
-
else:
|
115 |
-
st.error("Rate limit exceeded. Please try again later.")
|
116 |
-
return None
|
117 |
|
118 |
def main():
|
|
|
119 |
st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
|
120 |
|
121 |
# File upload buttons one after another
|
@@ -133,7 +485,7 @@ def main():
|
|
133 |
# Extract contract text and terms
|
134 |
contract_text = extract_text_from_docx(docx_file)
|
135 |
extracted_terms_json = extract_terms_from_contract(contract_text)
|
136 |
-
|
137 |
if extracted_terms_json is None:
|
138 |
return
|
139 |
|
@@ -142,7 +494,7 @@ def main():
|
|
142 |
except json.JSONDecodeError as e:
|
143 |
st.error(f"JSON decoding error: {e}")
|
144 |
return
|
145 |
-
|
146 |
# Read task descriptions and cost estimates from XLSX or CSV
|
147 |
if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
148 |
tasks_df = pd.read_excel(data_file)
|
@@ -150,23 +502,16 @@ def main():
|
|
150 |
tasks_df = pd.read_csv(data_file)
|
151 |
|
152 |
compliance_results = []
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
cost_estimate = row['Amount']
|
160 |
-
futures.append(executor.submit(analyze_task_compliance, task_description, cost_estimate, contract_terms))
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
if result is not None:
|
166 |
-
compliance_results.append(result)
|
167 |
-
except Exception as e:
|
168 |
-
st.error(f"An error occurred: {e}")
|
169 |
-
|
170 |
col1, col2 = st.columns(2)
|
171 |
|
172 |
with col1:
|
@@ -193,6 +538,8 @@ def main():
|
|
193 |
file_name="compliance_results.json",
|
194 |
mime="application/json"
|
195 |
)
|
|
|
|
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
main()
|
|
|
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 |
+
# import time
|
10 |
+
|
11 |
+
# # Load the OpenAI API key from environment variables
|
12 |
+
# load_dotenv()
|
13 |
+
# api_key = os.getenv("OPENAI_API_KEY")
|
14 |
+
# openai.api_key = api_key
|
15 |
+
|
16 |
+
# # Streamlit app layout
|
17 |
+
# st.set_page_config(layout="wide")
|
18 |
+
|
19 |
+
# # Add custom CSS for center alignment
|
20 |
+
# st.markdown("""
|
21 |
+
# <style>
|
22 |
+
# .centered-title {
|
23 |
+
# text-align: center;
|
24 |
+
# font-size: 2.5em;
|
25 |
+
# margin-top: 0;
|
26 |
+
# }
|
27 |
+
# </style>
|
28 |
+
# """, unsafe_allow_html=True)
|
29 |
+
|
30 |
+
# def extract_text_from_docx(docx_path):
|
31 |
+
# doc = Document(docx_path)
|
32 |
+
# return "\n".join([para.text for para in doc.paragraphs])
|
33 |
+
|
34 |
+
# def extract_terms_from_contract(contract_text):
|
35 |
+
# prompt = (
|
36 |
+
# "You are an AI tasked with analyzing a contract and extracting key terms and constraints. The contract contains "
|
37 |
+
# "various sections and subsections with terms related to budget constraints, types of allowable work, timelines, "
|
38 |
+
# "penalties, responsibilities, and other conditions for work execution. Your job is to extract these key terms and "
|
39 |
+
# "structure them in a clear JSON format, reflecting the hierarchy of sections and subsections. "
|
40 |
+
# "Ensure to capture all important constraints and conditions specified in the contract text. If a section or subsection "
|
41 |
+
# "contains multiple terms, list them all.\n\n"
|
42 |
+
# "Contract text:\n"
|
43 |
+
# f"{contract_text}\n\n"
|
44 |
+
# "Provide the extracted terms in JSON format."
|
45 |
+
# )
|
46 |
+
|
47 |
+
# retries = 2
|
48 |
+
# wait_time = 1
|
49 |
+
# for i in range(retries):
|
50 |
+
# try:
|
51 |
+
# response = openai.ChatCompletion.create(
|
52 |
+
# model="gpt-4",
|
53 |
+
# messages=[
|
54 |
+
# {"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
|
55 |
+
# {"role": "user", "content": prompt},
|
56 |
+
# ],
|
57 |
+
# max_tokens=4096,
|
58 |
+
# n=1,
|
59 |
+
# stop=None,
|
60 |
+
# temperature=0.1,
|
61 |
+
# )
|
62 |
+
# return response.choices[0].message["content"]
|
63 |
+
# except openai.error.RateLimitError:
|
64 |
+
# if i < retries - 1:
|
65 |
+
# st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
|
66 |
+
# time.sleep(wait_time)
|
67 |
+
# wait_time *= 2 # Exponential backoff
|
68 |
+
# else:
|
69 |
+
# st.error("Rate limit exceeded. Please try again later.")
|
70 |
+
# return None
|
71 |
+
|
72 |
+
# def analyze_task_compliance(task_description, cost_estimate, contract_terms):
|
73 |
+
# prompt = (
|
74 |
+
# "You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
|
75 |
+
# "Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
|
76 |
+
# "Your job is to analyze the task description and specify if it violates any conditions from the contract. "
|
77 |
+
# "If there are violations, list the reasons for each violation.\n\n"
|
78 |
+
# f"Contract terms:\n{json.dumps(contract_terms, indent=4)}\n\n"
|
79 |
+
# f"Task description:\n{task_description}\n"
|
80 |
+
# f"Cost estimate:\n{cost_estimate}\n\n"
|
81 |
+
# "Provide the compliance analysis in a clear JSON format."
|
82 |
+
# )
|
83 |
+
|
84 |
+
# retries = 5
|
85 |
+
# wait_time = 1
|
86 |
+
# for i in range(retries):
|
87 |
+
# try:
|
88 |
+
# response = openai.ChatCompletion.create(
|
89 |
+
# model="gpt-4",
|
90 |
+
# messages=[
|
91 |
+
# {"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
|
92 |
+
# {"role": "user", "content": prompt},
|
93 |
+
# ],
|
94 |
+
# max_tokens=4096,
|
95 |
+
# n=1,
|
96 |
+
# stop=None,
|
97 |
+
# temperature=0.1,
|
98 |
+
# stream=True,
|
99 |
+
# )
|
100 |
+
|
101 |
+
# compliance_analysis = ""
|
102 |
+
# for chunk in response:
|
103 |
+
# chunk_text = chunk['choices'][0]['delta'].get('content', '')
|
104 |
+
# compliance_analysis += chunk_text
|
105 |
+
# st.write(chunk_text)
|
106 |
+
# st.json(chunk_text)
|
107 |
+
|
108 |
+
# return json.loads(compliance_analysis)
|
109 |
+
# except openai.error.RateLimitError:
|
110 |
+
# if i < retries - 1:
|
111 |
+
# st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
|
112 |
+
# time.sleep(wait_time)
|
113 |
+
# wait_time *= 2 # Exponential backoff
|
114 |
+
# else:
|
115 |
+
# st.error("Rate limit exceeded. Please try again later.")
|
116 |
+
# return None
|
117 |
+
|
118 |
+
# def main():
|
119 |
+
# st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
|
120 |
+
|
121 |
+
# # File upload buttons one after another
|
122 |
+
# st.sidebar.file_uploader("Upload Contract Document (DOCX)", type="docx", key="docx_file")
|
123 |
+
# st.sidebar.file_uploader("Upload Task Descriptions (XLSX or CSV)", type=["xlsx", "csv"], key="data_file")
|
124 |
+
# submit_button = st.sidebar.button("Submit")
|
125 |
+
|
126 |
+
# docx_file = st.session_state.get("docx_file")
|
127 |
+
# data_file = st.session_state.get("data_file")
|
128 |
+
|
129 |
+
# if submit_button and docx_file and data_file:
|
130 |
+
# # Clear previous information
|
131 |
+
# st.session_state.clear()
|
132 |
+
|
133 |
+
# # Extract contract text and terms
|
134 |
+
# contract_text = extract_text_from_docx(docx_file)
|
135 |
+
# extracted_terms_json = extract_terms_from_contract(contract_text)
|
136 |
+
|
137 |
+
# if extracted_terms_json is None:
|
138 |
+
# return
|
139 |
+
|
140 |
+
# try:
|
141 |
+
# contract_terms = json.loads(extracted_terms_json)
|
142 |
+
# except json.JSONDecodeError as e:
|
143 |
+
# st.error(f"JSON decoding error: {e}")
|
144 |
+
# return
|
145 |
+
|
146 |
+
# # Read task descriptions and cost estimates from XLSX or CSV
|
147 |
+
# if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
148 |
+
# tasks_df = pd.read_excel(data_file)
|
149 |
+
# else:
|
150 |
+
# tasks_df = pd.read_csv(data_file)
|
151 |
+
|
152 |
+
# compliance_results = []
|
153 |
+
# futures = []
|
154 |
+
|
155 |
+
# # Use ThreadPoolExecutor to analyze tasks concurrently
|
156 |
+
# with ThreadPoolExecutor(max_workers=10) as executor: # Adjust max_workers as needed
|
157 |
+
# for _, row in tasks_df.iterrows():
|
158 |
+
# task_description = row['Task Description']
|
159 |
+
# cost_estimate = row['Amount']
|
160 |
+
# futures.append(executor.submit(analyze_task_compliance, task_description, cost_estimate, contract_terms))
|
161 |
+
|
162 |
+
# for future in as_completed(futures):
|
163 |
+
# try:
|
164 |
+
# result = future.result()
|
165 |
+
# if result is not None:
|
166 |
+
# compliance_results.append(result)
|
167 |
+
# except Exception as e:
|
168 |
+
# st.error(f"An error occurred: {e}")
|
169 |
+
|
170 |
+
# col1, col2 = st.columns(2)
|
171 |
+
|
172 |
+
# with col1:
|
173 |
+
# st.write("Extracted Contract Terms:")
|
174 |
+
# st.json(contract_terms)
|
175 |
+
|
176 |
+
# # Download button for contract terms
|
177 |
+
# st.download_button(
|
178 |
+
# label="Download Contract Terms",
|
179 |
+
# data=json.dumps(contract_terms, indent=4),
|
180 |
+
# file_name="contract_terms.json",
|
181 |
+
# mime="application/json"
|
182 |
+
# )
|
183 |
+
|
184 |
+
# with col2:
|
185 |
+
# st.write("Compliance Results:")
|
186 |
+
# st.json(compliance_results)
|
187 |
+
|
188 |
+
# # Download button for compliance results
|
189 |
+
# compliance_results_json = json.dumps(compliance_results, indent=4)
|
190 |
+
# st.download_button(
|
191 |
+
# label="Download Compliance Results",
|
192 |
+
# data=compliance_results_json,
|
193 |
+
# file_name="compliance_results.json",
|
194 |
+
# mime="application/json"
|
195 |
+
# )
|
196 |
+
|
197 |
+
# if __name__ == "__main__":
|
198 |
+
# main()
|
199 |
+
|
200 |
+
|
201 |
+
# import streamlit as st
|
202 |
+
# import os
|
203 |
+
# import openai
|
204 |
+
# import json
|
205 |
+
# import pandas as pd
|
206 |
+
# from docx import Document
|
207 |
+
# from dotenv import load_dotenv
|
208 |
+
# import time
|
209 |
+
|
210 |
+
|
211 |
+
# # Load the OpenAI API key from environment variables
|
212 |
+
# load_dotenv()
|
213 |
+
# api_key = os.getenv("OPENAI_API_KEY")
|
214 |
+
# openai.api_key = api_key
|
215 |
+
|
216 |
+
# # Streamlit app layout
|
217 |
+
# st.set_page_config(layout="wide")
|
218 |
+
|
219 |
+
# # Add custom CSS for center alignment
|
220 |
+
# st.markdown("""
|
221 |
+
# <style>
|
222 |
+
# .centered-title {
|
223 |
+
# text-align: center;
|
224 |
+
# font-size: 2.5em;
|
225 |
+
# margin-top: 0;
|
226 |
+
# }
|
227 |
+
# </style>
|
228 |
+
# """, unsafe_allow_html=True)
|
229 |
+
|
230 |
+
# def extract_text_from_docx(docx_path):
|
231 |
+
# doc = Document(docx_path)
|
232 |
+
# return "\n".join([para.text for para in doc.paragraphs])
|
233 |
+
|
234 |
+
# def extract_terms_from_contract(contract_text):
|
235 |
+
# prompt = (
|
236 |
+
# "You are an AI tasked with analyzing a contract and extracting key terms and constraints. The contract contains "
|
237 |
+
# "various sections and subsections with terms related to budget constraints, types of allowable work, timelines, "
|
238 |
+
# "penalties, responsibilities, and other conditions for work execution. Your job is to extract these key terms and "
|
239 |
+
# "structure them in a clear JSON format, reflecting the hierarchy of sections and subsections. "
|
240 |
+
# "Ensure to capture all important constraints and conditions specified in the contract text. If a section or subsection "
|
241 |
+
# "contains multiple terms, list them all.\n\n"
|
242 |
+
# "Contract text:\n"
|
243 |
+
# f"{contract_text}\n\n"
|
244 |
+
# "Provide the extracted terms in JSON format."
|
245 |
+
# )
|
246 |
+
|
247 |
+
# try:
|
248 |
+
# response = openai.ChatCompletion.create(
|
249 |
+
# model="gpt-4",
|
250 |
+
# messages=[
|
251 |
+
# {"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
|
252 |
+
# {"role": "user", "content": prompt},
|
253 |
+
# ],
|
254 |
+
# max_tokens=4096,
|
255 |
+
# n=1,
|
256 |
+
# stop=None,
|
257 |
+
# temperature=0.1,
|
258 |
+
# )
|
259 |
+
# return response.choices[0].message["content"]
|
260 |
+
# except openai.error.OpenAIError as e:
|
261 |
+
# st.error(f"Error extracting terms from contract: {e}")
|
262 |
+
# return None
|
263 |
+
|
264 |
+
# def analyze_task_compliance(task_description, cost_estimate, contract_text):
|
265 |
+
# prompt = (
|
266 |
+
# "You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
|
267 |
+
# "Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
|
268 |
+
# "Your job is to analyze the task description and specify if it violates any conditions from the contract. "
|
269 |
+
# "If there are violations, list the reasons for each violation.\n\n"
|
270 |
+
# f"Contract terms:\n{contract_text}\n\n"
|
271 |
+
# f"Task description:\n{task_description}\n"
|
272 |
+
# f"Cost estimate:\n{cost_estimate}\n\n"
|
273 |
+
# "Provide the compliance analysis in a clear JSON format."
|
274 |
+
# )
|
275 |
+
|
276 |
+
# try:
|
277 |
+
# response = openai.ChatCompletion.create(
|
278 |
+
# model="gpt-4",
|
279 |
+
# messages=[
|
280 |
+
# {"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
|
281 |
+
# {"role": "user", "content": prompt},
|
282 |
+
# ],
|
283 |
+
# max_tokens=4096,
|
284 |
+
# n=1,
|
285 |
+
# stop=None,
|
286 |
+
# temperature=0.1,
|
287 |
+
# )
|
288 |
+
|
289 |
+
# return json.loads(response.choices[0].message["content"])
|
290 |
+
# except openai.error.OpenAIError as e:
|
291 |
+
# st.error(f"Error analyzing task compliance: {e}")
|
292 |
+
# return None
|
293 |
+
|
294 |
+
# def main():
|
295 |
+
# start = time.time()
|
296 |
+
# st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
|
297 |
+
|
298 |
+
# # File upload buttons one after another
|
299 |
+
# st.sidebar.file_uploader("Upload Contract Document (DOCX)", type="docx", key="docx_file")
|
300 |
+
# st.sidebar.file_uploader("Upload Task Descriptions (XLSX or CSV)", type=["xlsx", "csv"], key="data_file")
|
301 |
+
# submit_button = st.sidebar.button("Submit")
|
302 |
+
|
303 |
+
# docx_file = st.session_state.get("docx_file")
|
304 |
+
# data_file = st.session_state.get("data_file")
|
305 |
+
|
306 |
+
# if submit_button and docx_file and data_file:
|
307 |
+
# # Clear previous information
|
308 |
+
# st.session_state.clear()
|
309 |
+
|
310 |
+
# # Extract contract text and terms
|
311 |
+
# contract_text = extract_text_from_docx(docx_file)
|
312 |
+
# extracted_terms_json = extract_terms_from_contract(contract_text)
|
313 |
+
|
314 |
+
# if extracted_terms_json is None:
|
315 |
+
# return
|
316 |
+
|
317 |
+
# try:
|
318 |
+
# contract_terms = json.loads(extracted_terms_json)
|
319 |
+
# except json.JSONDecodeError as e:
|
320 |
+
# st.error(f"JSON decoding error: {e}")
|
321 |
+
# return
|
322 |
+
# # Introducing a 1-second delay before analyzing task compliance
|
323 |
+
# time.sleep(8)
|
324 |
+
# # Read task descriptions and cost estimates from XLSX or CSV
|
325 |
+
# if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
326 |
+
# tasks_df = pd.read_excel(data_file)
|
327 |
+
# else:
|
328 |
+
# tasks_df = pd.read_csv(data_file)
|
329 |
+
|
330 |
+
# compliance_results = []
|
331 |
+
|
332 |
+
# # Process tasks sequentially
|
333 |
+
# for _, row in tasks_df.iterrows():
|
334 |
+
# task_description = row['Task Description']
|
335 |
+
# cost_estimate = row['Amount']
|
336 |
+
# result = analyze_task_compliance(task_description, cost_estimate, contract_text)
|
337 |
+
|
338 |
+
# if result is not None:
|
339 |
+
# compliance_results.append(result)
|
340 |
+
|
341 |
+
# col1, col2 = st.columns(2)
|
342 |
+
|
343 |
+
# with col1:
|
344 |
+
# st.write("Extracted Contract Terms:")
|
345 |
+
# st.json(contract_terms)
|
346 |
+
|
347 |
+
# # Download button for contract terms
|
348 |
+
# st.download_button(
|
349 |
+
# label="Download Contract Terms",
|
350 |
+
# data=json.dumps(contract_terms, indent=4),
|
351 |
+
# file_name="contract_terms.json",
|
352 |
+
# mime="application/json"
|
353 |
+
# )
|
354 |
+
|
355 |
+
# with col2:
|
356 |
+
# st.write("Compliance Results:")
|
357 |
+
# st.json(compliance_results)
|
358 |
+
|
359 |
+
# # Download button for compliance results
|
360 |
+
# compliance_results_json = json.dumps(compliance_results, indent=4)
|
361 |
+
# st.download_button(
|
362 |
+
# label="Download Compliance Results",
|
363 |
+
# data=compliance_results_json,
|
364 |
+
# file_name="compliance_results.json",
|
365 |
+
# mime="application/json"
|
366 |
+
# )
|
367 |
+
# end = time.time()
|
368 |
+
# print("Total Time: ", end-start)
|
369 |
+
|
370 |
+
# if __name__ == "__main__":
|
371 |
+
# main()
|
372 |
+
|
373 |
+
|
374 |
import streamlit as st
|
375 |
import os
|
376 |
import openai
|
377 |
import json
|
378 |
import pandas as pd
|
379 |
from docx import Document
|
|
|
380 |
from dotenv import load_dotenv
|
381 |
import time
|
382 |
+
import retrying
|
383 |
|
384 |
# Load the OpenAI API key from environment variables
|
385 |
load_dotenv()
|
|
|
417 |
"Provide the extracted terms in JSON format."
|
418 |
)
|
419 |
|
420 |
+
try:
|
421 |
+
response = openai.ChatCompletion.create(
|
422 |
+
model="gpt-4",
|
423 |
+
messages=[
|
424 |
+
{"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
|
425 |
+
{"role": "user", "content": prompt},
|
426 |
+
],
|
427 |
+
max_tokens=4096,
|
428 |
+
n=1,
|
429 |
+
stop=None,
|
430 |
+
temperature=0.1,
|
431 |
+
)
|
432 |
+
return response.choices[0].message["content"]
|
433 |
+
except openai.error.OpenAIError as e:
|
434 |
+
st.error(f"Error extracting terms from contract: {e}")
|
435 |
+
return None
|
436 |
+
|
437 |
+
# Add a retry decorator with exponential backoff
|
438 |
+
@retrying.retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=5)
|
439 |
+
def analyze_task_compliance(task_description, cost_estimate, contract_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
440 |
prompt = (
|
441 |
"You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
|
442 |
"Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
|
443 |
"Your job is to analyze the task description and specify if it violates any conditions from the contract. "
|
444 |
"If there are violations, list the reasons for each violation.\n\n"
|
445 |
+
f"Contract terms:\n{contract_text}\n\n"
|
446 |
f"Task description:\n{task_description}\n"
|
447 |
f"Cost estimate:\n{cost_estimate}\n\n"
|
448 |
"Provide the compliance analysis in a clear JSON format."
|
449 |
)
|
450 |
|
451 |
+
try:
|
452 |
+
response = openai.ChatCompletion.create(
|
453 |
+
model="gpt-4",
|
454 |
+
messages=[
|
455 |
+
{"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
|
456 |
+
{"role": "user", "content": prompt},
|
457 |
+
],
|
458 |
+
max_tokens=4096,
|
459 |
+
n=1,
|
460 |
+
stop=None,
|
461 |
+
temperature=0.1,
|
462 |
+
)
|
|
|
|
|
|
|
|
|
463 |
|
464 |
+
return json.loads(response.choices[0].message["content"])
|
465 |
+
except openai.error.OpenAIError as e:
|
466 |
+
st.error(f"Error analyzing task compliance: {e}")
|
467 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
468 |
|
469 |
def main():
|
470 |
+
start = time.time()
|
471 |
st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
|
472 |
|
473 |
# File upload buttons one after another
|
|
|
485 |
# Extract contract text and terms
|
486 |
contract_text = extract_text_from_docx(docx_file)
|
487 |
extracted_terms_json = extract_terms_from_contract(contract_text)
|
488 |
+
|
489 |
if extracted_terms_json is None:
|
490 |
return
|
491 |
|
|
|
494 |
except json.JSONDecodeError as e:
|
495 |
st.error(f"JSON decoding error: {e}")
|
496 |
return
|
497 |
+
|
498 |
# Read task descriptions and cost estimates from XLSX or CSV
|
499 |
if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
500 |
tasks_df = pd.read_excel(data_file)
|
|
|
502 |
tasks_df = pd.read_csv(data_file)
|
503 |
|
504 |
compliance_results = []
|
505 |
+
|
506 |
+
# Process tasks sequentially
|
507 |
+
for _, row in tasks_df.iterrows():
|
508 |
+
task_description = row['Task Description']
|
509 |
+
cost_estimate = row['Amount']
|
510 |
+
result = analyze_task_compliance(task_description, cost_estimate, contract_text)
|
|
|
|
|
511 |
|
512 |
+
if result is not None:
|
513 |
+
compliance_results.append(result)
|
514 |
+
|
|
|
|
|
|
|
|
|
|
|
515 |
col1, col2 = st.columns(2)
|
516 |
|
517 |
with col1:
|
|
|
538 |
file_name="compliance_results.json",
|
539 |
mime="application/json"
|
540 |
)
|
541 |
+
end = time.time()
|
542 |
+
print("Total Time: ", end-start)
|
543 |
|
544 |
if __name__ == "__main__":
|
545 |
main()
|
requirements.txt
CHANGED
@@ -5,3 +5,4 @@ python-docx
|
|
5 |
pandas
|
6 |
streamlit
|
7 |
openpyxl
|
|
|
|
5 |
pandas
|
6 |
streamlit
|
7 |
openpyxl
|
8 |
+
retrying
|