Aurélie GABU commited on
Commit
f489a18
·
1 Parent(s): 2df3397

removing db connexion on HF

Browse files
App/database.py CHANGED
@@ -3,13 +3,20 @@ from dotenv import load_dotenv
3
  from sqlalchemy import create_engine
4
  from sqlalchemy.orm import sessionmaker, declarative_base
5
 
 
 
 
 
 
 
6
  load_dotenv()
7
 
8
  # Détection si on est en CI (GitHub Actions) ou en test
9
  IS_CI = os.getenv("CI") == "true"
10
  IS_PYTEST = "pytest" in os.getenv("PYTHONPATH", "") or os.getenv("PYTEST_CURRENT_TEST") is not None
 
11
 
12
- SKIP_DB = IS_CI or IS_PYTEST
13
 
14
  DB_USER = os.getenv("DB_USER", "postgres")
15
  DB_PASSWORD = os.getenv("DB_PASSWORD", "password")
@@ -19,7 +26,7 @@ DB_NAME = os.getenv("DB_NAME", "test_db")
19
 
20
  DATABASE_URL = (f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}"f"@{DB_HOST}:{DB_PORT}/{DB_NAME}")
21
 
22
- Base = declarative_base()
23
 
24
  if not SKIP_DB:
25
  engine = create_engine(DATABASE_URL)
 
3
  from sqlalchemy import create_engine
4
  from sqlalchemy.orm import sessionmaker, declarative_base
5
 
6
+ # Tentative d'import SQLAlchemy uniquement si disponible
7
+ try:
8
+ SQLALCHEMY_AVAILABLE = True
9
+ except ModuleNotFoundError:
10
+ SQLALCHEMY_AVAILABLE = False
11
+
12
  load_dotenv()
13
 
14
  # Détection si on est en CI (GitHub Actions) ou en test
15
  IS_CI = os.getenv("CI") == "true"
16
  IS_PYTEST = "pytest" in os.getenv("PYTHONPATH", "") or os.getenv("PYTEST_CURRENT_TEST") is not None
17
+ IS_HF = os.getenv("SPACE_ID") is not None # Hugging Face
18
 
19
+ SKIP_DB = IS_CI or IS_PYTEST or IS_HF or not SQLALCHEMY_AVAILABLE
20
 
21
  DB_USER = os.getenv("DB_USER", "postgres")
22
  DB_PASSWORD = os.getenv("DB_PASSWORD", "password")
 
26
 
27
  DATABASE_URL = (f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}"f"@{DB_HOST}:{DB_PORT}/{DB_NAME}")
28
 
29
+ Base = declarative_base() if SQLALCHEMY_AVAILABLE else None
30
 
31
  if not SKIP_DB:
32
  engine = create_engine(DATABASE_URL)
App/predict.py CHANGED
@@ -4,8 +4,16 @@ from App.schemas import EmployeeFeatures
4
  import json
5
  from pathlib import Path
6
  from huggingface_hub import hf_hub_download
7
- from sqlalchemy.orm import Session
8
- from App.database import SessionLocal
 
 
 
 
 
 
 
 
9
  from App.model import Input, Predictions
10
 
11
  MODEL_REPO = "Diaure/xgb_model"
@@ -16,7 +24,6 @@ classes_mapping = None
16
  Features = list(EmployeeFeatures.model_fields.keys())
17
 
18
 
19
-
20
  # Chargement des fichiers: fonction pour charger le modèle, le mapping afin de permettre à l'API de démarrer m^me si les éléments ne sont pas présents
21
  def files_load():
22
  global model, classes_mapping
@@ -46,7 +53,10 @@ def predict_employee(data: dict):
46
  pred = model.predict(df)[0]
47
  proba = model.predict_proba(df)[0][1]
48
 
49
- db: Session = SessionLocal() if SessionLocal is not None else None
 
 
 
50
 
51
  if db is not None:
52
  try:
@@ -62,7 +72,7 @@ def predict_employee(data: dict):
62
  db.commit()
63
 
64
  except Exception as e:
65
- print("🔥 ERREUR DB :", e)
66
  raise e
67
 
68
  finally:
 
4
  import json
5
  from pathlib import Path
6
  from huggingface_hub import hf_hub_download
7
+
8
+
9
+ # Import SQLAlchemy uniquement si disponible
10
+ try:
11
+ from sqlalchemy.orm import Session
12
+ SQLALCHEMY_AVAILABLE = True
13
+ except ModuleNotFoundError:
14
+ SQLALCHEMY_AVAILABLE = False
15
+
16
+ from App.database import SessionLocal
17
  from App.model import Input, Predictions
18
 
19
  MODEL_REPO = "Diaure/xgb_model"
 
24
  Features = list(EmployeeFeatures.model_fields.keys())
25
 
26
 
 
27
  # Chargement des fichiers: fonction pour charger le modèle, le mapping afin de permettre à l'API de démarrer m^me si les éléments ne sont pas présents
28
  def files_load():
29
  global model, classes_mapping
 
53
  pred = model.predict(df)[0]
54
  proba = model.predict_proba(df)[0][1]
55
 
56
+ # DB désactivée si SQLAlchemy indisponible ou SessionLocal = None
57
+ if SQLALCHEMY_AVAILABLE and SessionLocal is not None:
58
+ db: Session = SessionLocal()
59
+ else: db = None
60
 
61
  if db is not None:
62
  try:
 
72
  db.commit()
73
 
74
  except Exception as e:
75
+ print("ERREUR DB:", e)
76
  raise e
77
 
78
  finally:
requirements.txt CHANGED
@@ -12,5 +12,4 @@ joblib==1.4.2
12
  pandas==2.2.2
13
  scikit-learn==1.4.2
14
  xgboost ==2.0.3
15
- huggingface-hub ==1.3.1
16
  python-dotenv ==1.2.1
 
12
  pandas==2.2.2
13
  scikit-learn==1.4.2
14
  xgboost ==2.0.3
 
15
  python-dotenv ==1.2.1
scripts/create_tables.py CHANGED
@@ -1,6 +1,9 @@
1
- from App.database import engine
2
- from App.database import Base
3
 
4
- Base.metadata.create_all(bind=engine)
5
 
6
- print("Tables créées avec succès")
 
 
 
 
 
 
1
+ from App.database import engine, Base
 
2
 
 
3
 
4
+ Base.metadata.create_all(bind=engine)
5
+ if engine is None:
6
+ print("DB désactivée : aucune table créée.")
7
+ else:
8
+ Base.metadata.create_all(bind=engine)
9
+ print("Tables créées avec succès")