Bikas0 commited on
Commit
b8ceeb2
·
verified ·
1 Parent(s): 9a8fa46
Files changed (1) hide show
  1. app.py +158 -159
app.py CHANGED
@@ -11,168 +11,167 @@ load_dotenv()
11
 
12
  # Azure OpenAI credentials
13
  key = os.getenv("AZURE_OPENAI_API_KEY")
14
- print(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")
28
-
29
- # # Add custom CSS for center alignment
30
- # st.markdown("""
31
- # <style>
32
- # .centered-title {
33
- # text-align: center;
34
- # font-size: 2.5em;
35
- # margin-top: 0;
36
- # }
37
- # </style>
38
- # """, unsafe_allow_html=True)
39
-
40
- # def extract_text_from_docx(docx_path):
41
- # doc = Document(docx_path)
42
- # return "\n".join([para.text for para in doc.paragraphs])
43
-
44
- # def extract_terms_from_contract(contract_text):
45
- # prompt = (
46
- # "You are an AI tasked with analyzing a contract and extracting key terms and constraints. The contract contains "
47
- # "various sections and subsections with terms related to budget constraints, types of allowable work, timelines, "
48
- # "penalties, responsibilities, and other conditions for work execution. Your job is to extract these key terms and "
49
- # "structure them in a clear JSON format, reflecting the hierarchy of sections and subsections. "
50
- # "Ensure to capture all important constraints and conditions specified in the contract text. If a section or subsection "
51
- # "contains multiple terms, list them all.\n\n"
52
- # "Contract text:\n"
53
- # f"{contract_text}\n\n"
54
- # "Provide the extracted terms in JSON format."
55
- # )
56
-
57
- # try:
58
- # response = client.chat.completions.create(
59
- # model=deployment_id,
60
- # messages=[
61
- # {"role": "system", "content": "You are an AI specialized in extracting structured data from text documents."},
62
- # {"role": "user", "content": prompt},
63
- # ],
64
- # max_tokens=1250,
65
- # n=1,
66
- # stop=None,
67
- # temperature=0.1,
68
- # )
69
- # return response.choices[0].message.content
70
- # except Exception as e:
71
- # st.error(f"Error extracting terms from contract: {e}")
72
- # return None
73
-
74
- # def analyze_task_compliance(task_description, cost_estimate, contract_terms):
75
- # print("Task D: ", task_description, cost_estimate)
76
- # prompt = (
77
- # "You are an AI tasked with analyzing a task description and its associated cost estimate for compliance with contract conditions. "
78
- # "Below are the key terms and constraints extracted from the contract, followed by a task description and its cost estimate. "
79
- # "Your job is to analyze each task description and specify if it violates any conditions from the contract. "
80
- # "If there are violations, list the reasons for each violation. Provide detailed answers and do not give only true or false answers.\n\n"
81
- # f"Contract terms:\n{json.dumps(contract_terms, indent=4)}\n\n"
82
- # f"Task description:\n{task_description}\n"
83
- # f"Cost estimate:\n{cost_estimate}\n\n"
84
- # "Provide the compliance analysis in a clear JSON format."
85
- # )
86
-
87
- # try:
88
- # response = client.chat.completions.create(
89
- # model=deployment_id,
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=1250,
95
- # n=1,
96
- # stop=None,
97
- # temperature=0.1,
98
- # )
99
- # return json.loads(response.choices[0].message.content)
100
- # except Exception as e:
101
- # st.error(f"Error analyzing task compliance: {e}")
102
- # return None
103
-
104
- # def main():
105
- # st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
106
-
107
- # # Initialize session state
108
- # if 'contract_terms' not in st.session_state:
109
- # st.session_state.contract_terms = None
110
- # if 'compliance_results' not in st.session_state:
111
- # st.session_state.compliance_results = None
112
-
113
- # # File upload buttons one after another
114
- # docx_file = st.sidebar.file_uploader("Upload Contract Document (DOCX)", type="docx", key="docx_file")
115
- # data_file = st.sidebar.file_uploader("Upload Task Descriptions (XLSX or CSV)", type=["xlsx", "csv"], key="data_file")
116
- # submit_button = st.sidebar.button("Submit")
117
-
118
- # if submit_button and docx_file and data_file:
119
- # # Extract contract text and terms
120
- # contract_text = extract_text_from_docx(docx_file)
121
- # extracted_terms_json = extract_terms_from_contract(contract_text)
122
-
123
- # if extracted_terms_json is None:
124
- # return
125
 
126
- # try:
127
- # st.session_state.contract_terms = json.loads(extracted_terms_json)
128
- # except json.JSONDecodeError as e:
129
- # st.error(f"JSON decoding error: {e}")
130
- # return
131
 
132
- # # Read task descriptions and cost estimates from XLSX or CSV
133
- # if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
134
- # tasks_df = pd.read_excel(data_file)
135
- # else:
136
- # tasks_df = pd.read_csv(data_file)
137
-
138
- # compliance_results = []
139
-
140
- # # Process tasks sequentially
141
- # for _, row in tasks_df.iterrows():
142
- # result = analyze_task_compliance(row['Task Description'], row['Amount'], st.session_state.contract_terms)
143
- # if result is not None:
144
- # print(result)
145
- # compliance_results.append(result)
146
 
147
- # st.session_state.compliance_results = compliance_results
148
 
149
- # col1, col2 = st.columns(2)
150
 
151
- # with col1:
152
- # if st.session_state.contract_terms:
153
- # st.write("Extracted Contract Terms:")
154
- # st.json(st.session_state.contract_terms)
155
 
156
- # # Download button for contract terms
157
- # st.download_button(
158
- # label="Download Contract Terms",
159
- # data=json.dumps(st.session_state.contract_terms, indent=4),
160
- # file_name="contract_terms.json",
161
- # mime="application/json"
162
- # )
163
-
164
- # with col2:
165
- # if st.session_state.compliance_results:
166
- # st.write("Compliance Results:")
167
- # st.json(st.session_state.compliance_results)
168
-
169
- # # Download button for compliance results
170
- # st.download_button(
171
- # label="Download Compliance Results",
172
- # data=json.dumps(st.session_state.compliance_results, indent=4),
173
- # file_name="compliance_results.json",
174
- # mime="application/json"
175
- # )
176
-
177
- # if __name__ == "__main__":
178
- # main()
 
11
 
12
  # Azure OpenAI credentials
13
  key = os.getenv("AZURE_OPENAI_API_KEY")
14
+ endpoint_url = "https://interview-key.openai.azure.com/"
15
+ api_version = "2024-05-01-preview"
16
+ deployment_id = "interview"
17
+
18
+ # Initialize Azure OpenAI client
19
+ client = AzureOpenAI(
20
+ api_version=api_version,
21
+ azure_endpoint=endpoint_url,
22
+ api_key=key
23
+ )
24
+
25
+ # Streamlit app layout
26
+ st.set_page_config(layout="wide")
27
+
28
+ # Add custom CSS for center alignment
29
+ st.markdown("""
30
+ <style>
31
+ .centered-title {
32
+ text-align: center;
33
+ font-size: 2.5em;
34
+ margin-top: 0;
35
+ }
36
+ </style>
37
+ """, unsafe_allow_html=True)
38
+
39
+ def extract_text_from_docx(docx_path):
40
+ doc = Document(docx_path)
41
+ return "\n".join([para.text for para in doc.paragraphs])
42
+
43
+ def extract_terms_from_contract(contract_text):
44
+ prompt = (
45
+ "You are an AI tasked with analyzing a contract and extracting key terms and constraints. The contract contains "
46
+ "various sections and subsections with terms related to budget constraints, types of allowable work, timelines, "
47
+ "penalties, responsibilities, and other conditions for work execution. Your job is to extract these key terms and "
48
+ "structure them in a clear JSON format, reflecting the hierarchy of sections and subsections. "
49
+ "Ensure to capture all important constraints and conditions specified in the contract text. If a section or subsection "
50
+ "contains multiple terms, list them all.\n\n"
51
+ "Contract text:\n"
52
+ f"{contract_text}\n\n"
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)
134
+ else:
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
+ )
175
+
176
+ if __name__ == "__main__":
177
+ main()