DavidJyes commited on
Commit
e852f01
·
verified ·
1 Parent(s): d23add7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -149
app.py CHANGED
@@ -1,149 +1 @@
1
-
2
- from fastapi import FastAPI, HTTPException
3
- from pydantic import BaseModel, Field
4
- import joblib
5
- import json
6
- import numpy as np
7
- from datetime import datetime
8
- from typing import Optional
9
-
10
- # Initialisation FastAPI
11
- app = FastAPI(
12
- title="API Détection de Fraude",
13
- description="API de détection de fraude dans les transactions bancaires",
14
- version="1.0.0"
15
- )
16
-
17
- # Chargement du modèle et des encodeurs
18
- model = joblib.load('fraud_model.pkl')
19
- le_category = joblib.load('le_category.pkl')
20
- le_gender = joblib.load('le_gender.pkl')
21
- le_state = joblib.load('le_state.pkl')
22
-
23
- with open('mappings.json', 'r') as f:
24
- mappings = json.load(f)
25
-
26
- # Modèle de données
27
- class Transaction(BaseModel):
28
- amt: float = Field(..., description="Montant de la transaction", example=125.50)
29
- category: str = Field(..., description="Catégorie du marchand", example="personal_care")
30
- merchant: str = Field(..., description="Nom du marchand", example="fraud_Kirlin and Sons")
31
- trans_date_trans_time: str = Field(..., description="Date et heure", example="2020-06-21 12:14:25")
32
- gender: str = Field(..., description="Genre (M/F)", example="M")
33
- state: str = Field(..., description="État (code à 2 lettres)", example="SC")
34
- lat: float = Field(..., description="Latitude du client", example=33.9659)
35
- long: float = Field(..., description="Longitude du client", example=-80.9355)
36
- city_pop: int = Field(..., description="Population de la ville", example=333497)
37
- dob: str = Field(..., description="Date de naissance", example="1968-03-19")
38
- merch_lat: float = Field(..., description="Latitude du marchand", example=33.986391)
39
- merch_long: float = Field(..., description="Longitude du marchand", example=-81.200714)
40
- cc_num: Optional[str] = Field(None, description="Numéro de carte (optionnel)")
41
- avg_amt: Optional[float] = Field(50.0, description="Montant moyen historique")
42
- std_amt: Optional[float] = Field(30.0, description="Écart-type historique")
43
- nb_trans: Optional[int] = Field(10, description="Nombre de transactions historiques")
44
-
45
- class PredictionResponse(BaseModel):
46
- is_fraud: bool
47
- fraud_probability: float
48
- risk_level: str
49
- details: dict
50
-
51
- # Endpoint Health
52
- @app.get("/health")
53
- def health_check():
54
- return {
55
- "status": "healthy",
56
- "model_loaded": model is not None
57
- }
58
-
59
- # Endpoint catégories, états et genres
60
- @app.get("/categories")
61
- def get_categories():
62
- return {
63
- "categories": list(mappings['categories'].keys()),
64
- "states": list(mappings['states'].keys()),
65
- "genders": list(mappings['genders'].keys())
66
- }
67
-
68
- # Endpoint de prédiction
69
- @app.post("/predict", response_model=PredictionResponse)
70
- def predict_fraud(transaction: Transaction):
71
- try:
72
- # Parsing des dates
73
- trans_dt = datetime.strptime(transaction.trans_date_trans_time, "%Y-%m-%d %H:%M:%S")
74
- dob_dt = datetime.strptime(transaction.dob, "%Y-%m-%d")
75
-
76
- # Features temporelles
77
- hour = trans_dt.hour
78
- day_of_week = trans_dt.weekday()
79
- day = trans_dt.day
80
- month = trans_dt.month
81
- age = (trans_dt - dob_dt).days // 365
82
-
83
- # Distance client → marchand
84
- distance = np.sqrt(
85
- (transaction.lat - transaction.merch_lat)**2 +
86
- (transaction.long - transaction.merch_long)**2
87
- ) * 111
88
-
89
- # Vérification des catégories
90
- if transaction.category not in mappings['categories']:
91
- raise HTTPException(400, f"Catégorie inconnue: {transaction.category}")
92
- if transaction.gender not in mappings['genders']:
93
- raise HTTPException(400, f"Genre inconnu: {transaction.gender}")
94
- if transaction.state not in mappings['states']:
95
- raise HTTPException(400, f"État inconnu: {transaction.state}")
96
-
97
- category_encoded = mappings['categories'][transaction.category]
98
- gender_encoded = mappings['genders'][transaction.gender]
99
- state_encoded = mappings['states'][transaction.state]
100
-
101
- # Construction du vecteur de features
102
- features = np.array([[
103
- transaction.amt,
104
- hour,
105
- day_of_week,
106
- day,
107
- month,
108
- age,
109
- category_encoded,
110
- gender_encoded,
111
- state_encoded,
112
- transaction.lat,
113
- transaction.long,
114
- transaction.city_pop,
115
- distance,
116
- transaction.avg_amt,
117
- transaction.std_amt,
118
- transaction.nb_trans
119
- ]])
120
-
121
- # Prédiction
122
- fraud_proba = model.predict_proba(features)[0][1]
123
- is_fraud = fraud_proba > 0.5
124
-
125
- # Niveau de risque
126
- if fraud_proba < 0.3:
127
- risk_level = "Faible"
128
- elif fraud_proba < 0.7:
129
- risk_level = "Moyen"
130
- else:
131
- risk_level = "Élevé"
132
-
133
- return PredictionResponse(
134
- is_fraud=bool(is_fraud),
135
- fraud_probability=float(fraud_proba),
136
- risk_level=risk_level,
137
- details={
138
- "montant": transaction.amt,
139
- "categorie": transaction.category,
140
- "heure": hour,
141
- "age_client": age,
142
- "distance_km": round(distance, 2)
143
- }
144
- )
145
-
146
- except ValueError as e:
147
- raise HTTPException(status_code=400, detail=f"Erreur de format: {str(e)}")
148
- except Exception as e:
149
- raise HTTPException(status_code=500, detail=f"Erreur interne: {str(e)}")
 
