File size: 3,518 Bytes
794871d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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()