Spaces:
Sleeping
Sleeping
Update app.py
Browse files20 unmodified lines21222324252627282930313233343536378 unmodified lines4647484950513 unmodified lines5556575859606162636465666768697071721 unmodified line747576777879808182838420 unmodified linesrequired_columns = [clean_column_name(col) for col in required_columns]for i in range(min(max_rows_to_check, len(df))):# Clean up each row's values for matchingrow_values = [clean_column_name(str(val)) for val in df.iloc[i].values]# Check if all required columns are present in the rowif all(col in row_values for col in required_columns):return ireturn -1def process_uploaded_file(uploaded_file, required_columns):try:file_extension = uploaded_file.name.split('.')[-1].lower()8 unmodified linesst.error("Unsupported file format. Please upload a CSV or Excel file.")return Noneheader_row = find_header_row(df, required_columns)if header_row == -1:3 unmodified linesreturn None# Re-read the file with the correct header rowuploaded_file.seek(0) # Reset file pointerif file_extension == 'csv':df = pd.read_csv(uploaded_file, header=header_row)else:df = pd.read_excel(uploaded_file, header=header_row, engine='openpyxl')# Clean column namesdf.columns = [clean_column_name(col) for col in df.columns]st.write(f"Uploaded data has {df.shape[0]} rows and {df.shape[1]} columns.")# Check for PID columnif 'pid' not in df.columns:1 unmodified linereturn Nonereturn dfexcept Exception as e:st.error(f"Error reading the file: {e}")return Nonedef predict_with_model(model, data, includes_preprocessor):if includes_preprocessor:return model.predict(data)20 unmodified lines2122232425262728293031323334353637388 unmodified lines474849505152533 unmodified lines57585960616263646566676869707172731 unmodified line7576777879808182838485868720 unmodified linesrequired_columns = [clean_column_name(col) for col in required_columns]for i in range(min(max_rows_to_check, len(df))):row_values = [clean_column_name(str(val)) for val in df.iloc[i].values]# Allow partial matching - consider a match if we have at least 80% of the required columnsmatch_count = sum(1 for col in required_columns if col in row_values)if match_count / len(required_columns) >= 0.8:return ireturn -1def process_uploaded_file(uploaded_file, required_columns):try:file_extension = uploaded_file.name.split('.')[-1].lower()8 unmodified linesst.error("Unsupported file format. Please upload a CSV or Excel file.")return None# Find the header rowheader_row = find_header_row(df, required_columns)if header_row == -1:3 unmodified linesreturn None# Re-read the file with the correct header rowuploaded_file.seek(0) # Reset file pointer before re-readingif file_extension == 'csv':df = pd.read_csv(uploaded_file, header=header_row)else:df = pd.read_excel(uploaded_file, header=header_row, engine='openpyxl')# Clean column namesdf.columns = [clean_column_name(col) for col in df.columns]st.write(f"Uploaded data has {df.shape[0]} rows and {df.shape[1]} columns.")# Check for PID columnif 'pid' not in df.columns:1 unmodified linereturn Nonereturn dfexcept Exception as e:st.error(f"Error reading the file: {e}")return Nonedef predict_with_model(model, data, includes_preprocessor):if includes_preprocessor:return model.predict(data)
app.py+11-8