1
+ from fastapi import FastAPI #, HTTPException from pydantic import BaseModel, Field import joblib import json import numpy as np from datetime import datetime from typing import Optional import os # Initialisation app = FastAPI( title="API Détection de Fraude", description="API de détection de fraude dans les transactions bancaires", version="1.0.0" ) # Chargement du modèle et des encodeurs model = joblib.load('fraud_model.pkl') le_category = joblib.load('le_category.pkl') le_gender = joblib.load('le_gender.pkl') le_state = joblib.load('le_state.pkl') with open('mappings.json', 'r') as f: mappings = json.load(f) # Modèle de données class Transaction(BaseModel): amt: float = Field(..., description="Montant de la transaction", example=125.50) category: str = Field(..., description="Catégorie du marchand", example="personal_care") merchant: str = Field(..., description="Nom du marchand", example="fraud_Kirlin and Sons") trans_date_trans_time: str = Field(..., description="Date et heure", example="2020-06-21 12:14:25") gender: str = Field(..., description="Genre (M/F)", example="M") state: str = Field(..., description="État (code à 2 lettres)", example="SC") lat: float = Field(..., description="Latitude du client", example=33.9659) long: float = Field(..., description="Longitude du client", example=-80.9355) city_pop: int = Field(..., description="Population de la ville", example=333497) dob: str = Field(..., description="Date de naissance", example="1968-03-19") merch_lat: float = Field(..., description="Latitude du marchand", example=33.986391) merch_long: float = Field(..., description="Longitude du marchand", example=-81.200714) cc_num: Optional[str] = Field(None, description="Numéro de carte (optionnel)") avg_amt: Optional[float] = Field(50.0, description="Montant moyen historique") std_amt: Optional[float] = Field(30.0, description="Écart-type historique") nb_trans: Optional[int] = Field(10, description="Nombre de transactions historiques") class PredictionResponse(BaseModel): is_fraud: bool fraud_probability: float risk_level: str details: dict # Routes @app.get("/") def read_root(): return { "message": "API de Détection de Fraude", "version": "1.0.0", "endpoints": { "/predict": "POST - Prédire une transaction", "/health": "GET - Statut de l'API", "/categories": "GET - Liste des catégories", "/docs": "GET - Documentation interactive" } } @app.get("/health") def health_check(): return { "status": "healthy"}, # "model_loaded": model is not None, #"timestamp": datetime.now().isoformat() # } @app.get("/categories") def get_categories(): return { "categories": list(mappings['categories'].keys()), "states": list(mappings['states'].keys()), "genders": list(mappings['genders'].keys()) } @app.post("/predict", response_model=PredictionResponse) def predict_fraud(transaction: Transaction): try: # Parsing des dates trans_dt = datetime.strptime(transaction.trans_date_trans_time, "%Y-%m-%d %H:%M:%S") dob_dt = datetime.strptime(transaction.dob, "%Y-%m-%d") # Features temporelles hour = trans_dt.hour day_of_week = trans_dt.weekday() day = trans_dt.day month = trans_dt.month age = (trans_dt - dob_dt).days // 365 # Distance distance = np.sqrt( (transaction.lat - transaction.merch_lat)**2 + (transaction.long - transaction.merch_long)**2 ) * 111 # Encodage if transaction.category not in mappings['categories']: raise HTTPException(400, f"Catégorie inconnue: {transaction.category}") if transaction.gender not in mappings['genders']: raise HTTPException(400, f"Genre inconnu: {transaction.gender}") if transaction.state not in mappings['states']: raise HTTPException(400, f"État inconnu: {transaction.state}") category_encoded = mappings['categories'][transaction.category] gender_encoded = mappings['genders'][transaction.gender] state_encoded = mappings['states'][transaction.state] # Construction du vecteur de features features = np.array([[ transaction.amt, hour, day_of_week, day, month, age, category_encoded, gender_encoded, state_encoded, transaction.lat, transaction.long, transaction.city_pop, distance, transaction.avg_amt, transaction.std_amt, transaction.nb_trans ]]) # Prédiction fraud_proba = model.predict_proba(features)[0][1] is_fraud = fraud_proba > 0.5 # Niveau de risque if fraud_proba < 0.3: risk_level = "Faible" elif fraud_proba < 0.7: risk_level = "Moyen" else: risk_level = "Élevé" return PredictionResponse( is_fraud=bool(is_fraud), fraud_probability=float(fraud_proba), risk_level=risk_level, details={ "montant": transaction.amt, "categorie": transaction.category, "heure": hour, "age_client": age, "distance_km": round(distance, 2) } ) except ValueError as e: raise HTTPException(status_code=400, detail=f"Erreur de format: {str(e)}") except Exception as e: raise HTTPException(status_code=500, detail=f"Erreur interne: {str(e)}") #if __name__ == "__main__": # import uvicorn # uvicorn.run(app, host="0.0.0.0", port=7860) # if __name__ == "__main__": # import gradio as gr # from app import predict_fraud, Transaction # si tu es déjà dans app.py, inutile de réimporter # # Wrapper pour adapter les inputs de Gradio au modèle FastAPI # def api_predict_wrapper( # amt, category, merchant, trans_date_trans_time, gender, state, # lat, long, city_pop, dob, merch_lat, merch_long, # cc_num=None, avg_amt=50, std_amt=30, nb_trans=10 # ): # tx = Transaction( # amt=amt, # category=category, # merchant=merchant, # trans_date_trans_time=trans_date_trans_time, # gender=gender, # state=state, # lat=lat, # long=long, # city_pop=city_pop, # dob=dob, # merch_lat=merch_lat, # merch_long=merch_long, # cc_num=cc_num, # avg_amt=avg_amt, # std_amt=std_amt, # nb_trans=nb_trans # ) # result = predict_fraud(tx) # return result.dict() # # Définition de l'interface Gradio # iface = gr.Interface( # fn=api_predict_wrapper, # inputs=[ # gr.Number(label="Montant"), # gr.Textbox(label="Catégorie"), # gr.Textbox(label="Marchand"), # gr.Textbox(label="Date/Heure"), # gr.Textbox(label="Genre"), # gr.Textbox(label="État"), # gr.Number(label="Latitude"), # gr.Number(label="Longitude"), # gr.Number(label="Population ville"), # gr.Textbox(label="Date de naissance"), # gr.Number(label="Lat march."), # gr.Number(label="Long march."), # ], # outputs=gr.JSON(label="Résultat") # ) # # Lancement du front Gradio # iface.launch()