Vx2-3y commited on
Commit
fcd441c
·
1 Parent(s): a08b2e2

Integrate Supabase: store job input, parameters, model, and result in inference_results table

Browse files
Files changed (2) hide show
  1. main.py +28 -0
  2. requirements.txt +1 -1
main.py CHANGED
@@ -9,6 +9,7 @@ import threading
9
  import time
10
  import uuid
11
  import redis
 
12
 
13
  app = FastAPI(
14
  title="NCOS Compliance LLM API",
@@ -78,6 +79,19 @@ JOB_RESULT_PREFIX = "ncos_job_result:"
78
  # --- Model Cache ---
79
  model_cache = {"name": None, "pipeline": None}
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  # --- Background Worker Thread ---
82
  def job_worker():
83
  while True:
@@ -103,6 +117,20 @@ def job_worker():
103
  output = pipe(input_text, **params)
104
  result_text = output[0]["generated_text"] if output and "generated_text" in output[0] else str(output)
105
  redis_client.set(JOB_RESULT_PREFIX + job_id, result_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  except Exception as e:
107
  logger.error(f"Job {job_id} failed: {e}")
108
  redis_client.set(JOB_RESULT_PREFIX + job_id, f"ERROR: {e}")
 
9
  import time
10
  import uuid
11
  import redis
12
+ from supabase import create_client, Client
13
 
14
  app = FastAPI(
15
  title="NCOS Compliance LLM API",
 
79
  # --- Model Cache ---
80
  model_cache = {"name": None, "pipeline": None}
81
 
82
+ # --- Supabase Connection ---
83
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
84
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
85
+ supabase: Client = None
86
+ if SUPABASE_URL and SUPABASE_KEY:
87
+ try:
88
+ supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
89
+ logger.info("Connected to Supabase.")
90
+ except Exception as e:
91
+ logger.error(f"Failed to connect to Supabase: {e}")
92
+ else:
93
+ logger.warning("Supabase credentials not set. Skipping Supabase integration.")
94
+
95
  # --- Background Worker Thread ---
96
  def job_worker():
97
  while True:
 
117
  output = pipe(input_text, **params)
118
  result_text = output[0]["generated_text"] if output and "generated_text" in output[0] else str(output)
119
  redis_client.set(JOB_RESULT_PREFIX + job_id, result_text)
120
+ # --- Store result in Supabase ---
121
+ if supabase:
122
+ try:
123
+ data = {
124
+ "job_id": job_id,
125
+ "input_text": input_text,
126
+ "parameters": str(parameters),
127
+ "model_name": model_name,
128
+ "result": result_text
129
+ }
130
+ supabase.table("inference_results").insert(data).execute()
131
+ logger.info(f"Stored job {job_id} result in Supabase.")
132
+ except Exception as e:
133
+ logger.error(f"Failed to store job {job_id} in Supabase: {e}")
134
  except Exception as e:
135
  logger.error(f"Job {job_id} failed: {e}")
136
  redis_client.set(JOB_RESULT_PREFIX + job_id, f"ERROR: {e}")
requirements.txt CHANGED
@@ -5,7 +5,7 @@ fastapi
5
  uvicorn[standard]
6
 
7
  # Supabase Python client
8
- supabase
9
 
10
  # For loading environment variables from .env
11
  python-dotenv
 
5
  uvicorn[standard]
6
 
7
  # Supabase Python client
8
+ supabase==2.3.5
9
 
10
  # For loading environment variables from .env
11
  python-dotenv