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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
%%writefile app.py
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_option_menu import option_menu
|
4 |
+
from openai import AzureOpenAI
|
5 |
+
import docx2txt
|
6 |
+
import PyPDF2
|
7 |
+
import os
|
8 |
+
import pdfplumber
|
9 |
+
from pdfminer.high_level import extract_text
|
10 |
+
class OpenaiAPI():
|
11 |
+
def __init__(self) -> None:
|
12 |
+
|
13 |
+
self.client = AzureOpenAI(
|
14 |
+
api_key = "API",
|
15 |
+
api_version="2023-07-01-preview",
|
16 |
+
azure_endpoint="URL",
|
17 |
+
)
|
18 |
+
|
19 |
+
def get_response(self,prompt,) -> str:
|
20 |
+
try:
|
21 |
+
|
22 |
+
completion = self.client.chat.completions.create(
|
23 |
+
model="GPT-4o", # e.g. gpt-35-instant
|
24 |
+
messages=prompt,
|
25 |
+
temperature=0,)
|
26 |
+
return completion.choices[0].message.content
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
print("An error occurred while generate prompt from openai api: %s", e)
|
30 |
+
|
31 |
+
def docx_to_text(self,docx_path):
|
32 |
+
text = docx2txt.process(docx_path)
|
33 |
+
return text
|
34 |
+
|
35 |
+
def pdf_to_text_pypdf2(self,pdf_file):
|
36 |
+
text = extract_text(pdf_file)
|
37 |
+
return text
|
38 |
+
|
39 |
+
st.markdown("""
|
40 |
+
<style>
|
41 |
+
h1#contract-ai {
|
42 |
+
text-align: center;
|
43 |
+
}
|
44 |
+
header.st-emotion-cache-12fmjuu.ezrtsby2{
|
45 |
+
background-color: rgb(234 237 240);
|
46 |
+
color: rgb(0, 0, 0);
|
47 |
+
}
|
48 |
+
.st-emotion-cache-1mi2ry5{
|
49 |
+
background:url('https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftheindustryspread.com%2Fwp-content%2Fuploads%2F2019%2F05%2FBroadridge-1.png') no-repeat;
|
50 |
+
background-size: 250px 50px;
|
51 |
+
background-position: center;
|
52 |
+
}
|
53 |
+
</style>
|
54 |
+
""", unsafe_allow_html=True)
|
55 |
+
with st.sidebar:
|
56 |
+
selected = option_menu("CONTRACT AI", ['Home','Tags','Clauses',
|
57 |
+
'Summarizer','Headings','Extract Date',
|
58 |
+
'Pdf to Json','Key Values','Incorrent Sentences',
|
59 |
+
'Incompleted Sentences','Agressive Content',
|
60 |
+
'Compare Contract','Find Contract',"Contract Generator"
|
61 |
+
],
|
62 |
+
icons=['arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
|
63 |
+
'arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
|
64 |
+
'arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
|
65 |
+
'arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
|
66 |
+
'arrow-right-circle-fill','arrow-right-circle-fill'],
|
67 |
+
menu_icon="house-gear-fill",
|
68 |
+
default_index=0)
|
69 |
+
uploaded_file=st.file_uploader("Upload a Docs")
|
70 |
+
openai = OpenaiAPI()
|
71 |
+
if selected == 'Home':
|
72 |
+
st.title('Contract AI')
|
73 |
+
# Dictionary containing the topics and their descriptions
|
74 |
+
topics = {
|
75 |
+
"None": "Default option with no specific action.",
|
76 |
+
"Tags": "Extract tags or keywords from the document.",
|
77 |
+
"Clauses": "Identify and extract specific clauses from contracts.",
|
78 |
+
"Summarizer": "Generate a concise summary of the document.",
|
79 |
+
"Headings": "Extract and display headings from the document.",
|
80 |
+
"Extract Date": "Find and extract dates from the document.",
|
81 |
+
"Pdf to Json": "Convert PDF documents to JSON format.",
|
82 |
+
"Key Values": "Extract key-value pairs from the document.",
|
83 |
+
"Incorrect Sentences": "Identify and highlight incorrect sentences.",
|
84 |
+
"Incomplete Sentences": "Detect and list incomplete sentences.",
|
85 |
+
"Aggressive Content": "Identify and flag aggressive or inappropriate content.",
|
86 |
+
"Compare Contract": "Compare two contracts to find differences.",
|
87 |
+
"Find Contract": "Search and locate specific contracts.",
|
88 |
+
"Contract Generator": "Generate a contract based on provided inputs."
|
89 |
+
}
|
90 |
+
|
91 |
+
# Custom CSS for the gray background
|
92 |
+
st.markdown("""
|
93 |
+
<style>
|
94 |
+
h1#contract-ai {
|
95 |
+
text-align: center;
|
96 |
+
}
|
97 |
+
.topic-box {
|
98 |
+
background-color: #f0f0f0;
|
99 |
+
padding: 10px;
|
100 |
+
border-radius: 5px;
|
101 |
+
margin-bottom: 10px;
|
102 |
+
}
|
103 |
+
.topic-box:hover{
|
104 |
+
background-color: #000080;
|
105 |
+
box-shadow: 6px 1px 12px gray;
|
106 |
+
color:#fff;
|
107 |
+
}
|
108 |
+
.topic-title {
|
109 |
+
font-weight: bold;
|
110 |
+
}
|
111 |
+
.st-emotion-cache-ocqkz7 {
|
112 |
+
gap: 1.5rem;
|
113 |
+
}
|
114 |
+
</style>
|
115 |
+
""", unsafe_allow_html=True)
|
116 |
+
|
117 |
+
# Split topics into groups of three for a 3-column layout
|
118 |
+
topic_items = list(topics.items())
|
119 |
+
for i in range(0, len(topic_items), 3):
|
120 |
+
cols = st.columns(3)
|
121 |
+
for col, (title, description) in zip(cols, topic_items[i:i+3]):
|
122 |
+
col.markdown(f"""
|
123 |
+
<div class="topic-box">
|
124 |
+
<div class="topic-title">{title}</div>
|
125 |
+
<div>{description}</div>
|
126 |
+
</div>
|
127 |
+
""", unsafe_allow_html=True)
|
128 |
+
elif selected == 'Contract Generator':
|
129 |
+
st.markdown(
|
130 |
+
"""
|
131 |
+
<style>
|
132 |
+
h1,#contract-generator,#extract-date,#pdf-to-json,#key-values {
|
133 |
+
text-align: center;
|
134 |
+
}
|
135 |
+
</style>
|
136 |
+
""",
|
137 |
+
unsafe_allow_html=True,)
|
138 |
+
st.title(selected)
|
139 |
+
contract_info=st.text_input("Enter Contract info")
|
140 |
+
conversation = [{"role": "system", "content": """You are a helpful assistant. Your task is creating a complete contract with important terms and condiations based on the contract information and type.
|
141 |
+
the contract type given by user.
|
142 |
+
generate a contract :
|
143 |
+
"""},
|
144 |
+
{"role": "user", "content": f"```content: {contract_info}```"}]
|
145 |
+
get_response = openai.get_response(conversation)
|
146 |
+
st.write(get_response)
|
147 |
+
|
148 |
+
elif selected == 'Extract Date':
|
149 |
+
st.title(selected)
|
150 |
+
if uploaded_file is not None:
|
151 |
+
print('File Name : ',uploaded_file.name)
|
152 |
+
ftype=uploaded_file.name.split('.')
|
153 |
+
if ftype[-1]=='pdf':
|
154 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
155 |
+
elif ftype[-1]=='docx':
|
156 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
157 |
+
conversation = [{"role": "system", "content": """
|
158 |
+
You are a helpful assistant.
|
159 |
+
Your task is Identify Dates and Durations Mentioned in the contract and extract that date and duration in key-value pair.
|
160 |
+
format:
|
161 |
+
date:
|
162 |
+
-extracted date
|
163 |
+
-
|
164 |
+
Durations:
|
165 |
+
-extracted Durations
|
166 |
+
-
|
167 |
+
- """},
|
168 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
169 |
+
get_response = openai.get_response(conversation)
|
170 |
+
st.write(get_response)
|
171 |
+
else:
|
172 |
+
st.write('Upload File')
|
173 |
+
elif selected == 'Pdf to Json':
|
174 |
+
st.title(selected)
|
175 |
+
if uploaded_file is not None:
|
176 |
+
print('File Name : ',uploaded_file.name)
|
177 |
+
ftype=uploaded_file.name.split('.')
|
178 |
+
if ftype[-1]=='pdf':
|
179 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
180 |
+
elif ftype[-1]=='docx':
|
181 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
182 |
+
conversation = [{"role": "system", "content": """
|
183 |
+
You are a helpful assistant.
|
184 |
+
Your task is Get the text and analyse and split it into Topics and Content in json format.Give Proper Name to Topic dont give any Numbers and Dont Give any empty Contents.The Output Format Should Be very good."""},
|
185 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
186 |
+
get_response = openai.get_response(conversation)
|
187 |
+
st.write(get_response)
|
188 |
+
else:
|
189 |
+
st.write('Upload File')
|
190 |
+
elif selected == 'Key Values':
|
191 |
+
st.title(selected)
|
192 |
+
if uploaded_file is not None:
|
193 |
+
print('File Name : ',uploaded_file.name)
|
194 |
+
ftype=uploaded_file.name.split('.')
|
195 |
+
if ftype[-1]=='pdf':
|
196 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
197 |
+
elif ftype[-1]=='docx':
|
198 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
199 |
+
conversation = [{"role": "system", "content": """
|
200 |
+
You are a helpful Keywords Extracter..
|
201 |
+
analyze the given contract and Extract Keywords for following contract in triple backticks. tags should be bullet points.contract :
|
202 |
+
"""},
|
203 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
204 |
+
get_response = openai.get_response(conversation)
|
205 |
+
st.write(get_response)
|
206 |
+
else:
|
207 |
+
st.write('Upload File')
|
208 |
+
|
209 |
+
elif selected == 'Tags':
|
210 |
+
st.title(selected)
|
211 |
+
if uploaded_file is not None:
|
212 |
+
print('File Name : ',uploaded_file.name)
|
213 |
+
ftype=uploaded_file.name.split('.')
|
214 |
+
if ftype[-1]=='pdf':
|
215 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
216 |
+
elif ftype[-1]=='docx':
|
217 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
218 |
+
conversation = [{"role": "system", "content": """
|
219 |
+
You are a helpful Tags Extracter.
|
220 |
+
analyze the given contract to extract tags for following contract in triple backticks.
|
221 |
+
tags should be bullet points.contract :
|
222 |
+
"""},
|
223 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
224 |
+
get_response = openai.get_response(conversation)
|
225 |
+
st.write(get_response)
|
226 |
+
else:
|
227 |
+
st.write('Upload File')
|
228 |
+
|
229 |
+
elif selected == 'Clauses':
|
230 |
+
st.title(selected)
|
231 |
+
if uploaded_file is not None:
|
232 |
+
print('File Name : ',uploaded_file.name)
|
233 |
+
ftype=uploaded_file.name.split('.')
|
234 |
+
if ftype[-1]=='pdf':
|
235 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
236 |
+
elif ftype[-1]=='docx':
|
237 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
238 |
+
conversation = [{"role": "system", "content": """
|
239 |
+
You are a helpful Cluases and SubCluases Extracter From Given Content
|
240 |
+
Extract clauses and sub-clauses from the provided contract PDF
|
241 |
+
"""},
|
242 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
243 |
+
get_response = openai.get_response(conversation)
|
244 |
+
st.write(get_response)
|
245 |
+
else:
|
246 |
+
st.write('Upload File')
|
247 |
+
|
248 |
+
elif selected == 'Headings':
|
249 |
+
st.title(selected)
|
250 |
+
if uploaded_file is not None:
|
251 |
+
print('File Name : ',uploaded_file.name)
|
252 |
+
ftype=uploaded_file.name.split('.')
|
253 |
+
if ftype[-1]=='pdf':
|
254 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
255 |
+
elif ftype[-1]=='docx':
|
256 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
257 |
+
conversation = [{"role": "system", "content": """
|
258 |
+
You are a helpful document assistant.
|
259 |
+
Extract Headings from given paragraph do not generate jsu extract the headings from paragraph.
|
260 |
+
"""},
|
261 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
262 |
+
get_response = openai.get_response(conversation)
|
263 |
+
st.write(get_response)
|
264 |
+
else:
|
265 |
+
st.write('Upload File')
|
266 |
+
|
267 |
+
elif selected == 'Incorrent Sentences':
|
268 |
+
st.title(selected)
|
269 |
+
if uploaded_file is not None:
|
270 |
+
print('File Name : ',uploaded_file.name)
|
271 |
+
ftype=uploaded_file.name.split('.')
|
272 |
+
if ftype[-1]=='pdf':
|
273 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
274 |
+
elif ftype[-1]=='docx':
|
275 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
276 |
+
conversation = [{"role": "system", "content": """
|
277 |
+
You are a helpful Error sentence finder.
|
278 |
+
list out the grammatical error sentence in the given text:
|
279 |
+
"""},
|
280 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
281 |
+
get_response = openai.get_response(conversation)
|
282 |
+
st.write(get_response)
|
283 |
+
else:
|
284 |
+
st.write('Upload File')
|
285 |
+
|
286 |
+
elif selected == 'Incompleted Sentences':
|
287 |
+
st.title(selected)
|
288 |
+
if uploaded_file is not None:
|
289 |
+
print('File Name : ',uploaded_file.name)
|
290 |
+
ftype=uploaded_file.name.split('.')
|
291 |
+
if ftype[-1]=='pdf':
|
292 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
293 |
+
elif ftype[-1]=='docx':
|
294 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
295 |
+
conversation = [{"role": "system", "content": """
|
296 |
+
You are a helpful incomplete sentences finder.
|
297 |
+
list out the incomplete sentences in the following text:
|
298 |
+
"""},
|
299 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
300 |
+
get_response = openai.get_response(conversation)
|
301 |
+
st.write(get_response)
|
302 |
+
else:
|
303 |
+
st.write('Upload File')
|
304 |
+
|
305 |
+
elif selected == 'Agressive Content':
|
306 |
+
st.title(selected)
|
307 |
+
if uploaded_file is not None:
|
308 |
+
print('File Name : ',uploaded_file.name)
|
309 |
+
ftype=uploaded_file.name.split('.')
|
310 |
+
if ftype[-1]=='pdf':
|
311 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
312 |
+
elif ftype[-1]=='docx':
|
313 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
314 |
+
conversation = [{"role": "system", "content": """
|
315 |
+
You are a helpful Keywords Extracter..
|
316 |
+
analyze the given contract and Extract Keywords for following contract in triple backticks. tags should be bullet points.contract :
|
317 |
+
"""},
|
318 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
319 |
+
get_response = openai.get_response(conversation)
|
320 |
+
st.write(get_response)
|
321 |
+
else:
|
322 |
+
st.write('Upload File')
|
323 |
+
|
324 |
+
elif selected == 'Compare Contract':
|
325 |
+
st.title(selected)
|
326 |
+
uploaded_file2 = st.file_uploader("Upload a Second Contract for Comparison")
|
327 |
+
if uploaded_file is not None and uploaded_file2 is not None:
|
328 |
+
print('File Name : ', uploaded_file.name)
|
329 |
+
print('File Name : ', uploaded_file2.name)
|
330 |
+
ftype1 = uploaded_file.name.split('.')
|
331 |
+
ftype2 = uploaded_file2.name.split('.')
|
332 |
+
if ftype1[-1] == 'pdf' and ftype2[-1] == 'pdf':
|
333 |
+
docs_data1 = openai.pdf_to_text_pypdf2(uploaded_file)
|
334 |
+
docs_data2 = openai.pdf_to_text_pypdf2(uploaded_file2)
|
335 |
+
elif ftype1[-1] == 'docx' and ftype2[-1] == 'docx':
|
336 |
+
docs_data1 = openai.docx_to_text(uploaded_file)
|
337 |
+
docs_data2 = openai.docx_to_text(uploaded_file2)
|
338 |
+
conversation = [{"role": "system", "content": """
|
339 |
+
You are a helpful contract comparison assistant.
|
340 |
+
Compare the following two contracts and highlight any differences or similarities.
|
341 |
+
"""},
|
342 |
+
{"role": "user", "content": f"```contract 1: {docs_data1}``` ```contract 2: {docs_data2}```"}]
|
343 |
+
get_response = openai.get_response(conversation)
|
344 |
+
st.write(get_response)
|
345 |
+
else:
|
346 |
+
st.write('Upload Both Files')
|
347 |
+
|
348 |
+
elif selected == 'Find Contract':
|
349 |
+
st.title(selected)
|
350 |
+
contract_search = st.text_input("Enter Contract Information to Search")
|
351 |
+
if contract_search:
|
352 |
+
conversation = [{"role": "system", "content": """
|
353 |
+
You are a helpful contract finder.
|
354 |
+
Search and locate the specific contract based on the following information:
|
355 |
+
"""},
|
356 |
+
{"role": "user", "content": f"```search: {contract_search}```"}]
|
357 |
+
get_response = openai.get_response(conversation)
|
358 |
+
st.write(get_response)
|
359 |
+
else:
|
360 |
+
st.write('Enter Information to Search')
|
361 |
+
|
362 |
+
elif selected == 'Summarizer':
|
363 |
+
st.title(selected)
|
364 |
+
if uploaded_file is not None:
|
365 |
+
print('File Name : ', uploaded_file.name)
|
366 |
+
ftype = uploaded_file.name.split('.')
|
367 |
+
if ftype[-1] == 'pdf':
|
368 |
+
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
|
369 |
+
elif ftype[-1] == 'docx':
|
370 |
+
docs_data = openai.docx_to_text(uploaded_file)
|
371 |
+
conversation = [{"role": "system", "content": """
|
372 |
+
You are a helpful summarizer.
|
373 |
+
Write a concise summary of the following contract:
|
374 |
+
"""},
|
375 |
+
{"role": "user", "content": f"```contract: {docs_data}```"}]
|
376 |
+
get_response = openai.get_response(conversation)
|
377 |
+
st.write(get_response)
|
378 |
+
else:
|
379 |
+
st.write('Upload File')
|
380 |
+
|