Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/__pycache__/__init__.cpython-312.pyc +0 -0
- app/__pycache__/database.cpython-312.pyc +0 -0
- app/__pycache__/database.cpython-313.pyc +0 -0
- app/__pycache__/feature_engineering.cpython-312.pyc +0 -0
- app/__pycache__/feature_engineering.cpython-313.pyc +0 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/__pycache__/main.cpython-313.pyc +0 -0
- app/__pycache__/models.cpython-312.pyc +0 -0
- app/__pycache__/models.cpython-313.pyc +0 -0
- app/database.py +5 -0
- app/main.py +76 -7
- app/models.py +34 -1
app/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (139 Bytes). View file
|
|
|
app/__pycache__/database.cpython-312.pyc
ADDED
|
Binary file (1.36 kB). View file
|
|
|
app/__pycache__/database.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/database.cpython-313.pyc and b/app/__pycache__/database.cpython-313.pyc differ
|
|
|
app/__pycache__/feature_engineering.cpython-312.pyc
ADDED
|
Binary file (1.15 kB). View file
|
|
|
app/__pycache__/feature_engineering.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/feature_engineering.cpython-313.pyc and b/app/__pycache__/feature_engineering.cpython-313.pyc differ
|
|
|
app/__pycache__/main.cpython-312.pyc
ADDED
|
Binary file (6.82 kB). View file
|
|
|
app/__pycache__/main.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-313.pyc and b/app/__pycache__/main.cpython-313.pyc differ
|
|
|
app/__pycache__/models.cpython-312.pyc
ADDED
|
Binary file (2.5 kB). View file
|
|
|
app/__pycache__/models.cpython-313.pyc
CHANGED
|
Binary files a/app/__pycache__/models.cpython-313.pyc and b/app/__pycache__/models.cpython-313.pyc differ
|
|
|
app/database.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
from sqlalchemy import create_engine
|
|
@@ -12,6 +14,8 @@ RUNNING_TESTS = os.getenv("ENV") == "test"
|
|
| 12 |
if RUNNING_IN_HF or RUNNING_TESTS:
|
| 13 |
# Use SQLite for HuggingFace and for tests
|
| 14 |
DATABASE_URL = "sqlite:///./futurisys.db"
|
|
|
|
|
|
|
| 15 |
else:
|
| 16 |
# Normal PostgreSQL configuration
|
| 17 |
DB_USER = os.getenv("DB_USER")
|
|
@@ -31,3 +35,4 @@ engine = create_engine(
|
|
| 31 |
|
| 32 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 33 |
Base = declarative_base()
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from sqlalchemy import create_engine
|
|
|
|
| 14 |
if RUNNING_IN_HF or RUNNING_TESTS:
|
| 15 |
# Use SQLite for HuggingFace and for tests
|
| 16 |
DATABASE_URL = "sqlite:///./futurisys.db"
|
| 17 |
+
print("DATABASE_URL =", DATABASE_URL)
|
| 18 |
+
|
| 19 |
else:
|
| 20 |
# Normal PostgreSQL configuration
|
| 21 |
DB_USER = os.getenv("DB_USER")
|
|
|
|
| 35 |
|
| 36 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 37 |
Base = declarative_base()
|
| 38 |
+
|
app/main.py
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
# app/main.py
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import Literal
|
| 5 |
import pandas as pd
|
| 6 |
from joblib import load
|
| 7 |
from app.database import Base, engine, SessionLocal
|
| 8 |
-
from app.models import Input, Output
|
| 9 |
import datetime
|
| 10 |
from app.feature_engineering import transform_fe
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
# Création automatique des tables
|
|
@@ -60,12 +64,14 @@ class PredictionRawData(BaseModel):
|
|
| 60 |
|
| 61 |
app = FastAPI(title="API Futurisys")
|
| 62 |
|
|
|
|
|
|
|
| 63 |
@app.get("/")
|
| 64 |
def read_root():
|
| 65 |
return {
|
| 66 |
"message": "Bienvenue dans l'API Futurisys",
|
| 67 |
"documentation": "/docs",
|
| 68 |
-
"info": "Accedez au Swagger : https://
|
| 69 |
|
| 70 |
}
|
| 71 |
|
|
@@ -88,10 +94,10 @@ def predict(data: PredictionRawData):
|
|
| 88 |
proba = pipeline.predict_proba(df)[0][1]
|
| 89 |
pred = bool(proba >= threshold)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
db = SessionLocal()
|
| 93 |
|
| 94 |
-
#
|
| 95 |
|
| 96 |
new_input = Input(
|
| 97 |
timestamp_input=datetime.datetime.now(),
|
|
@@ -102,18 +108,81 @@ def predict(data: PredictionRawData):
|
|
| 102 |
db.commit()
|
| 103 |
db.refresh(new_input)
|
| 104 |
|
| 105 |
-
#
|
| 106 |
new_output = Output(
|
| 107 |
id_input = new_input.id_input,
|
| 108 |
prediction = int(pred),
|
| 109 |
-
|
|
|
|
| 110 |
)
|
| 111 |
db.add(new_output)
|
| 112 |
db.commit()
|
| 113 |
-
#
|
| 114 |
|
| 115 |
return {
|
| 116 |
"probabilité": round(float(proba), 3),
|
| 117 |
"prédiction": pred
|
| 118 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app/main.py
|
| 2 |
+
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Literal
|
| 6 |
import pandas as pd
|
| 7 |
from joblib import load
|
| 8 |
from app.database import Base, engine, SessionLocal
|
| 9 |
+
from app.models import Input, Output, Employe
|
| 10 |
import datetime
|
| 11 |
from app.feature_engineering import transform_fe
|
| 12 |
+
from fastapi import Query
|
| 13 |
+
|
| 14 |
+
from app.database import DATABASE_URL
|
| 15 |
|
| 16 |
|
| 17 |
# Création automatique des tables
|
|
|
|
| 64 |
|
| 65 |
app = FastAPI(title="API Futurisys")
|
| 66 |
|
| 67 |
+
|
| 68 |
+
|
| 69 |
@app.get("/")
|
| 70 |
def read_root():
|
| 71 |
return {
|
| 72 |
"message": "Bienvenue dans l'API Futurisys",
|
| 73 |
"documentation": "/docs",
|
| 74 |
+
"info": "Accedez au Swagger : https://pcelia-futurisys-api.hf.space/docs"
|
| 75 |
|
| 76 |
}
|
| 77 |
|
|
|
|
| 94 |
proba = pipeline.predict_proba(df)[0][1]
|
| 95 |
pred = bool(proba >= threshold)
|
| 96 |
|
| 97 |
+
# Enregistrement dans la DB
|
| 98 |
db = SessionLocal()
|
| 99 |
|
| 100 |
+
# Input
|
| 101 |
|
| 102 |
new_input = Input(
|
| 103 |
timestamp_input=datetime.datetime.now(),
|
|
|
|
| 108 |
db.commit()
|
| 109 |
db.refresh(new_input)
|
| 110 |
|
| 111 |
+
# Output
|
| 112 |
new_output = Output(
|
| 113 |
id_input = new_input.id_input,
|
| 114 |
prediction = int(pred),
|
| 115 |
+
probabilite = float(proba)
|
| 116 |
+
|
| 117 |
)
|
| 118 |
db.add(new_output)
|
| 119 |
db.commit()
|
| 120 |
+
# FIN enregistrement
|
| 121 |
|
| 122 |
return {
|
| 123 |
"probabilité": round(float(proba), 3),
|
| 124 |
"prédiction": pred
|
| 125 |
}
|
| 126 |
+
from sqlalchemy import text
|
| 127 |
+
|
| 128 |
+
@app.get("/test_ids")
|
| 129 |
+
def test_ids():
|
| 130 |
+
db = SessionLocal()
|
| 131 |
+
from sqlalchemy import text
|
| 132 |
+
result = db.execute(text("SELECT employee_id FROM employes LIMIT 20;")).fetchall()
|
| 133 |
+
return {"ids": result}
|
| 134 |
+
|
| 135 |
+
|
| 136 |
|
| 137 |
+
import traceback
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
@app.post("/predict_from_db_employe")
|
| 141 |
+
def predict_from_db_employe(
|
| 142 |
+
employee_id: int = Query(..., ge=1)
|
| 143 |
+
):
|
| 144 |
+
try:
|
| 145 |
+
db = SessionLocal()
|
| 146 |
+
|
| 147 |
+
employe = db.query(Employe).filter(Employe.employee_id == employee_id).first()
|
| 148 |
+
if not employe:
|
| 149 |
+
return {"message": f"Aucun employé trouvé avec l'id {employee_id}"}
|
| 150 |
+
|
| 151 |
+
data = employe.__dict__.copy()
|
| 152 |
+
data.pop("_sa_instance_state", None)
|
| 153 |
+
|
| 154 |
+
df = pd.DataFrame([data])
|
| 155 |
+
df = transform_fe(df)
|
| 156 |
+
|
| 157 |
+
proba = pipeline.predict_proba(df)[0][1]
|
| 158 |
+
pred = proba >= threshold
|
| 159 |
+
|
| 160 |
+
new_input = Input(
|
| 161 |
+
timestamp_input=datetime.datetime.now(),
|
| 162 |
+
employee_id=employee_id,
|
| 163 |
+
age=employe.age
|
| 164 |
+
)
|
| 165 |
+
db.add(new_input)
|
| 166 |
+
db.commit()
|
| 167 |
+
db.refresh(new_input)
|
| 168 |
+
|
| 169 |
+
new_output = Output(
|
| 170 |
+
id_input=new_input.id_input,
|
| 171 |
+
prediction=int(pred),
|
| 172 |
+
probabilite=float(proba)
|
| 173 |
+
|
| 174 |
+
)
|
| 175 |
+
db.add(new_output)
|
| 176 |
+
db.commit()
|
| 177 |
+
|
| 178 |
+
return {
|
| 179 |
+
"params": data,
|
| 180 |
+
"probabilité": round(float(proba), 3),
|
| 181 |
+
"prédiction": bool(pred)
|
| 182 |
+
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
except Exception as e:
|
| 186 |
+
print("ERROR PREDICT_FROM_DB:", e)
|
| 187 |
+
traceback.print_exc()
|
| 188 |
+
return {"error": str(e)}
|
app/models.py
CHANGED
|
@@ -17,6 +17,39 @@ class Output(Base):
|
|
| 17 |
id_output = Column(Integer, primary_key=True, index=True)
|
| 18 |
id_input = Column(Integer, ForeignKey("inputs.id_input"))
|
| 19 |
prediction = Column(Integer)
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
input = relationship("Input", back_populates="outputs")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
id_output = Column(Integer, primary_key=True, index=True)
|
| 18 |
id_input = Column(Integer, ForeignKey("inputs.id_input"))
|
| 19 |
prediction = Column(Integer)
|
| 20 |
+
probabilite = Column("probabilite", Float)
|
| 21 |
+
|
| 22 |
|
| 23 |
input = relationship("Input", back_populates="outputs")
|
| 24 |
+
|
| 25 |
+
class Employe(Base):
|
| 26 |
+
__tablename__ = "employes"
|
| 27 |
+
|
| 28 |
+
employee_id = Column(Integer, primary_key=True)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
age = Column(Integer)
|
| 33 |
+
revenu_mensuel = Column(Float)
|
| 34 |
+
statut_marital = Column(String)
|
| 35 |
+
departement = Column(String)
|
| 36 |
+
poste = Column(String)
|
| 37 |
+
|
| 38 |
+
annee_experience_totale = Column(Integer)
|
| 39 |
+
annees_dans_l_entreprise = Column(Integer)
|
| 40 |
+
annees_dans_le_poste_actuel = Column(Integer)
|
| 41 |
+
|
| 42 |
+
satisfaction_employee_environnement = Column(Integer)
|
| 43 |
+
note_evaluation_precedente = Column(Float)
|
| 44 |
+
satisfaction_employee_nature_travail = Column(Integer)
|
| 45 |
+
satisfaction_employee_equipe = Column(Integer)
|
| 46 |
+
satisfaction_employee_equilibre_pro_perso = Column(Integer)
|
| 47 |
+
note_evaluation_actuelle = Column(Float)
|
| 48 |
+
|
| 49 |
+
heure_supplementaires = Column(String)
|
| 50 |
+
augementation_salaire_precedente = Column(Float)
|
| 51 |
+
nombre_participation_pee = Column(Integer)
|
| 52 |
+
frequence_deplacement = Column(String)
|
| 53 |
+
|
| 54 |
+
annes_sous_responsable_actuel = Column(Integer)
|
| 55 |
+
|