dwipper commited on
Commit
60ab3b8
·
1 Parent(s): 800cbcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -217
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import os
2
  import openai
3
  import datetime
 
4
  import gradio as gr
5
  import json
6
  from jinja2 import Template
7
  import requests
 
 
8
 
9
  # Initialize OpenAI
10
  openai.api_key = os.environ.get('OPENAI_API_KEY')
@@ -13,28 +16,24 @@ openai.api_key = os.environ.get('OPENAI_API_KEY')
13
  airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
14
 
15
  # Airtable table names
16
- schools_table_name = 'tblFh7tguTjbjWms7'
17
  prompts_table_name = 'tblYIZEB8m6JkGDEP'
18
- qalog_table_name = 'tbl4oNgFPWM5xH1XO'
19
- examples_table_name = 'tblu7sraOEmRgEGkp'
20
  users_table_name = 'tblLNe5ZL47SvrAEk'
21
  user_log_table_name = 'tblrlTsRrkl6BqMAJ'
22
- students_table_name = 'tbl9h106z0Jg34DQX'
23
  scoutlog_table_name = 'tblF30p0krA1FwOiD'
24
- sports_table_name = 'tbler8HTzD0Jm8Woi'
25
- interests_table_name = 'tblEXowB3pkkm74gj'
26
 
27
  # Define the style and content for the response field
28
- label_text = "Scout Response"
29
  color = "#6562F4"
30
  background_color = "white"
31
  border_radius = "10px"
32
- #response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
33
- response_label = response_label = f'<span style="display: inline-block; position: relative; z-index: var(--layer-4); border: solid var(--block-title-border-width) var(--block-title-border-color); border-radius: var(--block-title-radius); background: var(--block-title-background-fill); padding: var(--block-title-padding); color: var(--block-title-text-color); font-weight: var(--block-title-text-weight); font-size: var(--block-title-text-size); line-height: var(--line-sm); margin-bottom: var(--spacing-lg);">{label_text}</span>'
34
  base_id = 'appcUK3hUWC7GM2Kb'
35
 
36
- #App name for user login logging
37
- app="Scout"
38
 
