DocSrvNyk commited on
Commit
1dcbe02
·
1 Parent(s): d08c037

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +58 -0
  2. requirements (3).txt +2 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import os
4
+ import zipfile
5
+
6
+ def process_csv(uploaded_file):
7
+
8
+ # Load the data from the uploaded file's byte stream
9
+ data = pd.read_csv(uploaded_file.name)
10
+
11
+ # Dictionary to store column name and its mapping of original values to codes
12
+ legend_dict = {}
13
+
14
+ # List to store the details of columns where data was added
15
+ data_added_details = []
16
+
17
+ # Loop through each column in the DataFrame
18
+ for col in data.columns:
19
+ # Check if the column is of type object (text-based) or if it's numerical with less than six unique options
20
+ if data[col].dtype == 'object' or (data[col].nunique() < 6 and pd.api.types.is_numeric_dtype(data[col])):
21
+ # Create a mapping of original values to codes, including NaN or blank values mapped to -9999
22
+ mapping = {value: code if pd.notna(value) else -9999 for code, value in enumerate(data[col].unique())}
23
+ legend_dict[col] = mapping
24
+ # Replace the values in the column with their respective codes
25
+ data[col] = data[col].map(mapping)
26
+ elif pd.api.types.is_numeric_dtype(data[col]) and any(pd.isna(data[col])):
27
+ # Replace with median
28
+ median_value = data[col].median()
29
+ data[col].fillna(median_value, inplace=True)
30
+ data_added_details.append([col, "Median", median_value])
31
+
32
+ # Name of the zip file based on uploaded file name
33
+ zip_name = "processed_files.zip"
34
+
35
+ # Save CSV files and add them to the zip file
36
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
37
+ data.to_csv("modified_data.csv", index=False)
38
+ zipf.write("modified_data.csv")
39
+
40
+ legend_df = pd.DataFrame(list(legend_dict.items()), columns=['Column', 'Mapping'])
41
+ legend_df.to_csv("legend.csv", index=False)
42
+ zipf.write("legend.csv")
43
+
44
+ data_added_df = pd.DataFrame(data_added_details, columns=['Column', 'Method', 'Value Added'])
45
+ data_added_df.to_csv("data_added_details.csv", index=False)
46
+ zipf.write("data_added_details.csv")
47
+
48
+ return zip_name
49
+
50
+ # Gradio Interface
51
+ iface = gr.Interface(
52
+ fn=process_csv,
53
+ inputs=gr.inputs.File(type="file", label="Upload CSV File"),
54
+ outputs=gr.outputs.File(label="Download Processed Files"),
55
+ live=False
56
+ )
57
+
58
+ iface.launch()
requirements (3).txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ pandas==1.5.3
2
+ gradio==3.41.2