Spaces:
Sleeping
Sleeping
| 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() |