import tempfile import gradio as gr import pandas as pd from src.blocking import generate_blocks, count_comparisons from src.hashing_dedup import detect_exact_duplicates from src.similarity_dedup import detect_near_duplicates from src.preprocess import preprocess_data def process_data(file_obj, method): if file_obj is None: return "Please upload a CSV file before running.", None, None try: df_raw = pd.read_csv(file_obj.name, dtype=str, keep_default_na=False) df_preprocessed = preprocess_data(df_raw) if method == "Hashing": df_result = detect_exact_duplicates(df_preprocessed) number_of_duplicate_records = df_result['is_duplicate'].sum() if 'is_duplicate' in df_result.columns else 0 summary_msg = ( f" Hashing completed successfully!\n" f"- Number of duplicate records detected: {number_of_duplicate_records}\n" f"- Total number of rows returned: {len(df_result)}" ) elif method == "Similarity": df_blocked = generate_blocks(df_preprocessed, blocking_columns=["surname_clean"], prefix_len=3) df_result = detect_near_duplicates(df_blocked) stats = count_comparisons(df_blocked) reduce_load = stats['reduction_ratio'] * 100 number_of_duplicate_records = df_result['is_duplicate'].sum() if 'is_duplicate' in df_result.columns else 0 summary_msg = ( f" Blocking & Similarity ran successfully!\n" f"- Reduction rate in calculations due to Blocking: {reduce_load:.2f}%\n" f"- Number of duplicate records detected: {number_of_duplicate_records}\n" f"- Total number of rows returned: {len(df_result)}" ) else: return " Invalid method.", None, None if 'is_duplicate' in df_result.columns: df_cleaned = df_result[df_result['is_duplicate'] == False] else: df_cleaned = df_result temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv") df_cleaned.to_csv(temp_file.name, index=False) return summary_msg, df_cleaned.head(15), temp_file.name except Exception as e: return f" Error processing: {str(e)}", None, None with gr.Blocks(title="Data Deduplication App") as demo: gr.Markdown("# Data Deduplication System ") with gr.Row(): with gr.Column(scale=1): file_input = gr.File(label="1. Upload dataset (CSV)", file_types=[".csv"]) method_input = gr.Radio( choices=["Hashing", "Similarity"], label="2. Select a method", value="Hashing" ) submit_btn = gr.Button(" Run the algorithm", variant="primary") with gr.Column(scale=2): text_output = gr.Textbox(label="System status", lines=5, interactive=False) file_output = gr.File(label=" CSV file with duplicates removed", interactive=False) gr.Markdown("### Filtered results dataset (Preview of the first 15 rows)") data_output = gr.Dataframe(interactive=False) submit_btn.click( fn=process_data, inputs=[file_input, method_input], outputs=[text_output, data_output, file_output] ) if __name__ == "__main__": demo.launch()