Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,156 +1,106 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
-
import tempfile
|
5 |
-
import chardet
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
with open(file_path, 'rb') as f:
|
12 |
-
result = chardet.detect(f.read())
|
13 |
-
return result['encoding']
|
14 |
|
15 |
-
|
16 |
-
"""
|
17 |
-
Function to merge multiple CSV files into one
|
18 |
-
|
19 |
-
Args:
|
20 |
-
files: List of uploaded CSV files
|
21 |
-
|
22 |
-
Returns:
|
23 |
-
Path to the merged CSV file and status message
|
24 |
-
"""
|
25 |
-
if not files or len(files) == 0:
|
26 |
-
return None, "No files were uploaded. Please select CSV files to merge."
|
27 |
-
|
28 |
-
if len(files) > 30:
|
29 |
-
return None, "Maximum 30 files can be merged at once."
|
30 |
-
|
31 |
try:
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
# Try other encodings if detected encoding fails
|
41 |
-
encodings_to_try = ['cp949', 'euc-kr', 'latin1', 'ISO-8859-1']
|
42 |
-
for enc in encodings_to_try:
|
43 |
-
try:
|
44 |
-
df = pd.read_csv(file.name, encoding=enc)
|
45 |
-
break
|
46 |
-
except UnicodeDecodeError:
|
47 |
-
continue
|
48 |
-
else:
|
49 |
-
return None, f"Could not determine encoding for '{os.path.basename(file.name)}'."
|
50 |
-
|
51 |
-
dataframes.append(df)
|
52 |
-
|
53 |
-
# Merge all DataFrames
|
54 |
-
if dataframes:
|
55 |
-
merged_df = pd.concat(dataframes, ignore_index=True)
|
56 |
-
|
57 |
-
# Save to temporary file
|
58 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
|
59 |
-
output_path = tmp.name
|
60 |
-
|
61 |
-
# Save merged data in Excel-compatible format (UTF-8 with BOM)
|
62 |
-
merged_df.to_csv(output_path, index=False, encoding='utf-8-sig')
|
63 |
-
|
64 |
-
return output_path, f"Successfully merged {len(files)} files. Please open with UTF-8 encoding in Excel."
|
65 |
-
else:
|
66 |
-
return None, "No data to merge."
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
""
|
75 |
-
|
76 |
-
|
77 |
-
*Seamlessly combine multiple CSV files into one unified dataset*
|
78 |
-
|
79 |
-
---
|
80 |
-
"""
|
81 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
with gr.Row():
|
84 |
-
with gr.Column(scale=
|
85 |
-
gr.
|
86 |
-
|
87 |
-
|
88 |
-
2. Click the "Merge Files" button
|
89 |
-
3. Download your consolidated CSV file
|
90 |
-
|
91 |
-
### Features:
|
92 |
-
- Automatic encoding detection
|
93 |
-
- Handles various CSV formats
|
94 |
-
- Excel-compatible output (UTF-8)
|
95 |
-
- Preserves all data columns
|
96 |
-
""")
|
97 |
-
|
98 |
-
with gr.Column(scale=3):
|
99 |
-
input_files = gr.File(
|
100 |
-
file_count="multiple",
|
101 |
-
label="Upload CSV Files (Max 30)",
|
102 |
-
file_types=[".csv"],
|
103 |
-
elem_id="file_upload"
|
104 |
-
)
|
105 |
|
106 |
-
|
107 |
-
merge_button = gr.Button("Merge Files", variant="primary", size="lg")
|
108 |
|
109 |
with gr.Row():
|
110 |
-
|
111 |
-
|
112 |
-
with gr.Column():
|
113 |
-
output_file = gr.File(label="Download Merged CSV")
|
114 |
-
|
115 |
-
# Add custom CSS for better visual appeal
|
116 |
-
gr.HTML("""
|
117 |
-
<style>
|
118 |
-
.gradio-container {
|
119 |
-
background: linear-gradient(to right, #f9f9f9, #ffffff);
|
120 |
-
border-radius: 12px;
|
121 |
-
}
|
122 |
-
#file_upload {
|
123 |
-
border: 2px dashed #3498db;
|
124 |
-
border-radius: 8px;
|
125 |
-
padding: 20px;
|
126 |
-
transition: all 0.3s;
|
127 |
-
}
|
128 |
-
#file_upload:hover {
|
129 |
-
border-color: #2980b9;
|
130 |
-
box-shadow: 0 0 10px rgba(52, 152, 219, 0.3);
|
131 |
-
}
|
132 |
-
.footer {
|
133 |
-
text-align: center;
|
134 |
-
margin-top: 30px;
|
135 |
-
color: #7f8c8d;
|
136 |
-
font-size: 0.9em;
|
137 |
-
}
|
138 |
-
</style>
|
139 |
-
""")
|
140 |
|
141 |
-
|
142 |
-
gr.HTML("""
|
143 |
-
<div class="footer">
|
144 |
-
<p>CSVFusion © 2025 - A powerful tool for data professionals</p>
|
145 |
-
</div>
|
146 |
-
""")
|
147 |
-
|
148 |
-
merge_button.click(
|
149 |
-
fn=merge_csv_files,
|
150 |
-
inputs=[input_files],
|
151 |
-
outputs=[output_file, status]
|
152 |
-
)
|
153 |
|
154 |
-
|
155 |
-
if __name__ == "__main__":
|
156 |
-
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
from io import BytesIO
|
|
|
|
|
4 |
|
5 |
+
def convert_file(input_file, conversion_type):
|
6 |
+
# Check if a file was uploaded
|
7 |
+
if input_file is None:
|
8 |
+
raise ValueError("Please upload a file.")
|
|
|
|
|
|
|
9 |
|
10 |
+
# Determine if input_file is a file-like object or a file path string.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
try:
|
12 |
+
# Try reading from file-like object
|
13 |
+
file_bytes = input_file.read()
|
14 |
+
file_name = input_file.name
|
15 |
+
except AttributeError:
|
16 |
+
# If there's an AttributeError, treat input_file as a file path.
|
17 |
+
file_name = input_file
|
18 |
+
with open(file_name, "rb") as f:
|
19 |
+
file_bytes = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
file_extension = file_name.lower().split('.')[-1]
|
22 |
+
df = None
|
23 |
+
output_file = None
|
24 |
+
converted_format = None
|
25 |
+
|
26 |
+
# Conversion: CSV to Parquet
|
27 |
+
if conversion_type == "CSV to Parquet":
|
28 |
+
if file_extension != "csv":
|
29 |
+
raise ValueError("For CSV to Parquet conversion, please upload a CSV file.")
|
30 |
+
df = pd.read_csv(BytesIO(file_bytes))
|
31 |
+
output_file = "output.parquet"
|
32 |
+
df.to_parquet(output_file, index=False)
|
33 |
+
converted_format = "Parquet"
|
34 |
+
# Conversion: Parquet to CSV
|
35 |
+
elif conversion_type == "Parquet to CSV":
|
36 |
+
if file_extension != "parquet":
|
37 |
+
raise ValueError("For Parquet to CSV conversion, please upload a Parquet file.")
|
38 |
+
df = pd.read_parquet(BytesIO(file_bytes))
|
39 |
+
output_file = "output.csv"
|
40 |
+
df.to_csv(output_file, index=False)
|
41 |
+
converted_format = "CSV"
|
42 |
+
else:
|
43 |
+
raise ValueError("Invalid conversion type selected.")
|
44 |
|
45 |
+
# Generate a preview of the top 10 rows
|
46 |
+
preview = df.head(10).to_string(index=False)
|
47 |
+
info_message = (
|
48 |
+
f"Input file: {file_name}\n"
|
49 |
+
f"Converted file format: {converted_format}\n\n"
|
50 |
+
f"Preview (Top 10 Rows):\n{preview}"
|
|
|
|
|
|
|
|
|
51 |
)
|
52 |
+
return output_file, info_message
|
53 |
+
|
54 |
+
# Custom CSS for a modern and sleek look
|
55 |
+
custom_css = """
|
56 |
+
body {
|
57 |
+
background-color: #f4f4f4;
|
58 |
+
font-family: 'Helvetica Neue', Arial, sans-serif;
|
59 |
+
}
|
60 |
+
.gradio-container {
|
61 |
+
max-width: 900px;
|
62 |
+
margin: 40px auto;
|
63 |
+
padding: 20px;
|
64 |
+
background-color: #ffffff;
|
65 |
+
border-radius: 12px;
|
66 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
|
67 |
+
}
|
68 |
+
h1, h2 {
|
69 |
+
color: #333333;
|
70 |
+
}
|
71 |
+
.gradio-input, .gradio-output {
|
72 |
+
margin-bottom: 20px;
|
73 |
+
}
|
74 |
+
.gradio-button {
|
75 |
+
background-color: #4CAF50 !important;
|
76 |
+
color: white !important;
|
77 |
+
border: none !important;
|
78 |
+
padding: 10px 20px !important;
|
79 |
+
font-size: 16px !important;
|
80 |
+
border-radius: 6px !important;
|
81 |
+
cursor: pointer;
|
82 |
+
}
|
83 |
+
.gradio-button:hover {
|
84 |
+
background-color: #45a049 !important;
|
85 |
+
}
|
86 |
+
"""
|
87 |
+
|
88 |
+
with gr.Blocks(css=custom_css, title="CSV <-> Parquet Converter") as demo:
|
89 |
+
gr.Markdown("# CSV <-> Parquet Converter")
|
90 |
+
gr.Markdown("Upload a CSV or Parquet file and select the conversion type. The app converts the file to the opposite format and displays a preview of the top 10 rows.")
|
91 |
|
92 |
with gr.Row():
|
93 |
+
with gr.Column(scale=1):
|
94 |
+
input_file = gr.File(label="Upload CSV or Parquet File")
|
95 |
+
with gr.Column(scale=1):
|
96 |
+
conversion_type = gr.Radio(choices=["CSV to Parquet", "Parquet to CSV"], label="Conversion Type")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
convert_button = gr.Button("Convert", elem_classes=["gradio-button"])
|
|
|
99 |
|
100 |
with gr.Row():
|
101 |
+
output_file = gr.File(label="Converted File")
|
102 |
+
preview = gr.Textbox(label="Preview (Top 10 Rows)", lines=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
+
convert_button.click(fn=convert_file, inputs=[input_file, conversion_type], outputs=[output_file, preview])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
demo.launch()
|
|
|
|