dwipper commited on
Commit
67b54e2
·
1 Parent(s): 22fcf48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -80
app.py CHANGED
@@ -13,55 +13,52 @@ 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
- policies_table_name = 'tbla6PC65qZfqdJhE'
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
 
23
  # Define the style and content for the response field
24
- label_text = "NILI Response"
25
  color = "#6562F4"
26
  background_color = "white"
27
  border_radius = "10px"
28
  response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
29
 
30
- #Airtable Base ID
31
  base_id = 'appcUK3hUWC7GM2Kb'
32
 
33
- #Name of the prompt temlate record
34
- prompt_name = "NILI_v1"
35
-
36
  #App name for user login logging
37
- app="NILI"
38
 
39
- #Header for the Airtable requests
40
  headers = {
41
  "Authorization": f"Bearer {airtable_api_key}",
42
  "Content-Type": "application/json",
43
  "Accept": "application/json",
44
  }
45
 
46
- #Function to trim prompts....not used
47
  def prompt_trim(prompt: str) -> str:
48
  lines = prompt.split('\n')
49
  trimmed = '\n'.join([l.strip() for l in lines])
50
  return trimmed
51
 
52
- #Get the policies for the selected schools and concatenate them.
53
- def get_policies(school_selection):
54
 
55
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
 
56
 
57
  # Parameters for the API request to filter by 'school' field and retrieve 'policy_text'
58
  params = {
59
- 'filterByFormula': "OR({})".format(','.join(["school='{}'".format(school) for school in school_selection])),
60
- 'fields[]': 'policy_text', # Replace with the name of your field
61
  }
62
 
63
  # Initialize an empty string to store concatenated policies
64
- concatenated_policies = ''
65
 
66
  #print(params)
67
 
@@ -76,29 +73,24 @@ def get_policies(school_selection):
76
 
77
  # Check if there are records in the response
78
  if data.get('records'):
79
-
80
- # Extract the 'policy_text' values from each record and concatenate them
81
- for record in data['records']:
82
- policy_text = record['fields']['policy_text']
83
- if concatenated_policies:
84
- concatenated_policies += "\n----------\n"
85
- concatenated_policies += policy_text
86
 
87
  else:
88
- print("No records found in the 'policies' table for the selected schools.")
89
  else:
90
- print(f"Failed to retrieve data. Status code: {response.status_code}")
91
  except Exception as e:
92
  print(f"An error occurred: {str(e)}")
93
 
94
- #print(concatenated_policies)
95
 
96
- return concatenated_policies
97
 
98
- #Get a list of School Name from the policies for the UI dropdown
99
  def get_schools():
100
 
101
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
102
 
103
  # Parameters for the API request to select only the 'school' field
