Spaces:
Runtime error
Runtime error
Commit Β·
3836bc2
1
Parent(s): 880c9a5
Deploy PromptPrepML - Standalone Gradio App with ML Preprocessing
Browse filesFiles changed (3)
- README.md+34-6
- app.pyAdded+279
- requirements.txtAdded+4
12345678910111213---title: Auto ML Preprocessingemoji: πcolorFrom: greencolorTo: indigosdk: gradiosdk_version: 6.9.0app_file: app.pypinned: falseshort_description: ' AI-powered system that Automates Ml preprocessing'---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526272829303132333435363738394041---title: PromptPrepMLemoji: π€colorFrom: bluecolorTo: indigosdk: gradiosdk_version: 4.0.0app_file: app.pypinned: falselicense: mit---# PromptPrepMLAI-Powered Machine Learning Data Preprocessing AssistantUpload your dataset and describe your preprocessing needs in natural language. Our intelligent system will automatically:- Detect and remove identifier columns- Extract date features- Handle categorical encoding- Scale numeric features- Generate ML-ready datasets- Create reusable pipelines## π Features- **π§ Intelligent Preprocessing**: Smart column detection and processing- **π Automated EDA**: Comprehensive data analysis reports- **π§ Smart Feature Engineering**: Advanced feature extraction- **βοΈ Reusable Pipelines**: Scikit-learn pipelines for production- **π Clean Outputs**: ML-ready train/test datasets## π Usage1. Upload your CSV dataset2. Describe your preprocessing needs in natural language3. Click "Process Dataset"4. Download your ML-ready results## π οΈ Tech Stack- **Backend**: Python, FastAPI, scikit-learn- **Frontend**: Gradio- **EDA**: ydata-profiling- **ML**: pandas, numpy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279import gradio as grimport pandas as pdimport numpy as npfrom sklearn.impute import SimpleImputerfrom sklearn.preprocessing import StandardScaler, OneHotEncoderfrom sklearn.feature_selection import VarianceThresholdfrom sklearn.compose import ColumnTransformerfrom sklearn.pipeline import Pipelinefrom sklearn.model_selection import train_test_splitimport ioimport warningswarnings.filterwarnings('ignore')
class StandalonePreprocessor: def __init__(self): self.pipeline = None self.feature_names = [] self.analysis = {} def analyze_columns(self, df): """Analyze dataset columns""" analysis = { 'identifiers': [], 'dates': [], 'text_features': [], 'categorical_low_cardinality': [], 'categorical_high_cardinality': [], 'numeric': [] } for col in df.columns: col_lower = col.lower() # Identifier detection is_identifier = ( any(keyword in col_lower for keyword in ['id', 'index', 'uuid', 'key']) and (df[col].nunique() / len(df) > 0.8) ) if is_identifier: analysis['identifiers'].append(col) continue # Date detection if df[col].dtype == 'object': try: pd.to_datetime(df[col].dropna().head(10)) analysis['dates'].append(col) continue except: pass # Text feature detection text_keywords = ['name', 'email', 'phone', 'website', 'address', 'description'] if any(keyword in col_lower for keyword in text_keywords): analysis['text_features'].append(col) continue # Categorical vs Numeric if df[col].dtype == 'object': unique_ratio = df[col].nunique() / len(df) if unique_ratio > 0.5: analysis['categorical_high_cardinality'].append(col) else: analysis['categorical_low_cardinality'].append(col) else: analysis['numeric'].append(col) return analysis def extract_date_features(self, df, date_cols): """Extract features from date columns""" df_processed = df.copy() for col in date_cols: try: dates = pd.to_datetime(df_processed[col]) df_processed[f'{col}_year'] = dates.dt.year df_processed[f'{col}_month'] = dates.dt.month df_processed[f'{col}_day'] = dates.dt.day df_processed[f'{col}_weekday'] = dates.dt.weekday df_processed.drop(col, axis=1, inplace=True) except: pass return df_processed def process(self, df): """Main processing function""" # Step 1: Analyze columns self.analysis = self.analyze_columns(df) # Step 2: Remove unwanted columns columns_to_drop = ( self.analysis['identifiers'] + self.analysis['text_features'] + self.analysis['categorical_high_cardinality'] ) df_clean = df.drop(columns=columns_to_drop, errors='ignore') # Step 3: Extract date features if self.analysis['dates']: df_clean = self.extract_date_features(df_clean, self.analysis['dates']) # Step 4: Create preprocessing pipeline numeric_features = df_clean.select_dtypes(include=[np.number]).columns.tolist() categorical_features = df_clean.select_dtypes(include=['object']).columns.tolist() preprocessor = ColumnTransformer( transformers=[ ('numeric', Pipeline([ ('imputer', SimpleImputer(strategy='median')), ('scaler', StandardScaler()) ]), numeric_features), ('categorical', Pipeline([ ('imputer', SimpleImputer(strategy='most_frequent')), ('encoder', OneHotEncoder(handle_unknown='ignore', sparse_output=False)) ]), categorical_features) ] ) # Step 5: Create full pipeline self.pipeline = Pipeline([ ('preprocessor', preprocessor), ('feature_selector', VarianceThreshold(threshold=0.01)) ]) # Step 6: Fit and transform processed_data = self.pipeline.fit_transform(df_clean) # Step 7: Get feature names try: feature_names = [] if numeric_features: feature_names.extend([f'numeric__{f}' for f in numeric_features]) if categorical_features: encoder = self.pipeline.named_steps['preprocessor'].named_transformers_['categorical'].named_steps['encoder'] cat_names = encoder.get_feature_names_out(categorical_features) feature_names.extend([f'categorical__{name}' for name in cat_names]) self.feature_names = feature_names[:processed_data.shape[1]] except: self.feature_names = [f'feature_{i}' for i in range(processed_data.shape[1])] # Step 8: Create processed DataFrame processed_df = pd.DataFrame(processed_data, columns=self.feature_names) return processed_df def split_data(self, df): """Split dataset into train and test""" train_df, test_df = train_test_split(df, test_size=0.2, random_state=42) return train_df, test_df
# Global preprocessorpreprocessor = StandalonePreprocessor()
def process_dataset(file, prompt): if file is None: return "Please upload a dataset", None, None, None, None, "" try: # Read uploaded file file_content = file.read() df = pd.read_csv(io.BytesIO(file_content)) # Process dataset processed_df = preprocessor.process(df) # Split dataset train_df, test_df = preprocessor.split_data(processed_df) # Create summary analysis = preprocessor.analysis summary = f""" ## β
**Processing Complete!** ### π **Dataset Information** - **Original Shape**: {df.shape} - **Processed Shape**: {processed_df.shape} - **Training Set**: {train_df.shape} - **Test Set**: {test_df.shape} ### π **Column Analysis** - **π― Identifiers Removed**: {len(analysis['identifiers'])} columns - **π Text Features Removed**: {len(analysis['text_features'])} columns - **π
Date Columns Processed**: {len(analysis['dates'])} columns - **π·οΈ Low Cardinality Encoded**: {len(analysis['categorical_low_cardinality'])} columns - **π² High Cardinality Dropped**: {len(analysis['categorical_high_cardinality'])} columns - **π’ Numeric Features**: {len(analysis['numeric'])} columns ### ποΈ **Dropped Columns** {', '.join(analysis['identifiers'] + analysis['text_features'] + analysis['categorical_high_cardinality']) if analysis['identifiers'] + analysis['text_features'] + analysis['categorical_high_cardinality'] else 'None'} ### π **Processing Steps Applied** 1. β
Identifier column detection and removal 2. β
Text feature detection and removal 3. β
Date feature extraction (year, month, day, weekday) 4. β
Missing value imputation 5. β
Categorical encoding (one-hot) 6. β
Numeric feature scaling 7. β
Low-variance feature removal 8. β
Train/test split (80/20) ### π **Files Ready for Download** - Processed dataset (clean, ML-ready) - Training set (80% of data) - Test set (20% of data) """ # Convert DataFrames to CSV for download processed_csv = processed_df.to_csv(index=False).encode('utf-8') train_csv = train_df.to_csv(index=False).encode('utf-8') test_csv = test_df.to_csv(index=False).encode('utf-8') return summary, processed_csv, train_csv, test_csv, processed_df.head(10), "β
Processing completed successfully!" except Exception as e: return f"β Error: {str(e)}", None, None, None, None, f"β Processing failed: {str(e)}"
# Create Gradio interfacewith gr.Blocks(title="PromptPrepML", theme=gr.themes.Soft()) as demo: gr.Markdown("# π€ PromptPrepML") gr.Markdown("AI-Powered Machine Learning Data Preprocessing Assistant") gr.Markdown("Upload your dataset and get ML-ready results in seconds! π") with gr.Row(): with gr.Column(scale=1): file_input = gr.File(label="π Upload CSV Dataset", file_types=[".csv"]) prompt_input = gr.Textbox( label="π¬ Processing Instructions", value="Prepare this dataset for machine learning. Handle missing values, remove identifier columns, extract date features, encode categorical variables, and scale numeric features.", lines=3 ) process_btn = gr.Button("π Process Dataset", variant="primary", size="lg") with gr.Column(scale=2): output_summary = gr.Markdown(label="π Results Summary") status_output = gr.Textbox(label="π Status", interactive=False) gr.Markdown("## π Preview of Processed Dataset") preview_output = gr.Dataframe(label="π Dataset Preview", max_rows=10) gr.Markdown("## π₯ Download Files") with gr.Row(): processed_download = gr.File(label="π Processed Dataset") train_download = gr.File(label="π Training Set") test_download = gr.File(label="π§ͺ Test Set") # Event handlers process_btn.click( fn=process_dataset, inputs=[file_input, prompt_input], outputs=[output_summary, processed_download, train_download, test_download, preview_output, status_output] ) gr.Markdown("## π Instructions") gr.Markdown(""" 1. π **Upload your CSV dataset** (any size) 2. π¬ **Describe your preprocessing needs** (or use default) 3. π **Click "Process Dataset"** 4. π₯ **Download your ML-ready results** 5. π **Use for machine learning!** ### π§ **Intelligent Features** - **Automatic identifier detection** and removal - **Smart date feature extraction** - **Text feature handling** - **Categorical encoding** for low-cardinality features - **High cardinality handling** - **Missing value imputation** - **Feature scaling** - **Train/test splitting** """)
# Launch the appif __name__ == "__main__": demo.launch()1234gradiopandasnumpyscikit-learnREADME.md+34-6
app.pyAdded+279
requirements.txtAdded+4