File size: 3,441 Bytes
f8fa697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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-Old",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()