Bikas0 commited on
Commit
e9584dc
1 Parent(s): f4f51e9

update code

Browse files
Files changed (1) hide show
  1. app.py +103 -413
app.py CHANGED
@@ -1,390 +1,27 @@
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()
386
- api_key = os.getenv("OPENAI_API_KEY")
387
- openai.api_key = api_key
 
 
 
 
 
 
 
 
 
 
 
388
 
389
  # Streamlit app layout
390
  st.set_page_config(layout="wide")
@@ -417,9 +54,12 @@ def extract_terms_from_contract(contract_text):
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},
@@ -429,28 +69,37 @@ def extract_terms_from_contract(contract_text):
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},
@@ -459,15 +108,52 @@ def analyze_task_compliance(task_description, cost_estimate, contract_text):
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,7 +171,7 @@ def main():
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,7 +180,7 @@ def main():
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,16 +188,23 @@ def main():
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,9 +231,6 @@ def main():
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()
546
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import os
 
3
  import json
4
  import pandas as pd
5
  from docx import Document
6
  from dotenv import load_dotenv
7
+ from openai import AzureOpenAI
8
+ from concurrent.futures import ThreadPoolExecutor, as_completed
9
 
10
+ # Load environment variables
11
  load_dotenv()
12
+
13
+ # Azure OpenAI credentials
14
+ key = os.getenv("AZURE_OPENAI_API_KEY")
15
+ endpoint_url = "https://interview-key.openai.azure.com/"
16
+ api_version = "2024-05-01-preview"
17
+ deployment_id = "interview"
18
+
19
+ # Initialize Azure OpenAI client
20
+ client = AzureOpenAI(
21
+ api_version=api_version,
22
+ azure_endpoint=endpoint_url,
23
+ api_key=key
24
+ )
25
 
26
  # Streamlit app layout
27
  st.set_page_config(layout="wide")
 
54
  "Provide the extracted terms in JSON format."
55
  )
56
 
57
+ retries = 2
58
+ wait_time = 1
59
+ for i in range(retries):
60
+ try:
61
+ response = client.chat.completions.create(
62
+ model=deployment_id,
63
  messages=[
64
  {"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
65
  {"role": "user", "content": prompt},
 
69
  stop=None,
70
  temperature=0.1,
71
  )
72
+ return response.choices[0].message.content
73
+ except Exception as e:
74
+ st.error(f"Error extracting terms from contract: {e}")
75
+ return None
76
+ # except openai.error.RateLimitError:
77
+ # if i < retries - 1:
78
+ # st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
79
+ # time.sleep(wait_time)
80
+ # wait_time *= 2 # Exponential backoff
81
+ # else:
82
+ # st.error("Rate limit exceeded. Please try again later.")
83
+ # return None
84
+
85
+ def analyze_task_compliance(task_description, cost_estimate, contract_terms):
86
  prompt = (
87
  "You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
88
  "Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
89
  "Your job is to analyze the task description and specify if it violates any conditions from the contract. "
90
  "If there are violations, list the reasons for each violation.\n\n"
91
+ f"Contract terms:\n{json.dumps(contract_terms, indent=4)}\n\n"
92
  f"Task description:\n{task_description}\n"
93
  f"Cost estimate:\n{cost_estimate}\n\n"
94
  "Provide the compliance analysis in a clear JSON format."
95
  )
96
 
97
+ retries = 5
98
+ wait_time = 1
99
+ for i in range(retries):
100
+ try:
101
+ response = client.chat.completions.create(
102
+ model=deployment_id,
103
  messages=[
104
  {"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
105
  {"role": "user", "content": prompt},
 
108
  n=1,
109
  stop=None,
110
  temperature=0.1,
111
+ stream=True,
112
  )
113
 
114
+ compliance_analysis = ""
115
+ for chunk in response:
116
+ chunk_text = chunk['choices'][0]['delta'].get('content', '')
117
+ compliance_analysis += chunk_text
118
+ st.write(chunk_text)
119
+ st.json(chunk_text)
120
+
121
+ return json.loads(compliance_analysis)
122
+
123
+ except Exception as e:
124
+ st.error(f"Error analyzing task compliance: {e}")
125
+ return None
126
+ # response = openai.ChatCompletion.create(
127
+ # model="gpt-4",
128
+ # messages=[
129
+ # {"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
130
+ # {"role": "user", "content": prompt},
131
+ # ],
132
+ # max_tokens=4096,
133
+ # n=1,
134
+ # stop=None,
135
+ # temperature=0.1,
136
+ # stream=True,
137
+ # )
138
+
139
+ # compliance_analysis = ""
140
+ # for chunk in response:
141
+ # chunk_text = chunk['choices'][0]['delta'].get('content', '')
142
+ # compliance_analysis += chunk_text
143
+ # st.write(chunk_text)
144
+ # st.json(chunk_text)
145
+
146
+ # return json.loads(compliance_analysis)
147
+ # except openai.error.RateLimitError:
148
+ # if i < retries - 1:
149
+ # st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
150
+ # time.sleep(wait_time)
151
+ # wait_time *= 2 # Exponential backoff
152
+ # else:
153
+ # st.error("Rate limit exceeded. Please try again later.")
154
+ # return None
155
 
156
  def main():
 
157
  st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
158
 
159
  # File upload buttons one after another
 
171
  # Extract contract text and terms
172
  contract_text = extract_text_from_docx(docx_file)
173
  extracted_terms_json = extract_terms_from_contract(contract_text)
174
+
175
  if extracted_terms_json is None:
176
  return
177
 
 
180
  except json.JSONDecodeError as e:
181
  st.error(f"JSON decoding error: {e}")
182
  return
183
+
184
  # Read task descriptions and cost estimates from XLSX or CSV
185
  if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
186
  tasks_df = pd.read_excel(data_file)
 
188
  tasks_df = pd.read_csv(data_file)
189
 
190
  compliance_results = []
191
+ futures = []
192
+
193
+ # Use ThreadPoolExecutor to analyze tasks concurrently
194
+ with ThreadPoolExecutor(max_workers=10) as executor: # Adjust max_workers as needed
195
+ for _, row in tasks_df.iterrows():
196
+ task_description = row['Task Description']
197
+ cost_estimate = row['Amount']
198
+ futures.append(executor.submit(analyze_task_compliance, task_description, cost_estimate, contract_terms))
199
 
200
+ for future in as_completed(futures):
201
+ try:
202
+ result = future.result()
203
+ if result is not None:
204
+ compliance_results.append(result)
205
+ except Exception as e:
206
+ st.error(f"An error occurred: {e}")
207
+
208
  col1, col2 = st.columns(2)
209
 
210
  with col1:
 
231
  file_name="compliance_results.json",
232
  mime="application/json"
233
  )
 
 
234
 
235
  if __name__ == "__main__":
236
  main()