File size: 15,811 Bytes
68bc4aa
 
 
 
 
 
 
 
 
 
 
6e411cb
68bc4aa
6e411cb
68bc4aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bd1dd9
68bc4aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bd1dd9
68bc4aa
 
 
 
 
 
 
 
 
 
 
 
 
4bd1dd9
68bc4aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import streamlit as st
from streamlit_option_menu import option_menu
from openai import AzureOpenAI
import docx2txt
import PyPDF2
import os
from pdfminer.high_level import extract_text
class OpenaiAPI():
  def __init__(self) -> None:

      self.client = AzureOpenAI(
          api_key = os.environ["API"],
          api_version="2023-07-01-preview",
          azure_endpoint=os.environ["URL"],
      )

  def get_response(self,prompt,) -> str:
      try:

          completion = self.client.chat.completions.create(
              model="GPT-4o",  # e.g. gpt-35-instant
              messages=prompt,
              temperature=0,)
          return completion.choices[0].message.content

      except Exception as e:
          print("An error occurred while generate prompt from openai api: %s", e)

  def docx_to_text(self,docx_path):
      text = docx2txt.process(docx_path)
      return text

  def pdf_to_text_pypdf2(self,pdf_file):
      text = extract_text(pdf_file)
      return text

st.markdown("""
      <style>
      h1#contract-ai {
      text-align: center;
      }
      header.st-emotion-cache-12fmjuu.ezrtsby2{
        background-color: rgb(234 237 240);
        color: rgb(0, 0, 0);
      }
      .st-emotion-cache-1mi2ry5{
        background:url('https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftheindustryspread.com%2Fwp-content%2Fuploads%2F2019%2F05%2FBroadridge-1.png') no-repeat;
        background-size: 250px 50px;
        background-position: center;
      }
      </style>
  """, unsafe_allow_html=True)
with st.sidebar:
    selected = option_menu("CONTRACT AI", ['Home','Tags','Clauses',
                                           'Summarizer','Headings','Extract Date',
                                           'Pdf to Json','Key Values','Incorrent Sentences',
                                           'Incompleted Sentences','Agressive Content',
                                           'Compare Contract','Find Contract',"Contract Generator"
                                           ],
                           icons=['arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
                                                    'arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
                                                    'arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
                                                    'arrow-right-circle-fill','arrow-right-circle-fill','arrow-right-circle-fill',
                                                    'arrow-right-circle-fill','arrow-right-circle-fill'],
                           menu_icon="house-gear-fill",
                           default_index=0)
    uploaded_file=st.file_uploader("Upload a Docs")
openai = OpenaiAPI()
if selected == 'Home':
  st.title('Contract AI')
  # Dictionary containing the topics and their descriptions
  topics = {
    "None": "Default option with no specific action.",
    "Tags": "Extract tags or keywords from the document.",
    "Clauses": "Identify and extract specific clauses from contracts.",
    "Summarizer": "Generate a concise summary of the document.",
    "Headings": "Extract and display headings from the document.",
    "Extract Date": "Find and extract dates from the document.",
    "Pdf to Json": "Convert PDF documents to JSON format.",
    "Key Values": "Extract key-value pairs from the document.",
    "Incorrect Sentences": "Identify and highlight incorrect sentences.",
    "Incomplete Sentences": "Detect and list incomplete sentences.",
    "Aggressive Content": "Identify and flag aggressive or inappropriate content.",
    "Compare Contract": "Compare two contracts to find differences.",
    "Find Contract": "Search and locate specific contracts.",
    "Contract Generator": "Generate a contract based on provided inputs."
  }

  # Custom CSS for the gray background
  st.markdown("""
      <style>
      h1#contract-ai {
      text-align: center;
      }
      .topic-box {
          background-color: #f0f0f0;
          padding: 10px;
          border-radius: 5px;
          margin-bottom: 10px;
      }
      .topic-box:hover{
        background-color: #000080;
        box-shadow: 6px 1px 12px gray;
        color:#fff;
      }
      .topic-title {
          font-weight: bold;
      }
      .st-emotion-cache-ocqkz7 {
        gap: 1.5rem;
        }
      </style>
  """, unsafe_allow_html=True)

  # Split topics into groups of three for a 3-column layout
  topic_items = list(topics.items())
  for i in range(0, len(topic_items), 3):
      cols = st.columns(3)
      for col, (title, description) in zip(cols, topic_items[i:i+3]):
          col.markdown(f"""
          <div class="topic-box">
              <div class="topic-title">{title}</div>
              <div>{description}</div>
          </div>
          """, unsafe_allow_html=True)
