Aurélie GABU commited on
Commit
ff13a09
·
2 Parent(s): 2aa3e03 730f23d

Fix files conflicts

Browse files
App/database.py CHANGED
@@ -2,14 +2,29 @@ import os
2
  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 +34,11 @@ 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)
 
2
  from dotenv import load_dotenv
3
  from sqlalchemy import create_engine
4
  from sqlalchemy.orm import sessionmaker, declarative_base
5
+ <<<<<<< HEAD
6
+ =======
7
+
8
+ # Tentative d'import SQLAlchemy uniquement si disponible
9
+ try:
10
+ SQLALCHEMY_AVAILABLE = True
11
+ except ModuleNotFoundError:
12
+ SQLALCHEMY_AVAILABLE = False
13
+ >>>>>>> 730f23d8af257943c5e13570e1fab5a743e559e7
14
 
15
  load_dotenv()
16
 
17
  # Détection si on est en CI (GitHub Actions) ou en test
18
  IS_CI = os.getenv("CI") == "true"
19
  IS_PYTEST = "pytest" in os.getenv("PYTHONPATH", "") or os.getenv("PYTEST_CURRENT_TEST") is not None
20
+ <<<<<<< HEAD
21
 
22
  SKIP_DB = IS_CI or IS_PYTEST
23
+ =======
24
+ IS_HF = os.getenv("SPACE_ID") is not None # Hugging Face
25
+
26
+ SKIP_DB = IS_CI or IS_PYTEST or IS_HF or not SQLALCHEMY_AVAILABLE
27
+ >>>>>>> 730f23d8af257943c5e13570e1fab5a743e559e7
28
 
29
  DB_USER = os.getenv("DB_USER", "postgres")
30
  DB_PASSWORD = os.getenv("DB_PASSWORD", "password")
 
34
 
35
  DATABASE_URL = (f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}"f"@{DB_HOST}:{DB_PORT}/{DB_NAME}")
36
 
37
+ <<<<<<< HEAD
38
  Base = declarative_base()
39
+ =======
40
+ Base = declarative_base() if SQLALCHEMY_AVAILABLE else None
41
+ >>>>>>> 730f23d8af257943c5e13570e1fab5a743e559e7
42
 
43
  if not SKIP_DB:
44
  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,7 @@
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
+ if engine is None:
4
+ print("DB désactivée : aucune table créée.")
5
+ else:
6
+ Base.metadata.create_all(bind=engine)
7
+ print("Tables créées avec succès")