stvnnnnnn commited on
Commit
d769770
·
verified ·
1 Parent(s): a942df4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -17,7 +17,7 @@ from langdetect import detect
17
  from transformers import MarianMTModel, MarianTokenizer
18
  from openai import OpenAI
19
 
20
- # ---- Postgres (Neon) ----
21
  import psycopg2
22
  from psycopg2 import sql as pgsql
23
 
@@ -43,14 +43,14 @@ DEVICE = torch.device("cpu")
43
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
44
  openai_client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
45
 
46
- # DSN de Neon (Postgres) – EJEMPLO:
47
- # postgres://user:pass@host/neondb?sslmode=require
48
  POSTGRES_DSN = os.getenv("POSTGRES_DSN")
49
 
50
  if not POSTGRES_DSN:
51
  raise RuntimeError(
52
  "⚠️ POSTGRES_DSN no está definido. "
53
- "Configúralo en los secrets del Space con la cadena de conexión de Neon."
54
  )
55
 
56
  # ======================================================
@@ -128,8 +128,6 @@ class PostgresManager:
128
  "CREATE EXTENSION",
129
  "ALTER EXTENSION",
130
  "DROP EXTENSION",
131
- "CREATE SCHEMA", # no queremos recrear schemas globales
132
- "ALTER SCHEMA",
133
  "REVOKE ",
134
  "GRANT ",
135
  "ALTER ROLE",
@@ -574,7 +572,7 @@ sql_manager = PostgresManager(POSTGRES_DSN)
574
  # ======================================================
575
 
576
  app = FastAPI(
577
- title="NL2SQL Backend (Supabase + Postgres/Neon)",
578
  version="3.0.0",
579
  )
580
 
@@ -1042,7 +1040,7 @@ def _combine_sql_files_from_zip(zip_bytes: bytes) -> str:
1042
  @app.on_event("startup")
1043
  async def startup_event():
1044
  load_nl2sql_model()
1045
- print("✅ Backend NL2SQL inicializado (engine Postgres/Neon).")
1046
  print(f"MODEL_DIR={MODEL_DIR}, DEVICE={DEVICE}")
1047
  print(f"Conexiones activas al inicio: {len(sql_manager.connections)}")
1048
 
@@ -1383,9 +1381,9 @@ def get_my_databases(authorization: Optional[str] = Header(None)):
1383
  @app.get("/")
1384
  async def root():
1385
  return {
1386
- "message": "NL2SQL T5-large backend running on Postgres/Neon (no SQLite).",
1387
  "endpoints": [
1388
- "POST /upload (subir .sql o .zip con .sql → crea schema en Neon)",
1389
  "GET /connections (listar BDs subidas en esta instancia)",
1390
  "GET /schema/{id} (esquema resumido)",
1391
  "GET /preview/{id}/{t} (preview de tabla)",
 
17
  from transformers import MarianMTModel, MarianTokenizer
18
  from openai import OpenAI
19
 
20
+ # ---- Postgres ----
21
  import psycopg2
22
  from psycopg2 import sql as pgsql
23
 
 
43
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
44
  openai_client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
45
 
46
+ # DSN de Supabase Postgres – EJEMPLO:
47
+ # postgresql://postgres:TU_PASSWORD@db.xxx.supabase.co:5432/postgres
48
  POSTGRES_DSN = os.getenv("POSTGRES_DSN")
49
 
50
  if not POSTGRES_DSN:
51
  raise RuntimeError(
52
  "⚠️ POSTGRES_DSN no está definido. "
53
+ "Configúralo en los secrets del Space con la cadena de conexión de Supabase."
54
  )
55
 
56
  # ======================================================
 
128
  "CREATE EXTENSION",
129
  "ALTER EXTENSION",
130
  "DROP EXTENSION",
 
 
131
  "REVOKE ",
132
  "GRANT ",
133
  "ALTER ROLE",
 
572
  # ======================================================
573
 
574
  app = FastAPI(
575
+ title="NL2SQL Backend",
576
  version="3.0.0",
577
  )
578
 
 
1040
  @app.on_event("startup")
1041
  async def startup_event():
1042
  load_nl2sql_model()
1043
+ print("✅ Backend NL2SQL inicializado.")
1044
  print(f"MODEL_DIR={MODEL_DIR}, DEVICE={DEVICE}")
1045
  print(f"Conexiones activas al inicio: {len(sql_manager.connections)}")
1046
 
 
1381
  @app.get("/")
1382
  async def root():
1383
  return {
1384
+ "message": "NL2SQL T5-large backend running.",
1385
  "endpoints": [
1386
+ "POST /upload (subir .sql o .zip con .sql → crea schema en Supabase)",
1387
  "GET /connections (listar BDs subidas en esta instancia)",
1388
  "GET /schema/{id} (esquema resumido)",
1389
  "GET /preview/{id}/{t} (preview de tabla)",