|
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 = os.environ.get('APP_NAME') |
|
|
|
|
|
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}' |
|
|
|
|
|
new_fields = { |
|
'user_name': str(username), |
|
'app': str(app) |
|
} |
|
|
|
data = { |
|
'fields': new_fields |
|
} |
|
|
|
try: |
|
|
|
response = requests.post(airtable_endpoint, headers=headers, json=data) |
|
|
|
|
|
response.raise_for_status() |
|
|
|
except requests.exceptions.HTTPError as http_error: |
|
|
|
print(f"HTTP error occurred: {http_error}") |
|
|
|
except Exception as e: |
|
|
|
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}' |
|
|
|
|
|
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 data.get('records'): |
|
|
|
|
|
log_login(username) |
|
|
|
|
|
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'") |