104
  params = {
@@ -123,22 +115,61 @@ def get_schools():
123
  # Extract the 'school' values from each record
124
  schools = [record['fields']['school'] for record in data['records']]
125
 
 
 
126
  else:
127
  print("No records found in the 'policies' table.")
128
  else:
129
- print(f"Failed to retrieve data. Status code: {response.status_code}")
130
  except Exception as e:
131
  print(f"An error occurred: {str(e)}")
132
 
133
  return schools
134
 
135
- #Get the designated prompt template record
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  def get_prompt(header, template_content):
137
 
138
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{prompts_table_name}'
139
 
140
  params = {
141
- 'filterByFormula': "prompt_name='NILI_v1'"
142
  }
143
 
144
  response = requests.get(airtable_endpoint, headers=headers, params=params)
@@ -178,16 +209,19 @@ def get_examples():
178
  #print(ui_examples)
179
 
180
 
181
- def append_to_at_qalog(your_role, school_selection, output_format, input_text, gpt_response,response_time,question_cost,prompt_tokens,completion_tokens):
182
 
183
- airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{qalog_table_name}'
184
 
185
  # Organize data for Airtable
186
  new_fields = {
187
- 'your_role': str(your_role),
188
  'school_selection': str(school_selection),
 
 
 
 
 
189
  'output_format': str(output_format),
190
- 'input_text': str(input_text),
191
  'gpt_response': str(gpt_response),
192
  'response_time': str(response_time),
193
  'question_cost': question_cost,
@@ -200,9 +234,12 @@ def append_to_at_qalog(your_role, school_selection, output_format, input_text, g
200
  'fields': new_fields
201
  }
202
  try:
 
203
  # Post data to Airtable
204
  response = requests.post(airtable_endpoint, headers=headers, json=data)
205
 
 
 
206
  # Check for errors
207
  response.raise_for_status()
208
 
@@ -216,16 +253,11 @@ def append_to_at_qalog(your_role, school_selection, output_format, input_text, g
216
 
217
 
218
  #Chatbot Function
219
- def chatbot(your_role,school_selection,output_format,input_text):
220
 
221
  start_time = datetime.datetime.now()
222
 
223
- # school_selection holds an array of one or more schools
224
- #print(school_selection)
225
-
226
- # Read the Hydrated policies
227
-
228
- policies = get_policies(school_selection)
229
 
230
  template_content = ''
231
  header = ''
@@ -235,20 +267,14 @@ def chatbot(your_role,school_selection,output_format,input_text):
235
  #print(header)
236
  #print(template_content)
237
 
238
- header_template = Template(header)
239
- merged_header = header_template.render(your_role=your_role)
240
-
241
  # Create a Jinja2 template from the content
242
  template = Template(template_content)
243
 
244
- # Render the template with the policy JSON
245
- analysis_input = template.render(policies=policies, question=input_text,format=output_format,your_role=your_role)
246
 
247
  trimmed_input = prompt_trim(analysis_input)
248
 
249
- with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
250
- out_file.write(trimmed_input)
251
-
252
  response = openai.ChatCompletion.create(
253
  model="gpt-4",
254
  #model="gpt-3.5-turbo",
@@ -256,7 +282,7 @@ def chatbot(your_role,school_selection,output_format,input_text):
256
  messages=[
257
  {
258
  "role": "system",
259
- "content": merged_header
260
  },
261
  {
262
  "role": "user",
@@ -273,14 +299,15 @@ def chatbot(your_role,school_selection,output_format,input_text):
273
  prompt_tokens = tokens_used.get('prompt_tokens',)
274
  completion_tokens = tokens_used.get('completion_tokens', 0)
275
 
276
- """
277
- with open('response.txt', 'w', encoding='utf-8') as out_file:
278
- out_file.write(gpt_response)
279
- """
280
  end_time = datetime.datetime.now()
 
281
  response_time = end_time - start_time
282
 
283
- append_to_at_qalog(your_role, school_selection, output_format, input_text, gpt_response,response_time,question_cost,prompt_tokens,completion_tokens)
 
 
 
 
284
 
285
  return response_label,gpt_response
286
 
@@ -297,14 +324,14 @@ def log_login(username):
297
  data = {
298
  'fields': new_fields
299
  }
300
-
301
  try:
302
- # Post data to Airtable
303
- response = requests.post(airtable_endpoint, headers=headers, json=data)
304
 
305
- # Check for errors
306
- response.raise_for_status()
307
 
 
 
 
308
  except requests.exceptions.HTTPError as http_error:
309
  # Handle the HTTP error (e.g., log it or display an error message)
310
  print(f"HTTP error occurred: {http_error}")
@@ -313,7 +340,6 @@ def log_login(username):
313
  # Handle exceptions, log errors, or raise them as needed
314
  print(f"An error occurred: {str(e)}")
315
 
316
-
317
  def login_auth(username, password):
318
 
319
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
@@ -327,13 +353,9 @@ def login_auth(username, password):
327
 
328
  if response.status_code == 200:
329
  data = response.json()
330
- #If the matching user/password record is found:
331
  if data.get('records'):
332
 
333
- #Log that the user logged in
334
  log_login(username)
335
-
336
- #Set the global logged_in_user variable. This used in the append_to_at_qalog function to track what user asked the question
337
  global logged_in_user
338
  logged_in_user = username
339
 
@@ -351,6 +373,7 @@ ui_examples = []
351
  school_selection = []
352
 
353
  schools = get_schools()
 
354
 
355
  get_examples()
356
 
@@ -361,31 +384,31 @@ with gr.Blocks(CIMStheme) as iface:
361
  with gr.Column(scale=2):
362
  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)
363
  with gr.Column(scale=2):
364
- #gr.Textbox(value="# NILI - Powered by CIMS.AI",show_label=False,interactive=False,text_align="center",elem_id="CIMSTitle")
365
- gr.Markdown(value="# NILI - Powered by CIMS.AI")
366
  with gr.Column(scale=2):
367
  gr.Markdown("")
368
  with gr.Row():
369
  with gr.Column():
370
  gr.Interface(fn=chatbot,
371
  inputs=[
372
- gr.components.Dropdown(["Student Athlete","Parent","Athletic Director"],multiselect=False,info="Select a role.",label="User Role", ),
373
- gr.components.Dropdown(schools,multiselect=True,info="Select one or more schools. This will help set the context of your question.",label="School Context"),
 
 
 
374
  gr.components.Dropdown(["Summary","Detailed Analysis","Table"],multiselect=False,info="Select the desired output format.",label="Output Format"),
375
- gr.components.Textbox(lines=5, placeholder="Enter your question here", label="NIL Question")],
376
  outputs=[
377
  gr.components.Markdown(response_label),
378
- gr.components.HTML(label="NILI Response")
379
  ],
380
- description="Ask any question about Name, Image, Likeness (NIL)",
381
- allow_flagging="manual",
382
- examples=ui_examples,
383
- cache_examples=False,
384
- flagging_options=["The response is incorrect","The response is inappropriate","The response doesn't make sense"]
385
- )
386
  with gr.Row():
387
  with gr.Column():
388
  gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
389
 
390
- 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'")
391
- #iface.launch(share=True)
 
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
 
26
  # Define the style and content for the response field
27
+ label_text = "Scout Response"
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
 
 
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}",
40
  "Content-Type": "application/json",
41
  "Accept": "application/json",
42
  }
43
 
44
+
45
  def prompt_trim(prompt: str) -> str:
46
  lines = prompt.split('\n')
47
  trimmed = '\n'.join([l.strip() for l in lines])
48
  return trimmed
49
 
 
 
50
 
51
+ def get_students(school_selection):
52
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{students_table_name}'
53
 
54
  # Parameters for the API request to filter by 'school' field and retrieve 'policy_text'
55
  params = {
56
+ 'filterByFormula': "OR({})".format(','.join(["School='{}'".format(school) for school in school_selection])),
57
+ 'fields[]': ['Athlete_Name','Primary_Sport','Position','Facebook_Fans','Twitter_Fans','Instagram_Fans','Interests','Ethnicity','Gender'],
58
  }
59
 
60
  # Initialize an empty string to store concatenated policies
61
+ students = ''
62
 
63
  #print(params)
64
 
 
73
 
74
  # Check if there are records in the response
75
  if data.get('records'):
76
+
77
+ cleaned_data = [{'fields': entry['fields']} for entry in data.get('records')]
78
+
79
+ students = json.dumps(cleaned_data, indent=4)
 
 
 
80
 
81
  else:
82
+ print("No records found in the 'students' table for the selected schools.")
83
  else:
84
+ print(f"Failed to retrieve students data. Status code: {response.status_code}")
85
  except Exception as e:
86
  print(f"An error occurred: {str(e)}")
87
 
88
+ return students
89
 
 
90
 
 
91
  def get_schools():
92
 
93
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{schools_table_name}'
94
 
95
  # Parameters for the API request to select only the 'school' field
96
  params = {
 
115
  # Extract the 'school' values from each record
116
  schools = [record['fields']['school'] for record in data['records']]
117
 
118
+ # Print the list of 'school' values
119
+ # print(schools)
120
  else:
121
  print("No records found in the 'policies' table.")
122
  else:
123
+ print(f"Failed to retrieve school data. Status code: {response.status_code}")
124
  except Exception as e:
125
  print(f"An error occurred: {str(e)}")
126
 
127
  return schools
128
 
129
+ def get_sports ():
130
+
131
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{sports_table_name}'
132
+
133
+ # Parameters for the API request to select only the 'school' field
134
+ params = {
135
+ 'fields[]': 'sport', # Replace with the name of your field
136
+ 'sort[0][field]': 'sport', # Sort by the 'school' field
137
+ 'sort[0][direction]': 'asc', # Sort in ascending order
138
+ }
139
+
140
+ schools = ''
141
+
142
+ try:
143
+ # Send a GET request to the Airtable API
144
+ response = requests.get(airtable_endpoint, headers=headers, params=params)
145
+
146
+ # Check if the request was successful (status code 200)
147
+ if response.status_code == 200:
148
+ # Parse the JSON response
149
+ data = response.json()
150
+
151
+ # Check if there are records in the response
152
+ if data.get('records'):
153
+ # Extract the 'school' values from each record
154
+ sports = [record['fields']['sport'] for record in data['records']]
155
+
156
+ # Print the list of 'school' values
157
+ # print(sports)
158
+ else:
159
+ print("No records found in the 'policies' table.")
160
+ else:
161
+ print(f"Failed to retrieve school data. Status code: {response.status_code}")
162
+ except Exception as e:
163
+ print(f"An error occurred: {str(e)}")
164
+
165
+ return sports
166
+
167
  def get_prompt(header, template_content):
168
 
169
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{prompts_table_name}'
170
 
171
  params = {
172
+ 'filterByFormula': "prompt_name='Scout_v1'",
173
  }
174
 
175
  response = requests.get(airtable_endpoint, headers=headers, params=params)
 
209
  #print(ui_examples)
210
 
211
 
212
+ 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):
213
 
214
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{scoutlog_table_name}'
215
 
216
  # Organize data for Airtable
217
  new_fields = {
 
218
  'school_selection': str(school_selection),
219
+ 'gender': str(gender),
220
+ 'social_media_channel': str(social_media_channel),
221
+ 'sports': str(sports),
222
+ 'interests': str(interests),
223
+ 'number_of_athletes': int(number_of_athletes),
224
  'output_format': str(output_format),
 
225
  'gpt_response': str(gpt_response),
226
  'response_time': str(response_time),
227
  'question_cost': question_cost,
 
234
  'fields': new_fields
235
  }
236
  try:
237
+
238
  # Post data to Airtable
239
  response = requests.post(airtable_endpoint, headers=headers, json=data)
240
 
241
+ #print(response.json())
242
+
243
  # Check for errors
244
  response.raise_for_status()
245
 
 
253
 
254
 
255
  #Chatbot Function
256
+ def chatbot(school_selection,gender,social_media_channel,sports,interests,output_format,number_of_athletes):
257
 
258
  start_time = datetime.datetime.now()
259
 
260
+ students = get_students(school_selection)
 
 
 
 
 
261
 
262
  template_content = ''
263
  header = ''
 
267
  #print(header)
268
  #print(template_content)
269
 
 
 
 
270
  # Create a Jinja2 template from the content
271
  template = Template(template_content)
272
 
273
+ # Render the template with the inputs
274
+ 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)
275
 
276
  trimmed_input = prompt_trim(analysis_input)
277
 
 
 
 
278
  response = openai.ChatCompletion.create(
279
  model="gpt-4",
280
  #model="gpt-3.5-turbo",
 
282
  messages=[
283
  {
284
  "role": "system",
285
+ "content": header
286
  },
287
  {
288
  "role": "user",
 
299
  prompt_tokens = tokens_used.get('prompt_tokens',)
300
  completion_tokens = tokens_used.get('completion_tokens', 0)
301
 
 
 
 
 
302
  end_time = datetime.datetime.now()
303
+
304
  response_time = end_time - start_time
305
 
306
+ global scout_response
307
+
308
+ scout_response = gpt_response
309
+
310
+ 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)
311
 
312
  return response_label,gpt_response
313
 
 
324
  data = {
325
  'fields': new_fields
326
  }
 
327
  try:
 
 
328
 
329
+ # Post data to Airtable
330
+ response = requests.post(airtable_endpoint, headers=headers, json=data)
331
 
332
+ # Check for errors
333
+ response.raise_for_status()
334
+
335
  except requests.exceptions.HTTPError as http_error:
336
  # Handle the HTTP error (e.g., log it or display an error message)
337
  print(f"HTTP error occurred: {http_error}")
 
340
  # Handle exceptions, log errors, or raise them as needed
341
  print(f"An error occurred: {str(e)}")
342
 
 
343
  def login_auth(username, password):
344
 
345
  airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
 
353
 
354
  if response.status_code == 200:
355
  data = response.json()
 
356
  if data.get('records'):
357
 
 
358
  log_login(username)
 
 
359
  global logged_in_user
360
  logged_in_user = username
361
 
 
373
  school_selection = []
374
 
375
  schools = get_schools()
376
+ sports = get_sports()
377
 
378
  get_examples()
379
 
 
384
  with gr.Column(scale=2):
385
  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)
386
  with gr.Column(scale=2):
387
+ gr.Markdown(value="# Scout - Powered by CIMS.AI")
 
388
  with gr.Column(scale=2):
389
  gr.Markdown("")
390
  with gr.Row():
391
  with gr.Column():
392
  gr.Interface(fn=chatbot,
393
  inputs=[
394
+ gr.components.Dropdown(schools,multiselect=True,info="Select one or more schools. This will select avaliable Student Athletes from the school(s)",label="School Selection"),
395
+ gr.components.Dropdown(["Male","Female"],multiselect=True,info="Select one or more.",label="Gender"),
396
+ gr.components.Dropdown(["Facebook","Instagram","TikTok","X (Twitter)","YouTube","SnapChat","Twitch"],multiselect=True,info="Select one or more.",label="Social Media Channel"),
397
+ gr.components.Dropdown(sports,multiselect=True,info="Select one or more.",label="Sports"),
398
+ gr.components.Dropdown(["Fashion","Sports","Food","Cooking","Travel","Arts","Music"],multiselect=True,info="Select one or more.",label="Interests"),
399
  gr.components.Dropdown(["Summary","Detailed Analysis","Table"],multiselect=False,info="Select the desired output format.",label="Output Format"),
400
+ 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")],
401
  outputs=[
402
  gr.components.Markdown(response_label),
403
+ gr.components.HTML(label="Scout Response")
404
  ],
405
+ description="Use Scout to find and rank Athletes that meet your criteria",
406
+ allow_flagging="never",
407
+ cache_examples=False
408
+ )
 
 
409
  with gr.Row():
410
  with gr.Column():
411
  gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
412
 
413
+ 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'",share=True)
414
+ #iface.launch()