dwipper commited on
Commit
f8fa697
1 Parent(s): cfe29e7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+
5
+
6
+ HUGGING_FACE_TOKEN=os.environ.get('HUGGING_FACE_TOKEN', None)
7
+ airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
8
+ base_id = os.environ.get('AIRTABLE_BASE_ID')
9
+ users_table_name = os.environ.get('USERS_TABLE_NAME')
10
+ user_log_table_name = os.environ.get('USER_LOG_TABLE_NAME')
11
+ #App name for user login logging
12
+ app = os.environ.get('APP_NAME')
13
+
14
+ #Header for the Airtable requests
15
+ headers = {
16
+ "Authorization": f"Bearer {airtable_api_key}",
17
+ "Content-Type": "application/json",
18
+ "Accept": "application/json",
19
+ }
20
+
21
+ def log_login(username):
22
+
23
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
24
+
25
+ # Organize data for Airtable
26
+ new_fields = {
27
+ 'user_name': str(username),
28
+ 'app': str(app)
29
+ }
30
+
31
+ data = {
32
+ 'fields': new_fields
33
+ }
34
+
35
+ try:
36
+ # Post data to Airtable
37
+ response = requests.post(airtable_endpoint, headers=headers, json=data)
38
+
39
+ # Check for errors
40
+ response.raise_for_status()
41
+
42
+ except requests.exceptions.HTTPError as http_error:
43
+ # Handle the HTTP error (e.g., log it or display an error message)
44
+ print(f"HTTP error occurred: {http_error}")
45
+
46
+ except Exception as e:
47
+ # Handle exceptions, log errors, or raise them as needed
48
+ print(f"An error occurred: {str(e)}")
49
+
50
+
51
+ def login_auth(username, password):
52
+
53
+ airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
54
+
55
+ username_lcase = username.lower()
56
+
57
+ # Query the 'users' table to check for a match with the provided username and password
58
+ params = {
59
+ 'filterByFormula': f'AND(user_name = "{username_lcase}", password = "{password}")'
60
+ }
61
+
62
+ response = requests.get(airtable_endpoint, headers=headers, params=params)
63
+
64
+ if response.status_code == 200:
65
+ data = response.json()
66
+ #If the matching user/password record is found:
67
+ if data.get('records'):
68
+
69
+ user_details = data['records'][0]['fields']
70
+
71
+ #Log that the user logged in
72
+ log_login(username_lcase)
73
+
74
+ #Set the global logged_in_user variable. This used in the append_to_at_qalog function to track what user asked the question
75
+ global logged_in_user,user_user_role, user_output_format, user_school
76
+ user_user_role = user_details.get('user_role')
77
+ user_output_format = user_details.get('output_format')
78
+ user_school = user_details.get('school_name', [None])[0]
79
+ logged_in_user = username_lcase
80
+
81
+ #print(username)
82
+ #print(username_lcase)
83
+ #print(user_school)
84
+ #print(user_output_format)
85
+ #print(user_user_role)
86
+
87
+ return True
88
+
89
+ print(f"Invalid user/password combination (Airtable)")
90
+
91
+ return False
92
+
93
+ iface = gr.load(src="spaces",name="dwipper/NILI-Mobile-Old",hf_token=HUGGING_FACE_TOKEN)
94
+ """
95
+ def greet(name):
96
+ return "Hello " + name + "!"
97
+ demo = gr.Interface(fn=greet, inputs="text", outputs="text")
98
+
99
+ if __name__ == "__main__":
100
+ demo.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'")
101
+ """
102
+
103
+ 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'")
104
+ #iface.launch()