# outlier_detection_gradio.py import pandas as pd import gradio as gr # Dummy dataset data = { "Order ID": [101, 102, 103, 104, 105, 106], "Customer Name": ["Alice", "Bob", "Charlie", "David", "Eva", "Frank"], "Product": ["Laptop", "Mouse", "Keyboard", "Monitor", "Printer", "Tablet"], "Quantity": [1, 2, 1, 3, 2, 50], # Notice 50 is an outlier "Price": [1000, 20, 50, 300, 150, 200] } df = pd.DataFrame(data) # Outlier detection function def detect_outliers(column_name): if column_name not in df.columns: return pd.DataFrame({"Error": [f"Column '{column_name}' does not exist."]}) col_data = df[column_name] # Using IQR method Q1 = col_data.quantile(0.25) Q3 = col_data.quantile(0.75) IQR = Q3 - Q1 lower_bound = Q1 - 1.5 * IQR upper_bound = Q3 + 1.5 * IQR # Flag outliers df_outliers = df.copy() df_outliers["Outlier"] = df_outliers[column_name].apply( lambda x: "Yes" if x < lower_bound or x > upper_bound else "No" ) return df_outliers # Gradio Interface iface = gr.Interface( fn=detect_outliers, inputs=gr.Textbox(label="Column Name for Outlier Detection", placeholder="e.g., Quantity"), outputs=gr.Dataframe(label="Outlier Detection Result"), title="Outlier Detection Demo", description="Detects outliers in a specified column using the IQR method." ) if __name__ == "__main__": iface.launch()