Spaces:
Sleeping
Sleeping
File size: 2,812 Bytes
400e20f |
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 |
from supabase import create_client, Client
from datetime import datetime
import os
from dotenv import load_dotenv
import uuid
# Load environment variables
load_dotenv()
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
if not SUPABASE_URL or not SUPABASE_KEY:
raise ValueError("β Missing SUPABASE_URL or SUPABASE_KEY in .env file")
class Database:
def __init__(self):
self.client: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
print("β
Supabase client initialized successfully.")
# ---------- User Operations ----------
def create_user(self, user_data):
data = {
"id": user_data.get("id", str(uuid.uuid4())),
"username": user_data["username"],
"email": user_data["email"],
"age": user_data["age"],
"gender": user_data["gender"],
"allergies": user_data.get("allergies", ""),
"conditions": user_data.get("conditions", ""),
"created_at": datetime.utcnow().isoformat()
}
try:
response = self.client.table("users").insert(data).execute()
print("β
User created successfully:", response.data)
return data["id"]
except Exception as e:
raise Exception(f"β Error creating user: {str(e)}")
# ---------- Symptom Analysis Logging ----------
def log_symptom_analysis(self, analysis_data):
data = {
"id": analysis_data.get("id", str(uuid.uuid4())),
"user_id": analysis_data["user_id"],
"symptoms": analysis_data["symptoms"],
"analysis_result": analysis_data["result"],
"created_at": datetime.utcnow().isoformat()
}
try:
response = self.client.table("symptoms_history").insert(data).execute()
print("β
Symptom analysis logged successfully:", response.data)
except Exception as e:
raise Exception(f"β Error logging symptom analysis: {str(e)}")
# ---------- Image Analysis Logging ----------
def log_image_analysis(self, analysis_data):
data = {
"id": analysis_data.get("id", str(uuid.uuid4())),
"user_id": analysis_data["user_id"],
"filename": analysis_data["filename"],
"analysis_result": analysis_data["result"],
"confidence": analysis_data["confidence"],
"created_at": datetime.utcnow().isoformat()
}
try:
response = self.client.table("image_analysis").insert(data).execute()
print("β
Image analysis logged successfully:", response.data)
except Exception as e:
raise Exception(f"β Error logging image analysis: {str(e)}")
# Initialize database instance
db = Database() |