Bikas0 commited on
Commit
281fa65
1 Parent(s): c5435a0

complete code

Browse files
Files changed (1) hide show
  1. app.py +47 -106
app.py CHANGED
@@ -5,7 +5,6 @@ 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()
@@ -54,133 +53,81 @@ def extract_terms_from_contract(contract_text):
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},
66
  ],
67
- max_tokens=4096,
68
  n=1,
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},
106
  ],
107
- max_tokens=4096,
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
160
- st.sidebar.file_uploader("Upload Contract Document (DOCX)", type="docx", key="docx_file")
161
- st.sidebar.file_uploader("Upload Task Descriptions (XLSX or CSV)", type=["xlsx", "csv"], key="data_file")
162
  submit_button = st.sidebar.button("Submit")
163
 
164
- docx_file = st.session_state.get("docx_file")
165
- data_file = st.session_state.get("data_file")
166
-
167
  if submit_button and docx_file and data_file:
168
- # Clear previous information
169
- st.session_state.clear()
170
-
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
 
178
  try:
179
- contract_terms = json.loads(extracted_terms_json)
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,46 +135,40 @@ def main():
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:
 
211
  st.write("Extracted Contract Terms:")
212
- st.json(contract_terms)
213
 
214
  # Download button for contract terms
215
  st.download_button(
216
  label="Download Contract Terms",
217
- data=json.dumps(contract_terms, indent=4),
218
  file_name="contract_terms.json",
219
  mime="application/json"
220
  )
221
 
222
- with col2:
 
223
  st.write("Compliance Results:")
224
- st.json(compliance_results)
225
 
226
  # Download button for compliance results
227
- compliance_results_json = json.dumps(compliance_results, indent=4)
228
  st.download_button(
229
  label="Download Compliance Results",
230
- data=compliance_results_json,
231
  file_name="compliance_results.json",
232
  mime="application/json"
233
  )
 
5
  from docx import Document
6
  from dotenv import load_dotenv
7
  from openai import AzureOpenAI
 
8
 
9
  # Load environment variables
10
  load_dotenv()
 
53
  "Provide the extracted terms in JSON format."
54
  )
55
 
56
+ try:
57
+ response = client.chat.completions.create(
 
 
 
58
  model=deployment_id,
59
  messages=[
60
  {"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
61
  {"role": "user", "content": prompt},
62
  ],
63
+ max_tokens=1250,
64
  n=1,
65
  stop=None,
66
  temperature=0.1,
67
+ )
68
+ return response.choices[0].message.content
69
+ except Exception as e:
70
+ st.error(f"Error extracting terms from contract: {e}")
71
+ return None
 
 
 
 
 
 
 
 
72
 
73
  def analyze_task_compliance(task_description, cost_estimate, contract_terms):
74
+ print("Task D: ", task_description, cost_estimate)
75
  prompt = (
76
  "You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
77
  "Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
78
+ "Your job is to analyze each task description and specify if it violates any conditions from the contract. "
79
+ "If there are violations, list the reasons for each violation. Provide detailed answers and do not give only true or false answers.\n\n"
80
  f"Contract terms:\n{json.dumps(contract_terms, indent=4)}\n\n"
81
  f"Task description:\n{task_description}\n"
82
  f"Cost estimate:\n{cost_estimate}\n\n"
83
  "Provide the compliance analysis in a clear JSON format."
84
  )
85
 
86
+ try:
87
+ response = client.chat.completions.create(
 
 
 
88
  model=deployment_id,
89
  messages=[
90
  {"role": "system", "content": "You are an AI specialized in analyzing text for compliance with specified conditions."},
91
  {"role": "user", "content": prompt},
92
  ],
93
+ max_tokens=1250,
94
  n=1,
95
  stop=None,
96
  temperature=0.1,
 
97
  )
98
+ return json.loads(response.choices[0].message.content)
99
+ except Exception as e:
100
+ st.error(f"Error analyzing task compliance: {e}")
101
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  def main():
104
  st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
105
 
106
+ # Initialize session state
107
+ if 'contract_terms' not in st.session_state:
108
+ st.session_state.contract_terms = None
109
+ if 'compliance_results' not in st.session_state:
110
+ st.session_state.compliance_results = None
111
+
112
  # File upload buttons one after another
113
+ docx_file = st.sidebar.file_uploader("Upload Contract Document (DOCX)", type="docx", key="docx_file")
114
+ data_file = st.sidebar.file_uploader("Upload Task Descriptions (XLSX or CSV)", type=["xlsx", "csv"], key="data_file")
115
  submit_button = st.sidebar.button("Submit")
116
 
 
 
 
117
  if submit_button and docx_file and data_file:
 
 
 
118
  # Extract contract text and terms
119
  contract_text = extract_text_from_docx(docx_file)
120
  extracted_terms_json = extract_terms_from_contract(contract_text)
121
+
122
  if extracted_terms_json is None:
123
  return
124
 
125
  try:
126
+ st.session_state.contract_terms = json.loads(extracted_terms_json)
127
  except json.JSONDecodeError as e:
128
  st.error(f"JSON decoding error: {e}")
129
  return
130
+
131
  # Read task descriptions and cost estimates from XLSX or CSV
132
  if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
133
  tasks_df = pd.read_excel(data_file)
 
135
  tasks_df = pd.read_csv(data_file)
136
 
137
  compliance_results = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
+ # Process tasks sequentially
140
+ for _, row in tasks_df.iterrows():
141
+ result = analyze_task_compliance(row['Task Description'], row['Amount'], st.session_state.contract_terms)
142
+ if result is not None:
143
+ print(result)
144
+ compliance_results.append(result)
145
+
146
+ st.session_state.compliance_results = compliance_results
147
+
148
+ col1, col2 = st.columns(2)
149
 
150
+ with col1:
151
+ if st.session_state.contract_terms:
152
  st.write("Extracted Contract Terms:")
153
+ st.json(st.session_state.contract_terms)
154
 
155
  # Download button for contract terms
156
  st.download_button(
157
  label="Download Contract Terms",
158
+ data=json.dumps(st.session_state.contract_terms, indent=4),
159
  file_name="contract_terms.json",
160
  mime="application/json"
161
  )
162
 
163
+ with col2:
164
+ if st.session_state.compliance_results:
165
  st.write("Compliance Results:")
166
+ st.json(st.session_state.compliance_results)
167
 
168
  # Download button for compliance results
 
169
  st.download_button(
170
  label="Download Compliance Results",
171
+ data=json.dumps(st.session_state.compliance_results, indent=4),
172
  file_name="compliance_results.json",
173
  mime="application/json"
174
  )