fgh
Browse files- src/app.py +12 -31
src/app.py
CHANGED
|
@@ -8,46 +8,27 @@ import boto3
|
|
| 8 |
from io import StringIO
|
| 9 |
import numpy as np
|
| 10 |
from botocore.config import Config
|
|
|
|
| 11 |
import shap
|
| 12 |
from sklearn.linear_model import LinearRegression
|
| 13 |
from langchain_mistralai import ChatMistralAI
|
| 14 |
from langchain_core.output_parsers import StrOutputParser
|
| 15 |
|
| 16 |
-
# Répertoire de cache local -> /tmp (écriture autorisée sur Hugging Face Spaces)
|
| 17 |
-
CACHE_DIR = "/tmp/cache"
|
| 18 |
-
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 19 |
|
| 20 |
# Initialisation client S3
|
| 21 |
-
s3 = boto3.client(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
if os.path.exists(cache_path) and not force_download:
|
| 27 |
-
print(f"[CACHE] Lecture locale : {cache_path}")
|
| 28 |
-
# Laisse pandas inférer le séparateur aussi en local
|
| 29 |
-
return pd.read_csv(cache_path, encoding=encoding, sep=None, engine="python")
|
| 30 |
-
|
| 31 |
-
print(f"[S3] Téléchargement depuis s3://{bucket}/{key}")
|
| 32 |
-
obj = s3.get_object(Bucket=bucket, Key=key)
|
| 33 |
-
raw = obj["Body"].read().decode(encoding)
|
| 34 |
-
|
| 35 |
-
# Inférence du séparateur
|
| 36 |
-
df = pd.read_csv(StringIO(raw), sep=None, engine="python")
|
| 37 |
-
|
| 38 |
-
# Sauvegarde cache en CSV standard (virgule)
|
| 39 |
-
df.to_csv(cache_path, index=False, encoding=encoding)
|
| 40 |
-
print(f"[CACHE] Sauvegardé : {cache_path}")
|
| 41 |
-
return df
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# -------- Utilisation --------
|
| 46 |
-
bucket = "mygeodechetbuckets3"
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
print("df (dummies) :", df.shape)
|
| 52 |
print("observed_df :", observed_df.shape)
|
| 53 |
#df = pd.read_csv("https://geodechet-s3-certif.s3.eu-west-3.amazonaws.com/df_dummies_2019.csv").drop(columns=["Unnamed: 0"], errors="ignore")
|
|
|
|
| 8 |
from io import StringIO
|
| 9 |
import numpy as np
|
| 10 |
from botocore.config import Config
|
| 11 |
+
from botocore import UNSIGNED
|
| 12 |
import shap
|
| 13 |
from sklearn.linear_model import LinearRegression
|
| 14 |
from langchain_mistralai import ChatMistralAI
|
| 15 |
from langchain_core.output_parsers import StrOutputParser
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Initialisation client S3
|
| 19 |
+
s3 = boto3.client(
|
| 20 |
+
"s3",
|
| 21 |
+
region_name="eu-west-3",
|
| 22 |
+
config=Config(signature_version=UNSIGNED)
|
| 23 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# --- df_dummies_2019.csv : UTF-8-SIG + virgule ---
|
| 26 |
+
obj = s3.get_object(Bucket="mygeodechetbuckets3", Key="df_dummies_2019.csv")
|
| 27 |
+
df = pd.read_csv(StringIO(obj["Body"].read().decode("utf-8-sig")), sep=",")
|
| 28 |
|
| 29 |
+
# --- data_wip_v5.csv : latin1 + point-virgule ---
|
| 30 |
+
obj2 = s3.get_object(Bucket="mygeodechetbuckets3", Key="data_wip_v5.csv")
|
| 31 |
+
observed_df = pd.read_csv(StringIO(obj2["Body"].read().decode("latin-1")), sep=";")
|
| 32 |
print("df (dummies) :", df.shape)
|
| 33 |
print("observed_df :", observed_df.shape)
|
| 34 |
#df = pd.read_csv("https://geodechet-s3-certif.s3.eu-west-3.amazonaws.com/df_dummies_2019.csv").drop(columns=["Unnamed: 0"], errors="ignore")
|