Spaces:
cimsai
/
Runtime error

Scout / app.py
dwipper's picture
Update app.py
726ec66
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'")