johnnydevriese commited on
Commit
002b754
β€’
1 Parent(s): ce51916

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+
5
+ # Flexoki colors for highlighting
6
+ HIGHLIGHT_COLORS = [
7
+ "#D14D41", # Red
8
+ "#879A39", # Green
9
+ "#66A1DC", # Blue
10
+ "#D0A215", # Yellow
11
+ "#8E5F26", # Brown
12
+ ]
13
+
14
+
15
+ def highlight_diff(*values):
16
+ unique_values = set(str(v) for v in values if pd.notna(v))
17
+ if len(unique_values) == 1:
18
+ return str(values[0])
19
+
20
+ highlighted = []
21
+ for i, value in enumerate(values):
22
+ if pd.isna(value):
23
+ continue
24
+ highlighted.append(
25
+ f'<span style="background-color: {HIGHLIGHT_COLORS[i]}; color: white;">{value}</span>'
26
+ )
27
+
28
+ return " | ".join(highlighted)
29
+
30
+
31
+ def compare_csvs(*files):
32
+ valid_files = [f for f in files if f is not None]
33
+ if len(valid_files) < 2:
34
+ return "Please upload at least two CSV files.", None
35
+
36
+ # Read CSV files
37
+ dataframes = [pd.read_csv(file.name) for file in valid_files]
38
+
39
+ # Check if all dataframes have the same shape
40
+ if len(set(df.shape for df in dataframes)) > 1:
41
+ return (
42
+ "The CSV files have different shapes. Please ensure they all have the same number of rows and columns.",
43
+ None,
44
+ )
45
+
46
+ # Create a combined dataframe for comparison
47
+ combined_df = dataframes[0].copy()
48
+ for col in combined_df.columns:
49
+ combined_df[col] = [
50
+ highlight_diff(*values) for values in zip(*(df[col] for df in dataframes))
51
+ ]
52
+
53
+ # Calculate summary
54
+ total_cells = dataframes[0].size
55
+ different_cells = sum((dataframes[0] != df).sum().sum() for df in dataframes[1:])
56
+ diff_percentage = (different_cells / (total_cells * (len(dataframes) - 1))) * 100
57
+
58
+ summary = f"Total cells: {total_cells}\n"
59
+ summary += f"Different cells: {different_cells}\n"
60
+ summary += f"Percentage of differences: {diff_percentage:.2f}%"
61
+
62
+ return summary, combined_df.to_html(escape=False)
63
+
64
+
65
+ with gr.Blocks() as app:
66
+ gr.Markdown("# πŸ’• Kindness - CSV Comparison")
67
+
68
+ with gr.Row():
69
+ files = [gr.File(label=f"Upload CSV {i+1}") for i in range(5)]
70
+
71
+ compare_btn = gr.Button("Compare CSVs")
72
+
73
+ summary = gr.Textbox(label="Summary")
74
+ diff_output = gr.HTML(label="Differences")
75
+
76
+ compare_btn.click(compare_csvs, inputs=files, outputs=[summary, diff_output])
77
+
78
+
79
+ app.launch()
80
+