Bikas0 commited on
Commit
9a8fa46
·
verified ·
1 Parent(s): e9177e0
Files changed (1) hide show
  1. app.py +158 -158
app.py CHANGED
@@ -12,167 +12,167 @@ load_dotenv()
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()
 
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()