Spaces:
Sleeping
Sleeping
Update app.py
Browse files20 unmodified lines2122232424252728262728293031323334353637388 unmodified lines474849505152533 unmodified lines575859586061626364626365666768696869707172731 unmodified line7576777879808182838485868720 unmodified lines required_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 matching row_values = [clean_column_name(str(val)) for val in df.iloc[i].values] # Check if all required columns are present in the row if all(col in row_values for col in required_columns): # Allow partial matching - consider a match if we have at least 80% of the required columns match_count = sum(1 for col in required_columns if col in row_values) if match_count / len(required_columns) >= 0.8: return i return -1
def process_uploaded_file(uploaded_file, required_columns): try: file_extension = uploaded_file.name.split('.')[-1].lower()8 unmodified lines st.error("Unsupported file format. Please upload a CSV or Excel file.") return None
# Find the header row header_row = find_header_row(df, required_columns)
if header_row == -1:3 unmodified lines return None
# Re-read the file with the correct header row uploaded_file.seek(0) # Reset file pointer uploaded_file.seek(0) # Reset file pointer before re-reading
if file_extension == 'csv': df = pd.read_csv(uploaded_file, header=header_row) else: df = pd.read_excel( uploaded_file, header=header_row, engine='openpyxl') df = pd.read_excel(uploaded_file, header=header_row, engine='openpyxl')
# Clean column names df.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.") st.write(f"Uploaded data has {df.shape[0]} rows and {df.shape[1]} columns.")
# Check for PID column if 'pid' not in df.columns:1 unmodified line return None
return df
except Exception as e: st.error(f"Error reading the file: {e}") return None
def predict_with_model(model, data, includes_preprocessor): if includes_preprocessor: return model.predict(data)app.py+11-8