Spaces:
cimsai
/
Runtime error

dwipper commited on
Commit
9419470
·
1 Parent(s): 239abe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -1
app.py CHANGED
@@ -2,7 +2,69 @@ import gradio as gr
2
  import os
3
 
4
  HUGGING_FACE_TOKEN=os.environ.get('HUGGING_FACE_TOKEN', None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  nili = gr.load(src="spaces",name="dwipper/NILI",hf_token=HUGGING_FACE_TOKEN)
7
 
8
- nili.launch()
 
 
2
  import os
3
 
4
  HUGGING_FACE_TOKEN=os.environ.get('HUGGING_FACE_TOKEN', None)
5
+ airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
6
+ base_id = os.environ.get('AIRTABLE_BASE_ID')
7
+
8
+ def log_login(username):
9
+
10
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
11
+
12
+ # Organize data for Airtable
13
+ new_fields = {
14
+ 'user_name': str(username),
15
+ 'app': str(app)
16
+ }
17
+
18
+ data = {
19
+ 'fields': new_fields
20
+ }
21
+
22
+ try:
23
+ # Post data to Airtable
24
+ response = requests.post(airtable_endpoint, headers=headers, json=data)
25
+
26
+ # Check for errors
27
+ response.raise_for_status()
28
+
29
+ except requests.exceptions.HTTPError as http_error:
30
+ # Handle the HTTP error (e.g., log it or display an error message)
31
+ print(f"HTTP error occurred: {http_error}")
32
+
33
+ except Exception as e:
34
+ # Handle exceptions, log errors, or raise them as needed
35
+ print(f"An error occurred: {str(e)}")
36
+
37
+
38
+ def login_auth(username, password):
39
+
40
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
41
+
42
+ # Query the 'users' table to check for a match with the provided username and password
43
+ params = {
44
+ 'filterByFormula': f'AND(user_name = "{username}", password = "{password}")'
45
+ }
46
+
47
+ response = requests.get(airtable_endpoint, headers=headers, params=params)
48
+
49
+ if response.status_code == 200:
50
+ data = response.json()
51
+ #If the matching user/password record is found:
52
+ if data.get('records'):
53
+
54
+ #Log that the user logged in
55
+ log_login(username)
56
+
57
+ #Set the global logged_in_user variable. This used in the append_to_at_qalog function to track what user asked the question
58
+ global logged_in_user
59
+ logged_in_user = username
60
+
61
+ return True
62
+
63
+ print(f"Invalid user/password combination")
64
+
65
+ return False
66
 
67
  nili = gr.load(src="spaces",name="dwipper/NILI",hf_token=HUGGING_FACE_TOKEN)
68
 
69
+ 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'")
70
+ #nili.launch()