File size: 2,558 Bytes
c716c3a 2b4c5dc bf64463 2d13d3d 726ec66 a969318 9419470 bfa1d96 d8b6060 315e45b d8b6060 9419470 c9d5619 9419470 c9d5619 9419470 020fc51 022a5c3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import gradio as gr
import os
import requests
HUGGING_FACE_TOKEN=os.environ.get('HUGGING_FACE_TOKEN', None)
airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
base_id = os.environ.get('AIRTABLE_BASE_ID')
users_table_name = os.environ.get('USERS_TABLE_NAME')
user_log_table_name = os.environ.get('USER_LOG_TABLE_NAME')
#App name for user login logging
app = os.environ.get('APP_NAME')
#Header for the Airtable requests
headers = {
"Authorization": f"Bearer {airtable_api_key}",
"Content-Type": "application/json",
"Accept": "application/json",
}
def log_login(username):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
# Organize data for Airtable
new_fields = {
'user_name': str(username),
'app': str(app)
}
data = {
'fields': new_fields
}
try:
# Post data to Airtable
response = requests.post(airtable_endpoint, headers=headers, json=data)
# Check for errors
response.raise_for_status()
except requests.exceptions.HTTPError as http_error:
# Handle the HTTP error (e.g., log it or display an error message)
print(f"HTTP error occurred: {http_error}")
except Exception as e:
# Handle exceptions, log errors, or raise them as needed
print(f"An error occurred: {str(e)}")
def login_auth(username, password):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
# Query the 'users' table to check for a match with the provided username and password
params = {
'filterByFormula': f'AND(user_name = "{username}", password = "{password}")'
}
response = requests.get(airtable_endpoint, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
#If the matching user/password record is found:
if data.get('records'):
#Log that the user logged in
log_login(username)
#Set the global logged_in_user variable. This used in the append_to_at_qalog function to track what user asked the question
global logged_in_user
logged_in_user = username
return True
print(f"Invalid user/password combination (Airtable)")
return False
nili = gr.load(src="spaces",name="dwipper/Scout",hf_token=HUGGING_FACE_TOKEN)
nili.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'") |