Spaces:
Runtime error
Runtime error
| 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}' | |
| username_lcase = username.lower() | |
| # Query the 'users' table to check for a match with the provided username and password | |
| params = { | |
| 'filterByFormula': f'AND(user_name = "{username_lcase}", 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'): | |
| user_details = data['records'][0]['fields'] | |
| #Log that the user logged in | |
| log_login(username_lcase) | |
| #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,user_user_role, user_output_format, user_school | |
| user_user_role = user_details.get('user_role') | |
| user_output_format = user_details.get('output_format') | |
| user_school = user_details.get('school_name', [None])[0] | |
| logged_in_user = username_lcase | |
| #print(username) | |
| #print(username_lcase) | |
| #print(user_school) | |
| #print(user_output_format) | |
| #print(user_user_role) | |
| return True | |
| print(f"Invalid user/password combination (Airtable)") | |
| return False | |
| iface = gr.load(src="spaces",name="dwipper/NILI-Mobile",hf_token=HUGGING_FACE_TOKEN) | |
| """ | |
| def greet(name): | |
| return "Hello " + name + "!" | |
| demo = gr.Interface(fn=greet, inputs="text", outputs="text") | |
| if __name__ == "__main__": | |
| 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'") | |
| """ | |
| 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'") | |
| #iface.launch() |