dwipper commited on
Commit
47f650d
·
1 Parent(s): 80324a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -40
app.py CHANGED
@@ -19,7 +19,7 @@ airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
19
  prompts_table_name = 'tblYIZEB8m6JkGDEP'
20
  users_table_name = 'tblLNe5ZL47SvrAEk'
21
  user_log_table_name = 'tblrlTsRrkl6BqMAJ'
22
- scoutlog_table_name = 'tblF30p0krA1FwOiD'
23
  policies_table_name = 'tbla6PC65qZfqdJhE'
24
 
25
 
@@ -33,7 +33,7 @@ response_label = f'<span style="display: inline-block; position: relative; z-ind
33
  base_id = 'appcUK3hUWC7GM2Kb'
34
 
35
  # App name for user login logging
36
- app = "Scout"
37
 
38
  headers = {
39
  "Authorization": f"Bearer {airtable_api_key}",
@@ -147,19 +147,13 @@ def get_prompt(header, template_content):
147
  return header, template_content
148
 
149
 
150
- def append_to_at_scountlog(school_selection, output_format, gender, interests, sports, gpt_response, number_of_athletes,
151
- social_media_channel, response_time, question_cost, prompt_tokens, completion_tokens):
152
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{scoutlog_table_name}'
153
 
154
  # Organize data for Airtable
155
  new_fields = {
156
- 'school_selection': str(school_selection),
157
- 'gender': str(gender),
158
- 'social_media_channel': str(social_media_channel),
159
- 'sports': str(sports),
160
- 'interests': str(interests),
161
- 'number_of_athletes': int(number_of_athletes),
162
- 'output_format': str(output_format),
163
  'gpt_response': str(gpt_response),
164
  'response_time': str(response_time),
165
  'question_cost': question_cost,
@@ -191,7 +185,7 @@ def append_to_at_scountlog(school_selection, output_format, gender, interests, s
191
 
192
 
193
  # Chatbot Function
194
- def chatbot(policy_name,contract_text,progress=gr.Progress()):
195
 
196
  start_time = datetime.datetime.now()
197
 
@@ -206,7 +200,7 @@ def chatbot(policy_name,contract_text,progress=gr.Progress()):
206
  #print(policy_name)
207
 
208
  #students = get_students(school_selection)
209
- get_policy_text(policy_name)
210
 
211
  #print(policy_text)
212
  #print(contract_text)
@@ -227,8 +221,10 @@ def chatbot(policy_name,contract_text,progress=gr.Progress()):
227
 
228
  trimmed_input = prompt_trim(analysis_input)
229
 
 
230
  with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
231
  out_file.write(trimmed_input)
 
232
 
233
  response = openai.ChatCompletion.create(
234
  model="gpt-4",
@@ -254,9 +250,10 @@ def chatbot(policy_name,contract_text,progress=gr.Progress()):
254
  prompt_tokens = tokens_used.get('prompt_tokens', )
255
  completion_tokens = tokens_used.get('completion_tokens', 0)
256
 
 
257
  with open('response.txt', 'w', encoding='utf-8') as out_file:
258
  out_file.write(gpt_response)
259
-
260
  end_time = datetime.datetime.now()
261
 
262
  response_time = end_time - start_time
@@ -265,7 +262,7 @@ def chatbot(policy_name,contract_text,progress=gr.Progress()):
265
 
266
  #print(contract_redline)
267
 
268
- #append_to_at_scountlog(school_selection, output_format, gender, interests, sports, gpt_response, number_of_athletes,social_media_channel, response_time, question_cost, prompt_tokens, completion_tokens)
269
 
270
  return {contract_redline_html: contract_redline, download_row: gr.Row(visible=True)}
271
 
@@ -321,27 +318,22 @@ def login_auth(username, password):
321
 
322
  return False
323
 
324
- def pdf_to_text(contract_file_cmpt):
325
 
326
- file_text = extract_text(contract_file_cmpt.name)
327
 
 
328
  original_file_name = contract_file_cmpt.name.split("/")[-1]
329
  redline_file_name = original_file_name.split(".")[0]+" Redline.pdf"
330
 
331
- return file_text,redline_file_name
332
 
333
- def download_pdf(file_name,compliance_comments,contract_redline_html):
334
 
335
- contract_redline_comments = "<h2><u>Contract Redline:</u></h2><br>"+contract_redline + "<br><h2><u>Compliance Comments:</u></h2><br>"+compliance_comments
336
-
337
- """
338
- pdf_download = pdfkit.from_string(contract_redline_comments,False)
339
-
340
- with open(file_name, 'wb') as f:
341
- f.write(pdf_download)
342
- """
343
 
344
- return file_name
345
 
346
  # Gradio UI
347
  CIMStheme = gr.themes.Soft().set(
@@ -355,7 +347,7 @@ schools = get_schools()
355
  # scout_response = "<!DOCTYPEhtml><html><head><title>HTMLTableGenerator</title><style>table{width:100%;border:2pxsolid#000000;padding:5px;}tableth{border:2pxsolid#000000;padding:5px;background:#f0f0f0;color:#000000;}tabletd{border:2pxsolid#000000;text-align:left;padding:5px;background:#ffffff;color:#000000;}</style></head><body><table><thead><tr><th>Rank</th><th>CIMS.AI <br>Scout Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media <br>Fan Total</th><th>Interests</th><th>Commentary on Ranking</th></tr></thead><tbody><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></body></html>"
356
  scout_response = "<!DOCTYPEhtml><html><head><title>HTMLTableGenerator</title><style>table{width:100%;border:2pxsolid#000000;padding:5px;background:#ffffff}tableth{border:2pxsolid#000000;padding:5px;background:#f0f0f0;color:#000000;}tabletd{border:2pxsolid#000000;text-align:left;padding:5px;background:#ffffff;color:#000000;}</style></head><body><table><thead><tr><th>Rank</th><th>CIMS.AI<br>Scout<br>Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media<br>Fan Total</th><th>Interests</th><th>Commentary on Ranking</th></tr></thead><tbody><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></body></html>"
357
 
358
- contract_redline = "<p><strong>Campaign:</strong></p><p><strong>Engagement Name:</strong> Applebee's Fall Burger Promo</p><p><strong>Engagement Id:</strong> 7015j000001DvCAAA0</p><p><strong>Sponsor:</strong> Applebee's</p><p><strong>Start Date:</strong> 2023-10-28</p><p><strong>End Date:</strong> 2023-11-11</p><p><strong>Engagement Description</strong></p><p>The goal of the engagement is to Increase Sales by having the Student-Athlete Social Media Post. For the Social Media Post, the sponsor is requesting the student athlete take a photo in front of the <span style='text-decoration: line-through;'>football stadium</span> in eating a Applebee's burger <span style='text-decoration: line-through;'>in your team jersey</span>. The Media rights for the content will be 90 Days.</p><p><strong>Engagement Compensation.</strong></p><p>For successful completion of the engagement the student-athlete will receive payment in the form of Cash.</p><p>Part or all of the payment will be in cash, paid via PayPal.</p><p>The total value of compensation will be 250.</p>"
359
  #pdf_download = pdfkit.from_string(contract_redline, False)
360
 
361
  #print(pdf_download)
@@ -366,6 +358,7 @@ contract_text = ''
366
  policy_text = ''
367
  compliance_comments = ''
368
  file_name = 'redline.pdf'
 
369
 
370
  with gr.Blocks(CIMStheme) as iface:
371
  with gr.Row():
@@ -379,17 +372,17 @@ with gr.Blocks(CIMStheme) as iface:
379
 
380
  with gr.Row():
381
  with gr.Column(variant='panel',scale=1):
382
- policy_name = gr.components.Dropdown(schools, multiselect=False,label="NIL Policy Selection")
383
  contract_file_cmpt = gr.File(label="Select Contract File",file_count="single",file_types=[".pdf"],height=150)
384
  with gr.Column(variant='panel',scale=3):
385
  contract_text_tbox = gr.Textbox(label="Contract Text",interactive=True,info="Upload .pdf or paste in text")
386
  with gr.Row():
387
  with gr.Column():
388
- btn_upload = gr.components.Button(value="Upload Contract", size='sm', variant="primary")
389
  with gr.Column():
390
  gr.Markdown("")
391
  with gr.Column():
392
- btn_redline = gr.components.Button(value="Redline Contract", size='sm', variant="primary")
393
  with gr.Column():
394
  gr.Markdown("")
395
  with gr.Row():
@@ -399,14 +392,18 @@ with gr.Blocks(CIMStheme) as iface:
399
  compliance_comments_tbox = gr.Textbox(interactive=True,label='Compliance Comments')
400
  with gr.Row(visible=False) as download_row:
401
  with gr.Column(variant='panel'):
402
- file_name_tbox = gr.Textbox(interactive=True,label='File Name')
403
- btn_download = gr.components.Button(value="Download Redline", size='sm', variant="primary")
404
- #pdf_download = gr.File(label="Download Redline File",height=150)
405
- btn_download.click(download_pdf,inputs=[file_name_tbox,compliance_comments_tbox,contract_redline_html])
406
- btn_redline.click(chatbot,inputs=[policy_name,contract_text_tbox],outputs=[contract_redline_html,download_row])
407
- btn_upload.click(pdf_to_text,inputs=contract_file_cmpt,outputs=[contract_text_tbox,file_name_tbox])
408
  with gr.Column():
409
  gr.Markdown("")
 
 
 
 
410
  with gr.Column():
411
  gr.Markdown("")
412
  with gr.Column():
@@ -415,6 +412,5 @@ with gr.Blocks(CIMStheme) as iface:
415
  with gr.Column():
416
  gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
417
 
418
-
419
  #iface.queue(concurrency_count=20).launch(auth=login_auth, auth_message= "Enter your username and password that you received from CIMS.AI. To request a login, please email 'info@cims.ai'")
420
  iface.queue(concurrency_count=20).launch()
 
19
  prompts_table_name = 'tblYIZEB8m6JkGDEP'
20
  users_table_name = 'tblLNe5ZL47SvrAEk'
21
  user_log_table_name = 'tblrlTsRrkl6BqMAJ'
22
+ compliancelog_table_name = 'tblQMXWKGlOonkIw2'
23
  policies_table_name = 'tbla6PC65qZfqdJhE'
24
 
25
 
 
33
  base_id = 'appcUK3hUWC7GM2Kb'
34
 
35
  # App name for user login logging
36
+ app = "Compliance"
37
 
38
  headers = {
39
  "Authorization": f"Bearer {airtable_api_key}",
 
147
  return header, template_content
148
 
149
 
150
+ def append_to_at_compliancelog(policy_name_dd,contract_text,gpt_response, response_time, question_cost, prompt_tokens, completion_tokens):
151
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{compliancelog_table_name}'
 
152
 
153
  # Organize data for Airtable
154
  new_fields = {
155
+ 'policy_name': str(policy_name_dd),
156
+ 'contract_text': str(contract_text),
 
 
 
 
 
157
  'gpt_response': str(gpt_response),
158
  'response_time': str(response_time),
159
  'question_cost': question_cost,
 
185
 
186
 
187
  # Chatbot Function
188
+ def chatbot(policy_name_dd,contract_text,progress=gr.Progress()):
189
 
190
  start_time = datetime.datetime.now()
191
 
 
200
  #print(policy_name)
201
 
202
  #students = get_students(school_selection)
203
+ get_policy_text(policy_name_dd)
204
 
205
  #print(policy_text)
206
  #print(contract_text)
 
221
 
222
  trimmed_input = prompt_trim(analysis_input)
223
 
224
+ """
225
  with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
226
  out_file.write(trimmed_input)
227
+ """
228
 
229
  response = openai.ChatCompletion.create(
230
  model="gpt-4",
 
250
  prompt_tokens = tokens_used.get('prompt_tokens', )
251
  completion_tokens = tokens_used.get('completion_tokens', 0)
252
 
253
+ """
254
  with open('response.txt', 'w', encoding='utf-8') as out_file:
255
  out_file.write(gpt_response)
256
+ """
257
  end_time = datetime.datetime.now()
258
 
259
  response_time = end_time - start_time
 
262
 
263
  #print(contract_redline)
264
 
265
+ append_to_at_compliancelog(policy_name_dd,contract_text,contract_redline, response_time, question_cost, prompt_tokens, completion_tokens)
266
 
267
  return {contract_redline_html: contract_redline, download_row: gr.Row(visible=True)}
268
 
 
318
 
319
  return False
320
 
 
321
 
322
+ def pdf_to_text(contract_file_cmpt, contract_text_tbox, file_name_tbox):
323
 
324
+ file_text = extract_text(contract_file_cmpt.name)
325
  original_file_name = contract_file_cmpt.name.split("/")[-1]
326
  redline_file_name = original_file_name.split(".")[0]+" Redline.pdf"
327
 
328
+ return file_text, redline_file_name
329
 
330
+ def download_pdf(compliance_comments,contract_redline_html,file_name_tbox):
331
 
332
+ contract_redline_comments = "<h2><u>Contract Redline:</u></h2><br>"+contract_redline_html + "<br><h2><u>Compliance Comments:</u></h2><br>"+compliance_comments
333
+ global pdf_download
334
+ pdf_download = pdfkit.from_string(contract_redline_comments,None)
 
 
 
 
 
335
 
336
+ return {pdf_download_file: file_name_tbox}
337
 
338
  # Gradio UI
339
  CIMStheme = gr.themes.Soft().set(
 
347
  # scout_response = "<!DOCTYPEhtml><html><head><title>HTMLTableGenerator</title><style>table{width:100%;border:2pxsolid#000000;padding:5px;}tableth{border:2pxsolid#000000;padding:5px;background:#f0f0f0;color:#000000;}tabletd{border:2pxsolid#000000;text-align:left;padding:5px;background:#ffffff;color:#000000;}</style></head><body><table><thead><tr><th>Rank</th><th>CIMS.AI <br>Scout Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media <br>Fan Total</th><th>Interests</th><th>Commentary on Ranking</th></tr></thead><tbody><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></body></html>"
348
  scout_response = "<!DOCTYPEhtml><html><head><title>HTMLTableGenerator</title><style>table{width:100%;border:2pxsolid#000000;padding:5px;background:#ffffff}tableth{border:2pxsolid#000000;padding:5px;background:#f0f0f0;color:#000000;}tabletd{border:2pxsolid#000000;text-align:left;padding:5px;background:#ffffff;color:#000000;}</style></head><body><table><thead><tr><th>Rank</th><th>CIMS.AI<br>Scout<br>Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media<br>Fan Total</th><th>Interests</th><th>Commentary on Ranking</th></tr></thead><tbody><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></body></html>"
349
 
350
+ #contract_redline = "<p><strong>Campaign:</strong></p><p><strong>Engagement Name:</strong> Applebee's Fall Burger Promo</p><p><strong>Engagement Id:</strong> 7015j000001DvCAAA0</p><p><strong>Sponsor:</strong> Applebee's</p><p><strong>Start Date:</strong> 2023-10-28</p><p><strong>End Date:</strong> 2023-11-11</p><p><strong>Engagement Description</strong></p><p>The goal of the engagement is to Increase Sales by having the Student-Athlete Social Media Post. For the Social Media Post, the sponsor is requesting the student athlete take a photo in front of the <span style='text-decoration: line-through;'>football stadium</span> in eating a Applebee's burger <span style='text-decoration: line-through;'>in your team jersey</span>. The Media rights for the content will be 90 Days.</p><p><strong>Engagement Compensation.</strong></p><p>For successful completion of the engagement the student-athlete will receive payment in the form of Cash.</p><p>Part or all of the payment will be in cash, paid via PayPal.</p><p>The total value of compensation will be 250.</p>"
351
  #pdf_download = pdfkit.from_string(contract_redline, False)
352
 
353
  #print(pdf_download)
 
358
  policy_text = ''
359
  compliance_comments = ''
360
  file_name = 'redline.pdf'
361
+ pdf_download = ''
362
 
363
  with gr.Blocks(CIMStheme) as iface:
364
  with gr.Row():
 
372
 
373
  with gr.Row():
374
  with gr.Column(variant='panel',scale=1):
375
+ policy_name_dd = gr.components.Dropdown(schools, multiselect=False,label="NIL Policy Selection")
376
  contract_file_cmpt = gr.File(label="Select Contract File",file_count="single",file_types=[".pdf"],height=150)
377
  with gr.Column(variant='panel',scale=3):
378
  contract_text_tbox = gr.Textbox(label="Contract Text",interactive=True,info="Upload .pdf or paste in text")
379
  with gr.Row():
380
  with gr.Column():
381
+ upload_btn = gr.components.Button(value="Upload Contract", size='sm', variant="primary")
382
  with gr.Column():
383
  gr.Markdown("")
384
  with gr.Column():
385
+ redline_btn = gr.components.Button(value="Redline Contract", size='sm', variant="primary")
386
  with gr.Column():
387
  gr.Markdown("")
388
  with gr.Row():
 
392
  compliance_comments_tbox = gr.Textbox(interactive=True,label='Compliance Comments')
393
  with gr.Row(visible=False) as download_row:
394
  with gr.Column(variant='panel'):
395
+ file_name_tbox = gr.Textbox(interactive=False,label='File Name',visible=False)
396
+ pdf_download_file = gr.File()
397
+ download_btn = gr.components.Button(value="Create Redline PDF", size='sm', variant="primary")
398
+ upload_btn.click(pdf_to_text,inputs=[contract_file_cmpt,contract_text_tbox,file_name_tbox],outputs=[contract_text_tbox,file_name_tbox])
399
+ redline_btn.click(chatbot,inputs=[policy_name_dd,contract_text_tbox],outputs=[contract_redline_html,download_row])
400
+ download_btn.click(download_pdf,inputs=[compliance_comments_tbox,contract_redline_html,file_name_tbox],outputs=pdf_download_file)
401
  with gr.Column():
402
  gr.Markdown("")
403
+ #pdf_download_file = gr.File()
404
+ #email_btn = gr.components.Button(value="Email Redline", size='sm', variant="primary")
405
+ #email_btn.click(send_contract_email)
406
+ #download_btn.click(download_pdf,inputs=[file_name_tbox,compliance_comments_tbox,contract_redline_html])
407
  with gr.Column():
408
  gr.Markdown("")
409
  with gr.Column():
 
412
  with gr.Column():
413
  gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
414
 
 
415
  #iface.queue(concurrency_count=20).launch(auth=login_auth, auth_message= "Enter your username and password that you received from CIMS.AI. To request a login, please email 'info@cims.ai'")
416
  iface.queue(concurrency_count=20).launch()