File size: 2,462 Bytes
c0fec27
 
a49a92e
 
d8248fa
c0fec27
d8248fa
c0fec27
7fe8991
 
a49a92e
7ab126f
 
 
c0fec27
 
7fe8991
c0fec27
d8248fa
c5d5b9b
 
7ab126f
c0fec27
7ab126f
 
 
 
 
 
 
1947ad0
 
7ab126f
 
 
c0fec27
 
7ab126f
c0fec27
 
d8248fa
c0fec27
a49a92e
c0fec27
 
7ab126f
c0fec27
 
a49a92e
 
c0fec27
 
a49a92e
c0fec27
 
 
 
 
 
a49a92e
 
c0fec27
a49a92e
 
 
 
 
c0fec27
a49a92e
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
# utils/db.py



import os
from supabase import create_client, Client

# Fetch Supabase credentials securely from environment variables/secrets
SUPABASE_URL = "https://bgjnichdknqpagbfhbls.supabase.co"
SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJnam5pY2hka25xcGFnYmZoYmxzIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTIyMjg1NzgsImV4cCI6MjA2NzgwNDU3OH0.86KFknTnIc3xBGyM1aBJbdzjXzwcLkNC_HpDshZVeqc"




if not SUPABASE_URL or not SUPABASE_KEY:
    raise EnvironmentError("Supabase credentials (SUPABASE_URL, SUPABASE_KEY) are not set.")

supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)


def sign_up_user(email, password, gender, username):
    """Signs up a new user, passing gender in the options metadata."""
    try:
        # The 'gender' is passed inside the 'options' dictionary.
        # This is how Supabase receives extra data during signup, which our trigger will then use.
        response = supabase.auth.sign_up({
            "email": email,
            "password": password,
            "options": {
                "data": {
                    "gender": gender,
                    "username": username
                }
            }
        })
        return response
    except Exception as e:
        print(f"Error during sign up in utils.db: {e}")
        return None

def login_user(email, password):
    try:
        response = supabase.auth.sign_in_with_password({"email": email, "password": password})
        return response
    except Exception as e:
        print(f"Error during login in utils.db: {e}")
        return None

def log_prediction(data, prediction_result, user_id):
    """Logs prediction data to the 'predictions' table using user_id."""
    try:
        prediction_value = 1 if "Diabetic" in prediction_result else 0
        response = supabase.table("predictions").insert({
            "pregnancies": data[0],
            "glucose": data[1],
            "blood_pressure": data[2],
            "insulin": data[3],
            "bmi": data[4],
            "age": data[5],
            "prediction": prediction_value,
            "user_id": user_id
        }).execute()
        
        if len(response.data) > 0:
            print("Prediction logged successfully.")
        else:
            print(f"Error logging prediction: Supabase returned no data. Possible error: {response.error}")
    except Exception as e:
        print(f"Error during prediction logging function: {e}")