import os import uuid from supabase import create_client # Đọc từ HF Space Secrets (Settings -> Repository secrets) # KHÔNG hardcode key trực tiếp trong code SUPABASE_URL = os.environ["https://zmngnirfiryieyfoieku.supabase.co"] SUPABASE_KEY = os.environ["eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InptbmduaXJmaXJ5aWV5Zm9pZWt1Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc4NDg5MjYzOSwiZXhwIjoyMTAwNDY4NjM5fQ.PQaFHBfZYuPKej0qhd00tynFuRzApbTdURQMoz49Z6s"] supabase = create_client(SUPABASE_URL, SUPABASE_KEY) def save_full_pipeline_result(device_id, image_local_path, generated_caption, predicted_category, is_ambiguous, model_version="blip-at-wsld-fashion200k-v1", dict_version_id=1): # 1. Upload ảnh lên Storage remote_filename = f"products/{uuid.uuid4()}.jpg" with open(image_local_path, "rb") as f: supabase.storage.from_("product-images").upload(remote_filename, f) # 2. Ghi upload record upload_res = supabase.table("uploads").insert({ "device_id": device_id, "image_path": remote_filename, "status": "done" }).execute() upload_id = upload_res.data[0]["upload_id"] # 3. Ghi caption caption_res = supabase.table("captions").insert({ "upload_id": upload_id, "caption_text": generated_caption, "model_version": model_version }).execute() caption_id = caption_res.data[0]["caption_id"] # 4. Ghi kết quả phân loại supabase.table("classification_results").insert({ "upload_id": upload_id, "caption_id": caption_id, "dict_version_id": dict_version_id, "predicted_category": predicted_category, "is_ambiguous": is_ambiguous }).execute() return upload_id def get_upload_history(device_id): return supabase.table("uploads") \ .select("*, captions(*), classification_results(*)") \ .eq("device_id", device_id) \ .order("uploaded_at", desc=True) \ .execute()