39
  headers = {
40
  "Authorization": f"Bearer {airtable_api_key}",
@@ -48,20 +47,18 @@ def prompt_trim(prompt: str) -> str:
48
  trimmed = '\n'.join([l.strip() for l in lines])
49
  return trimmed
50
 
 
51
 
52
- def get_students(school_selection):
53
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{students_table_name}'
54
 
55
- # Parameters for the API request to filter by 'school' field and retrieve 'policy_text'
56
  params = {
57
- 'filterByFormula': "OR({})".format(','.join(["School='{}'".format(school) for school in school_selection])),
58
- 'fields[]': ['Athlete_Name','Primary_Sport','Position','Facebook_Fans','Twitter_Fans','Instagram_Fans','Interests','Ethnicity','Gender'],
59
  }
60
 
61
- # Initialize an empty string to store concatenated policies
62
- students = ''
63
-
64
- #print(params)
65
 
66
  try:
67
  # Send a GET request to the Airtable API
@@ -74,24 +71,23 @@ def get_students(school_selection):
74
 
75
  # Check if there are records in the response
76
  if data.get('records'):
77
-
78
- cleaned_data = [{'fields': entry['fields']} for entry in data.get('records')]
79
-
80
- students = json.dumps(cleaned_data, indent=4)
81
 
82
  else:
83
- print("No records found in the 'students' table for the selected schools.")
84
  else:
85
- print(f"Failed to retrieve students data. Status code: {response.status_code}")
86
  except Exception as e:
87
  print(f"An error occurred: {str(e)}")
88
 
89
- return students
90
 
 
91
 
92
  def get_schools():
93
 
94
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{schools_table_name}'
95
 
96
  # Parameters for the API request to select only the 'school' field
97
  params = {
@@ -116,99 +112,20 @@ def get_schools():
116
  # Extract the 'school' values from each record
117
  schools = [record['fields']['school'] for record in data['records']]
118
 
119
- # Print the list of 'school' values
120
- # print(schools)
121
  else:
122
  print("No records found in the 'policies' table.")
123
  else:
124
- print(f"Failed to retrieve school data. Status code: {response.status_code}")
125
  except Exception as e:
126
  print(f"An error occurred: {str(e)}")
127
 
128
  return schools
129
 
130
- def get_sports():
131
-
132
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{sports_table_name}'
133
-
134
- # Parameters for the API request to select only the 'school' field
135
- params = {
136
- 'fields[]': 'sport', # Replace with the name of your field
137
- 'sort[0][field]': 'sport', # Sort by the 'school' field
138
- 'sort[0][direction]': 'asc', # Sort in ascending order
139
- }
140
-
141
- schools = ''
142
-
143
- try:
144
- # Send a GET request to the Airtable API
145
- response = requests.get(airtable_endpoint, headers=headers, params=params)
146
-
147
- # Check if the request was successful (status code 200)
148
- if response.status_code == 200:
149
- # Parse the JSON response
150
- data = response.json()
151
-
152
- # Check if there are records in the response
153
- if data.get('records'):
154
- # Extract the 'school' values from each record
155
- sports = [record['fields']['sport'] for record in data['records']]
156
-
157
- # Print the list of 'school' values
158
- # print(sports)
159
- else:
160
- print("No records found in the 'schools' table.")
161
- else:
162
- print(f"Failed to retrieve school data. Status code: {response.status_code}")
163
- except Exception as e:
164
- print(f"An error occurred: {str(e)}")
165
-
166
- return sports
167
-
168
- def get_interests():
169
-
170
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{interests_table_name}'
171
-
172
- # Parameters for the API request to select only the 'interest' field
173
- params = {
174
- 'fields[]': 'interest', # Replace with the name of your field
175
- 'sort[0][field]': 'interest', # Sort by the 'school' field
176
- 'sort[0][direction]': 'asc', # Sort in ascending order
177
- }
178
-
179
- interests = ''
180
-
181
- try:
182
- # Send a GET request to the Airtable API
183
- response = requests.get(airtable_endpoint, headers=headers, params=params)
184
-
185
- # Check if the request was successful (status code 200)
186
- if response.status_code == 200:
187
- # Parse the JSON response
188
- data = response.json()
189
-
190
- # Check if there are records in the response
191
- if data.get('records'):
192
- # Extract the 'school' values from each record
193
- interests = [record['fields']['interest'] for record in data['records']]
194
-
195
- else:
196
- print("No records found in the 'interests' table.")
197
- else:
198
- print(f"Failed to retrieve interest data. Status code: {response.status_code}")
199
- except Exception as e:
200
- print(f"An error occurred: {str(e)}")
201
-
202
- return interests
203
-
204
-
205
-
206
  def get_prompt(header, template_content):
207
-
208
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{prompts_table_name}'
209
 
210
  params = {
211
- 'filterByFormula': "prompt_name='Scout_v1'",
212
  }
213
 
214
  response = requests.get(airtable_endpoint, headers=headers, params=params)
@@ -222,7 +139,7 @@ def get_prompt(header, template_content):
222
  if data.get('records'):
223
  # Get the first record (there should be only one)
224
  record = data['records'][0]['fields']
225
-
226
  # Assign system_prompt and user_prompt to variables
227
  header = record.get('system_prompt', '')
228
  template_content = record.get('user_prompt', '')
@@ -230,54 +147,36 @@ def get_prompt(header, template_content):
230
  return header, template_content
231
 
232
 
233
- def get_examples():
234
-
235
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{examples_table_name}'
236
-
237
- # Send your request and parse the response
238
- response = requests.get(airtable_endpoint, headers=headers)
239
- data = json.loads(response.text)
240
-
241
- # Check for errors
242
- response.raise_for_status()
243
-
244
- for record in data['records']:
245
- nil_question = record['fields']['nil_question']
246
- ui_examples.append([None, None, None, nil_question])
247
-
248
- #print(ui_examples)
249
-
250
-
251
- def 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):
252
-
253
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{scoutlog_table_name}'
254
-
255
  # Organize data for Airtable
256
  new_fields = {
257
- 'school_selection': str(school_selection),
258
- 'gender': str(gender),
259
- 'social_media_channel': str(social_media_channel),
260
- 'sports': str(sports),
261
- 'interests': str(interests),
262
- 'number_of_athletes': int(number_of_athletes),
263
- 'output_format': str(output_format),
264
- 'gpt_response': str(gpt_response),
265
- 'response_time': str(response_time),
266
- 'question_cost': question_cost,
267
- 'user_name': str(logged_in_user),
268
- 'prompt_tokens': prompt_tokens,
269
- 'completion_tokens': completion_tokens
270
- }
271
 
272
  data = {
273
  'fields': new_fields
274
- }
275
  try:
276
 
277
  # Post data to Airtable
278
  response = requests.post(airtable_endpoint, headers=headers, json=data)
279
 
280
- #print(response.json())
281
 
282
  # Check for errors
283
  response.raise_for_status()
@@ -291,35 +190,49 @@ def append_to_at_scountlog(school_selection, output_format, gender,interests,spo
291
  print(f"An error occurred: {str(e)}")
292
 
293
 
294
- #Chatbot Function
295
- def chatbot(school_selection,gender,social_media_channel,sports,interests,output_format,number_of_athletes):
296
 
297
  start_time = datetime.datetime.now()
298
 
299
- students = get_students(school_selection)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  template_content = ''
302
  header = ''
303
 
304
  header, template_content = get_prompt(header, template_content)
305
 
306
- #print(header)
307
- #print(template_content)
308
 
309
  # Create a Jinja2 template from the content
310
  template = Template(template_content)
311
 
312
  # Render the template with the inputs
313
- analysis_input = template.render(students=students, gender=gender,social_media_channel=social_media_channel,sports=sports,interests=interests,format=output_format,number_of_athletes=number_of_athletes)
314
 
315
  trimmed_input = prompt_trim(analysis_input)
316
 
317
  with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
318
  out_file.write(trimmed_input)
319
-
320
  response = openai.ChatCompletion.create(
321
  model="gpt-4",
322
- #model="gpt-3.5-turbo",
323
  temperature=0,
324
  messages=[
325
  {
@@ -330,15 +243,15 @@ def chatbot(school_selection,gender,social_media_channel,sports,interests,output
330
  "role": "user",
331
  "content": analysis_input
332
  }
333
- ]
334
- )
335
 
336
  gpt_response = response.choices[0].message["content"]
337
 
338
  tokens_used = response.usage
339
 
340
  question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
341
- prompt_tokens = tokens_used.get('prompt_tokens',)
342
  completion_tokens = tokens_used.get('completion_tokens', 0)
343
 
344
  with open('response.txt', 'w', encoding='utf-8') as out_file:
@@ -348,35 +261,34 @@ def chatbot(school_selection,gender,social_media_channel,sports,interests,output
348
 
349
  response_time = end_time - start_time
350
 
351
- #global scout_response
352
 
353
- scout_response = gpt_response
354
 
355
- 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)
356
 
357
- return scout_response
358
 
359
  def log_login(username):
360
-
361
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
362
 
363
  # Organize data for Airtable
364
  new_fields = {
365
- 'user_name': str(username),
366
- 'app': str(app)
367
- }
368
 
369
  data = {
370
  'fields': new_fields
371
- }
372
  try:
373
 
374
- # Post data to Airtable
375
  response = requests.post(airtable_endpoint, headers=headers, json=data)
376
 
377
  # Check for errors
378
  response.raise_for_status()
379
-
380
  except requests.exceptions.HTTPError as http_error:
381
  # Handle the HTTP error (e.g., log it or display an error message)
382
  print(f"HTTP error occurred: {http_error}")
@@ -385,10 +297,10 @@ def log_login(username):
385
  # Handle exceptions, log errors, or raise them as needed
386
  print(f"An error occurred: {str(e)}")
387
 
388
- def login_auth(username, password):
389
 
 
390
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
391
-
392
  # Query the 'users' table to check for a match with the provided username and password
393
  params = {
394
  'filterByFormula': f'AND(user_name = "{username}", password = "{password}")'
@@ -399,7 +311,6 @@ def login_auth(username, password):
399
  if response.status_code == 200:
400
  data = response.json()
401
  if data.get('records'):
402
-
403
  log_login(username)
404
  global logged_in_user
405
  logged_in_user = username
@@ -410,72 +321,97 @@ def login_auth(username, password):
410
 
411
  return False
412
 
413
- #Gradio UI
414
- CIMStheme = gr.themes.Soft().set(button_primary_background_fill='#6562F4')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415
 
416
- # Initialize an empty list to store the examples
417
- #ui_examples = []
418
- school_selection = []
 
419
 
420
  schools = get_schools()
421
- sports = get_sports()
422
- interests = get_interests()
423
 
424
- #get_examples()
425
- #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 Scout Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media 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>"
426
- 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 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>"
 
 
 
 
 
 
427
 
428
  logged_in_user = 'admin'
 
 
 
 
 
429
 
430
  with gr.Blocks(CIMStheme) as iface:
431
  with gr.Row():
432
  with gr.Column(scale=2):
433
- gr.Image(label="Logo",value="CIMS Logo Purple.png",width=10,show_download_button=False,interactive=False,show_label=False,elem_id="logo",container=False)
 
434
  with gr.Column(scale=2):
435
- gr.Markdown(value="<H1 style='text-align: center;'>Scout - Powered by CIMS.AI</h1>")
436
  with gr.Column(scale=2):
437
  gr.Markdown("")
438
- with gr.Row(variant='panel'):
439
- with gr.Column():
440
- with gr.Row():
441
- with gr.Column():
442
- school_selection=gr.components.Dropdown(schools,multiselect=True,info="Select one or more schools to select from their athlete roster(s).",label="School Selection")
443
- with gr.Column():
444
- gender=gr.components.Dropdown(["Male","Female"],multiselect=True,info="Select one or more.",label="Gender")
445
- with gr.Column():
446
- social_media_channel=gr.components.Dropdown(["Instagram","TikTok","X (Twitter)","YouTube","SnapChat","Twitch","Facebook"],multiselect=True,info="Select one or more.",label="Social Media Channel")
447
- with gr.Row():
448
- with gr.Column():
449
- sports=gr.components.Dropdown(sports,multiselect=True,info="Select one or more.",label="Sports")
450
- with gr.Column():
451
- interests=gr.components.Dropdown(interests,multiselect=True,info="Select one or more.",label="Interests")
452
- with gr.Column():
453
- number_of_athletes=gr.components.Dropdown(["1","2","3","4","5","6","7","8","9","10"],multiselect=False,info="How many athletes are you looking for?",label="Number of Athletes")
454
- with gr.Row():
455
- with gr.Column():
456
- output_format=gr.components.Dropdown(["Summary","Detailed Analysis","Table"],multiselect=False,info="Select the desired output format.",label="Output Format")
457
- with gr.Column():
458
- gr.Markdown("")
459
- with gr.Column():
460
- gr.Markdown("")
461
  with gr.Row():
462
  with gr.Column():
463
- btn = gr.components.Button(value="Submit",size='sm',elem_id="submit",variant="primary")
464
  with gr.Column():
465
  gr.Markdown("")
 
 
466
  with gr.Column():
467
  gr.Markdown("")
468
  with gr.Row():
469
  with gr.Column(variant='panel'):
470
- with gr.Row():
471
- with gr.Column():
472
- gr.components.Markdown(response_label)
473
- with gr.Row():
474
- with gr.Column():
475
- scout_response = gr.components.HTML(value=scout_response,label="response")
476
- btn.click(chatbot, inputs=[school_selection,gender,social_media_channel,sports,interests,output_format,number_of_athletes], outputs=[scout_response])
 
 
 
 
 
 
 
 
 
 
477
  with gr.Row():
478
  with gr.Column():
479
  gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
480
 
481
- iface.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'")
 
 
1
  import os
2
  import openai
3
  import datetime
4
+ import time
5
  import gradio as gr
6
  import json
7
  from jinja2 import Template
8
  import requests
9
+ from pdfminer.high_level import extract_text
10
+ import pdfkit
11
 
12
  # Initialize OpenAI
13
  openai.api_key = os.environ.get('OPENAI_API_KEY')
 
16
  airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
17
 
18
  # Airtable table names
 
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
 
26
  # Define the style and content for the response field
27
+ label_text = "Contract Redline"
28
  color = "#6562F4"
29
  background_color = "white"
30
  border_radius = "10px"
31
+ # response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
32
+ response_label = f'<span style="display: inline-block; position: relative; z-index: var(--layer-4); border: solid var(--block-title-border-width) var(--block-title-border-color); border-radius: var(--block-title-radius); background: var(--block-title-background-fill); padding: var(--block-title-padding); color: var(--block-title-text-color); font-weight: var(--block-title-text-weight); font-size: var(--block-title-text-size); line-height: var(--line-sm); margin-bottom: var(--spacing-lg);">{label_text}</span>'
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}",
 
47
  trimmed = '\n'.join([l.strip() for l in lines])
48
  return trimmed
49
 
50
+ def get_policy_text(school):
51
 
52
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
 
53
 
54
+ # Parameters for the API request to select only the 'school' field
55
  params = {
56
+ 'filterByFormula': f'school="{school}"',
57
+ 'fields[]': 'policy_text'
58
  }
59
 
60
+ global policy_text
61
+ policy_text = ''
 
 
62
 
63
  try:
64
  # Send a GET request to the Airtable API
 
71
 
72
  # Check if there are records in the response
73
  if data.get('records'):
74
+ # Extract the 'school' values from each record
75
+ policy_text = [record['fields']['policy_text'] for record in data['records']]
 
 
76
 
77
  else:
78
+ print("No records found in the 'policies' table.")
79
  else:
80
+ print(f"Failed to retrieve data. Status code: {response.status_code}")
81
  except Exception as e:
82
  print(f"An error occurred: {str(e)}")
83
 
84
+ #print(policy_text)
85
 
86
+ return policy_text
87
 
88
  def get_schools():
89
 
90
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
91
 
92
  # Parameters for the API request to select only the 'school' field
93
  params = {
 
112
  # Extract the 'school' values from each record
113
  schools = [record['fields']['school'] for record in data['records']]
114
 
 
 
115
  else:
116
  print("No records found in the 'policies' table.")
117
  else:
118
+ print(f"Failed to retrieve data. Status code: {response.status_code}")
119
  except Exception as e:
120
  print(f"An error occurred: {str(e)}")
121
 
122
  return schools
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  def get_prompt(header, template_content):
125
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{prompts_table_name}'
 
126
 
127
  params = {
128
+ 'filterByFormula': "prompt_name='Compliance_v1'",
129
  }
130
 
131
  response = requests.get(airtable_endpoint, headers=headers, params=params)
 
139
  if data.get('records'):
140
  # Get the first record (there should be only one)
141
  record = data['records'][0]['fields']
142
+
143
  # Assign system_prompt and user_prompt to variables
144
  header = record.get('system_prompt', '')
145
  template_content = record.get('user_prompt', '')
 
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,
166
+ 'user_name': str(logged_in_user),
167
+ 'prompt_tokens': prompt_tokens,
168
+ 'completion_tokens': completion_tokens
169
+ }
170
 
171
  data = {
172
  'fields': new_fields
173
+ }
174
  try:
175
 
176
  # Post data to Airtable
177
  response = requests.post(airtable_endpoint, headers=headers, json=data)
178
 
179
+ # print(response.json())
180
 
181
  # Check for errors
182
  response.raise_for_status()
 
190
  print(f"An error occurred: {str(e)}")
191
 
192
 
193
+ # Chatbot Function
194
+ def chatbot(policy_name,contract_text,progress=gr.Progress()):
195
 
196
  start_time = datetime.datetime.now()
197
 
198
+ progress(progress=None)
199
+
200
+ """
201
+ time.sleep(10)
202
+ for i in progress.tqdm(range(100)):
203
+ time.sleep(1)
204
+ """
205
+
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)
213
 
214
  template_content = ''
215
  header = ''
216
 
217
  header, template_content = get_prompt(header, template_content)
218
 
219
+ # print(header)
220
+ # print(template_content)
221
 
222
  # Create a Jinja2 template from the content
223
  template = Template(template_content)
224
 
225
  # Render the template with the inputs
226
+ analysis_input = template.render(contract_text=contract_text,policy_text=policy_text)
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",
235
+ # model="gpt-3.5-turbo",
236
  temperature=0,
237
  messages=[
238
  {
 
243
  "role": "user",
244
  "content": analysis_input
245
  }
246
+ ]
247
+ )
248
 
249
  gpt_response = response.choices[0].message["content"]
250
 
251
  tokens_used = response.usage
252
 
253
  question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
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:
 
261
 
262
  response_time = end_time - start_time
263
 
264
+ contract_redline = gpt_response
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
 
272
  def log_login(username):
 
273
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
274
 
275
  # Organize data for Airtable
276
  new_fields = {
277
+ 'user_name': str(username),
278
+ 'app': str(app)
279
+ }
280
 
281
  data = {
282
  'fields': new_fields
283
+ }
284
  try:
285
 
286
+ # Post data to Airtable
287
  response = requests.post(airtable_endpoint, headers=headers, json=data)
288
 
289
  # Check for errors
290
  response.raise_for_status()
291
+
292
  except requests.exceptions.HTTPError as http_error:
293
  # Handle the HTTP error (e.g., log it or display an error message)
294
  print(f"HTTP error occurred: {http_error}")
 
297
  # Handle exceptions, log errors, or raise them as needed
298
  print(f"An error occurred: {str(e)}")
299
 
 
300
 
301
+ def login_auth(username, password):
302
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
303
+
304
  # Query the 'users' table to check for a match with the provided username and password
305
  params = {
306
  'filterByFormula': f'AND(user_name = "{username}", password = "{password}")'
 
311
  if response.status_code == 200:
312
  data = response.json()
313
  if data.get('records'):
 
314
  log_login(username)
315
  global logged_in_user
316
  logged_in_user = username
 
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
+ pdf_download = pdfkit.from_string(contract_redline_comments,False)
338
+
339
+ with open(file_name, 'wb') as f:
340
+ f.write(pdf_download)
341
+
342
+ return file_name
343
 
344
+ # Gradio UI
345
+ CIMStheme = gr.themes.Soft().set(
346
+ button_primary_background_fill='#6562F4',
347
+ )
348
 
349
  schools = get_schools()
 
 
350
 
351
+ #policy_text = get_policy_text("LSU") #for testing the function call
352
+
353
+ # 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>"
354
+ 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>"
355
+
356
+ 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>"
357
+ pdf_download = pdfkit.from_string(contract_redline, False)
358
+
359
+ #print(pdf_download)
360
 
361
  logged_in_user = 'admin'
362
+ file_text = ''
363
+ contract_text = ''
364
+ policy_text = ''
365
+ compliance_comments = ''
366
+ file_name = 'redline.pdf'
367
 
368
  with gr.Blocks(CIMStheme) as iface:
369
  with gr.Row():
370
  with gr.Column(scale=2):
371
+ gr.Image(label="Logo", value="CIMS Logo Purple.png", width=10, show_download_button=False,
372
+ interactive=False, show_label=False, elem_id="logo", container=False)
373
  with gr.Column(scale=2):
374
+ gr.Markdown(value="<H1 style='text-align: center;'>Compliance Desktop - Powered by CIMS.AI</h1>")
375
  with gr.Column(scale=2):
376
  gr.Markdown("")
377
+
378
+ with gr.Row():
379
+ with gr.Column(variant='panel',scale=1):
380
+ policy_name = gr.components.Dropdown(schools, multiselect=False,label="NIL Policy Selection")
381
+ contract_file_cmpt = gr.File(label="Select Contract File",file_count="single",file_types=[".pdf"],height=150)
382
+ with gr.Column(variant='panel',scale=3):
383
+ contract_text_tbox = gr.Textbox(label="Contract Text",interactive=True,info="Upload .pdf or paste in text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  with gr.Row():
385
  with gr.Column():
386
+ btn_upload = gr.components.Button(value="Upload Contract", size='sm', variant="primary")
387
  with gr.Column():
388
  gr.Markdown("")
389
+ with gr.Column():
390
+ btn_redline = gr.components.Button(value="Redline Contract", size='sm', variant="primary")
391
  with gr.Column():
392
  gr.Markdown("")
393
  with gr.Row():
394
  with gr.Column(variant='panel'):
395
+ gr.components.Markdown(response_label)
396
+ contract_redline_html = gr.components.HTML(label="Contract Redline")
397
+ compliance_comments_tbox = gr.Textbox(interactive=True,label='Compliance Comments')
398
+ with gr.Row(visible=False) as download_row:
399
+ with gr.Column(variant='panel'):
400
+ file_name_tbox = gr.Textbox(interactive=True,label='File Name')
401
+ btn_download = gr.components.Button(value="Download Redline", size='sm', variant="primary")
402
+ #pdf_download = gr.File(label="Download Redline File",height=150)
403
+ btn_download.click(download_pdf,inputs=[file_name_tbox,compliance_comments_tbox,contract_redline_html])
404
+ btn_redline.click(chatbot,inputs=[policy_name,contract_text_tbox],outputs=[contract_redline_html,download_row])
405
+ btn_upload.click(pdf_to_text,inputs=contract_file_cmpt,outputs=[contract_text_tbox,file_name_tbox])
406
+ with gr.Column():
407
+ gr.Markdown("")
408
+ with gr.Column():
409
+ gr.Markdown("")
410
+ with gr.Column():
411
+ gr.Markdown("")
412
  with gr.Row():
413
  with gr.Column():
414
  gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
415
 
416
+ 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'")
417
+ #iface.queue(concurrency_count=20).launch()