Spaces:
Runtime error
Runtime error
| from fastapi import HTTPException, UploadFile | |
| import pandas as pd | |
| import io | |
| # Colonnes requises | |
| required_columns = [ | |
| "Header_Length", "Protocol Type", "Time_To_Live", "Rate", | |
| "fin_flag_number", "syn_flag_number", "rst_flag_number", | |
| "psh_flag_number", "ack_flag_number", "ece_flag_number", | |
| "cwr_flag_number", "ack_count", "syn_count", "fin_count", | |
| "rst_count", "TCP", "UDP", "Tot sum", "Min", "Max", "AVG", | |
| "Std", "Tot size", "IAT", "Number", "Variance" | |
| ] | |
| def file_verification(file: UploadFile) -> pd.DataFrame: | |
| """ | |
| Vérifie l'extension et les colonnes du fichier CSV, puis retourne un DataFrame valide. | |
| Lève une HTTPException descriptive en cas d'erreur. | |
| """ | |
| try: | |
| print("start verifying file extension") | |
| if not file.filename.endswith(".csv"): | |
| raise HTTPException(status_code=400, detail="Le fichier doit être un CSV") | |
| print("Correct file extension") | |
| # Lecture du CSV depuis le contenu binaire | |
| contents = file.file.read() | |
| data = pd.read_csv(io.BytesIO(contents)) | |
| print("start verifying required columns") | |
| # Vérification des colonnes | |
| missing_cols = [col for col in required_columns if col not in data.columns] | |
| if missing_cols: | |
| raise HTTPException( | |
| status_code=400, | |
| detail=f"Colonnes manquantes dans le CSV: {', '.join(missing_cols)}" | |
| ) | |
| print("file verification successful") | |
| return data | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Erreur de lecture du fichier: {e}") | |