elif selected == 'Contract Generator':
  st.markdown(
      """
  <style>
      h1,#contract-generator,#extract-date,#pdf-to-json,#key-values {
        text-align: center;
      }
  </style>
  """,
      unsafe_allow_html=True,)
  st.title(selected)
  contract_info=st.text_input("Enter Contract info")
  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.
                  the contract type given by user.
                  generate a contract :
                  """},
                  {"role": "user", "content": f"```content: {contract_info}```"}]
  get_response = openai.get_response(conversation)
  st.write(get_response)

elif selected == 'Extract Date':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful assistant.
                  Your task is Identify Dates and Durations Mentioned in the contract and extract that date and duration in key-value pair.
                  format:
                  date:
                  -extracted date
                  -
                  Durations:
                  -extracted Durations
                  -
                  - """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
      st.write('Upload File')
elif selected == 'Pdf to Json':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful assistant.
                  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."""},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')
elif selected == 'Key Values':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful Keywords Extracter..
                  analyze the given contract and Extract Keywords for following contract in triple backticks. tags should be bullet points.contract :
                  """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Tags':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful Tags Extracter.
                  analyze the given contract to extract tags for following contract in triple backticks.
                  tags should be bullet points.contract :
                  """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Clauses':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful Cluases and SubCluases Extracter From Given Content
                  Extract clauses and sub-clauses from the provided contract PDF
                  """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Headings':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful document assistant.
                  Extract Headings from given paragraph do not generate jsu extract the headings from paragraph.
                  """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Incorrent Sentences':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful Error sentence finder.
                  list out the grammatical error sentence in the given text:
                  """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Incompleted Sentences':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                      You are a helpful incomplete sentences finder.
                      list out the incomplete sentences in the following text:
                      """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Agressive Content':
  st.title(selected)
  if uploaded_file is not None:
    print('File Name : ',uploaded_file.name)
    ftype=uploaded_file.name.split('.')
    if ftype[-1]=='pdf':
      docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
    elif ftype[-1]=='docx':
      docs_data = openai.docx_to_text(uploaded_file)
    conversation = [{"role": "system", "content": """
                  You are a helpful Keywords Extracter..
                  analyze the given contract and Extract Keywords for following contract in triple backticks. tags should be bullet points.contract :
                  """},
                  {"role": "user", "content": f"```contract: {docs_data}```"}]
    get_response = openai.get_response(conversation)
    st.write(get_response)
  else:
    st.write('Upload File')

elif selected == 'Compare Contract':
    st.title(selected)
    uploaded_file2 = st.file_uploader("Upload a Second Contract for Comparison")
    if uploaded_file is not None and uploaded_file2 is not None:
        print('File Name : ', uploaded_file.name)
        print('File Name : ', uploaded_file2.name)
        ftype1 = uploaded_file.name.split('.')
        ftype2 = uploaded_file2.name.split('.')
        if ftype1[-1] == 'pdf' and ftype2[-1] == 'pdf':
            docs_data1 = openai.pdf_to_text_pypdf2(uploaded_file)
            docs_data2 = openai.pdf_to_text_pypdf2(uploaded_file2)
        elif ftype1[-1] == 'docx' and ftype2[-1] == 'docx':
            docs_data1 = openai.docx_to_text(uploaded_file)
            docs_data2 = openai.docx_to_text(uploaded_file2)
        conversation = [{"role": "system", "content": """
                        You are a helpful contract comparison assistant.
                        Compare the following two contracts and highlight any differences or similarities.
                        """},
                        {"role": "user", "content": f"```contract 1: {docs_data1}``` ```contract 2: {docs_data2}```"}]
        get_response = openai.get_response(conversation)
        st.write(get_response)
    else:
        st.write('Upload Both Files')

elif selected == 'Find Contract':
    st.title(selected)
    contract_search = st.text_input("Enter Contract Information to Search")
    if contract_search:
        conversation = [{"role": "system", "content": """
                        You are a helpful contract finder.
                        Search and locate the specific contract based on the following information:
                        """},
                        {"role": "user", "content": f"```search: {contract_search}```"}]
        get_response = openai.get_response(conversation)
        st.write(get_response)
    else:
        st.write('Enter Information to Search')

elif selected == 'Summarizer':
    st.title(selected)
    if uploaded_file is not None:
        print('File Name : ', uploaded_file.name)
        ftype = uploaded_file.name.split('.')
        if ftype[-1] == 'pdf':
            docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
        elif ftype[-1] == 'docx':
            docs_data = openai.docx_to_text(uploaded_file)
        conversation = [{"role": "system", "content": """
                        You are a helpful summarizer.
                        Write a concise summary of the following contract:
                        """},
                        {"role": "user", "content": f"```contract: {docs_data}```"}]
        get_response = openai.get_response(conversation)
        st.write(get_response)
    else:
        st.write('Upload File')