dwipper commited on
Commit
3de2132
·
1 Parent(s): 2c458c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -188,12 +188,21 @@ def append_to_at_qalog(your_role, school_selection, output_format, input_text, g
188
  'fields': new_fields
189
  }
190
 
 
191
  # Post data to Airtable
192
- response = requests.post(AIRTABLE_ENDPOINT, headers=HEADERS, json=data)
193
 
194
  # Check for errors
195
  response.raise_for_status()
196
 
 
 
 
 
 
 
 
 
197
  #Chatbot Function
198
  def chatbot(your_role,school_selection,output_format,input_text):
199
 
@@ -210,20 +219,15 @@ def chatbot(your_role,school_selection,output_format,input_text):
210
  header = ''
211
 
212
  header, template_content = get_prompt(header, template_content)
213
-
214
- #print(header)
215
- #print(template_content)
216
-
217
-
218
  # Create a Jinja2 template from the content
219
  template = Template(template_content)
220
 
221
  # Render the template with the policy JSON
222
  analysis_input = template.render(policies=policies, question=input_text,format=output_format)
223
-
224
 
225
- with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
226
- out_file.write(analysis_input)
227
 
228
  response = openai.ChatCompletion.create(
229
  model="gpt-4",
@@ -244,23 +248,13 @@ def chatbot(your_role,school_selection,output_format,input_text):
244
  gpt_response = response.choices[0].message["content"]
245
 
246
  tokens_used = response.usage
247
-
248
  question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
249
  prompt_tokens = tokens_used.get('prompt_tokens',)
250
  completion_tokens = tokens_used.get('completion_tokens', 0)
251
-
252
- #print(question_cost)
253
-
254
- """
255
- with open('response.txt', 'w', encoding='utf-8') as out_file:
256
- out_file.write(gpt_response)
257
- """
258
-
259
  end_time = datetime.datetime.now()
260
-
261
  response_time = end_time - start_time
262
 
263
- #append_to_at_qalog(your_role, school_selection, output_format, input_text, gpt_response,response_time,question_cost,prompt_tokens,completion_tokens)
264
 
265
  return response_label,gpt_response
266
 
@@ -277,17 +271,23 @@ def log_login(username):
277
  'fields': new_fields
278
  }
279
 
280
- # Post data to Airtable
281
- response = requests.post(AIRTABLE_ENDPOINT, headers=HEADERS, json=data)
282
-
283
- # Check for errors
284
- response.raise_for_status()
285
 
 
 
 
 
 
 
286
 
 
 
 
287
 
288
  def login_auth(username, password):
289
 
290
-
291
  AIRTABLE_ENDPOINT = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
292
 
293
  # Query the 'users' table to check for a match with the provided username and password
@@ -297,8 +297,6 @@ def login_auth(username, password):
297
 
298
  response = requests.get(AIRTABLE_ENDPOINT, headers=HEADERS, params=params)
299
 
300
- print(response)
301
-
302
  if response.status_code == 200:
303
  data = response.json()
304
  if data.get('records'):
@@ -308,7 +306,9 @@ def login_auth(username, password):
308
  logged_in_user = username
309
 
310
  return True
311
-
 
 
312
  return False
313
 
314
  #Gradio UI
 
188
  'fields': new_fields
189
  }
190
 
191
+ try:
192
  # Post data to Airtable
193
+ response = requests.post(airtable_endpoint, headers=headers, json=data)
194
 
195
  # Check for errors
196
  response.raise_for_status()
197
 
198
+ except requests.exceptions.HTTPError as http_error:
199
+ # Handle the HTTP error (e.g., log it or display an error message)
200
+ print(f"HTTP error occurred: {http_error}")
201
+
202
+ except Exception as e:
203
+ # Handle exceptions, log errors, or raise them as needed
204
+ print(f"An error occurred: {str(e)}")
205
+
206
  #Chatbot Function
207
  def chatbot(your_role,school_selection,output_format,input_text):
208
 
 
219
  header = ''
220
 
221
  header, template_content = get_prompt(header, template_content)
222
+
 
 
 
 
223
  # Create a Jinja2 template from the content
224
  template = Template(template_content)
225
 
226
  # Render the template with the policy JSON
227
  analysis_input = template.render(policies=policies, question=input_text,format=output_format)
 
228
 
229
+ #with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
230
+ #out_file.write(analysis_input)
231
 
232
  response = openai.ChatCompletion.create(
233
  model="gpt-4",
 
248
  gpt_response = response.choices[0].message["content"]
249
 
250
  tokens_used = response.usage
 
251
  question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
252
  prompt_tokens = tokens_used.get('prompt_tokens',)
253
  completion_tokens = tokens_used.get('completion_tokens', 0)
 
 
 
 
 
 
 
 
254
  end_time = datetime.datetime.now()
 
255
  response_time = end_time - start_time
256
 
257
+ append_to_at_qalog(your_role, school_selection, output_format, input_text, gpt_response,response_time,question_cost,prompt_tokens,completion_tokens)
258
 
259
  return response_label,gpt_response
260
 
 
271
  'fields': new_fields
272
  }
273
 
274
+ try:
275
+ # Post data to Airtable
276
+ response = requests.post(AIRTABLE_ENDPOINT, headers=HEADERS, json=data)
 
 
277
 
278
+ # Check for errors
279
+ response.raise_for_status()
280
+
281
+ except requests.exceptions.HTTPError as http_error:
282
+ # Handle the HTTP error (e.g., log it or display an error message)
283
+ print(f"HTTP error occurred: {http_error}")
284
 
285
+ except Exception as e:
286
+ # Handle exceptions, log errors, or raise them as needed
287
+ print(f"An error occurred: {str(e)}")
288
 
289
  def login_auth(username, password):
290
 
 
291
  AIRTABLE_ENDPOINT = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
292
 
293
  # Query the 'users' table to check for a match with the provided username and password
 
297
 
298
  response = requests.get(AIRTABLE_ENDPOINT, headers=HEADERS, params=params)
299
 
 
 
300
  if response.status_code == 200:
301
  data = response.json()
302
  if data.get('records'):
 
306
  logged_in_user = username
307
 
308
  return True
309
+
310
+ print(f"Invalid user/password combination")
311
+
312
  return False
313
 
314
  #Gradio UI