Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,14 @@ 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()
|
@@ -22,7 +22,7 @@ def convert_file(input_file, conversion_type):
|
|
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":
|
@@ -31,6 +31,7 @@ def convert_file(input_file, conversion_type):
|
|
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":
|
@@ -41,66 +42,227 @@ def convert_file(input_file, conversion_type):
|
|
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
|
|
|
|
|
50 |
f"Preview (Top 10 Rows):\n{preview}"
|
51 |
)
|
52 |
return output_file, info_message
|
53 |
|
54 |
-
#
|
55 |
custom_css = """
|
56 |
body {
|
57 |
-
background
|
58 |
-
font-family: '
|
59 |
}
|
|
|
60 |
.gradio-container {
|
61 |
-
max-width:
|
62 |
margin: 40px auto;
|
63 |
-
padding:
|
64 |
background-color: #ffffff;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
border-radius: 12px;
|
66 |
-
|
|
|
67 |
}
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
}
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
73 |
}
|
74 |
-
|
75 |
-
|
|
|
76 |
color: white !important;
|
77 |
border: none !important;
|
78 |
-
padding:
|
79 |
font-size: 16px !important;
|
80 |
-
|
|
|
81 |
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
}
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
85 |
}
|
86 |
"""
|
87 |
|
88 |
-
with gr.Blocks(css=custom_css, title="
|
89 |
-
gr.
|
90 |
-
gr.Markdown("
|
|
|
|
|
|
|
91 |
|
92 |
with gr.Row():
|
93 |
-
with gr.Column(
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
output_file = gr.File(label="Converted File")
|
102 |
-
preview = gr.Textbox(label="Preview (Top 10 Rows)", lines=15)
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
demo.launch()
|
|
|
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()
|
|
|
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":
|
|
|
31 |
output_file = "output.parquet"
|
32 |
df.to_parquet(output_file, index=False)
|
33 |
converted_format = "Parquet"
|
34 |
+
|
35 |
# Conversion: Parquet to CSV
|
36 |
elif conversion_type == "Parquet to CSV":
|
37 |
if file_extension != "parquet":
|
|
|
42 |
converted_format = "CSV"
|
43 |
else:
|
44 |
raise ValueError("Invalid conversion type selected.")
|
45 |
+
|
46 |
# Generate a preview of the top 10 rows
|
47 |
preview = df.head(10).to_string(index=False)
|
48 |
info_message = (
|
49 |
f"Input file: {file_name}\n"
|
50 |
+
f"Converted file format: {converted_format}\n"
|
51 |
+
f"Total rows: {len(df)}\n"
|
52 |
+
f"Total columns: {len(df.columns)}\n\n"
|
53 |
f"Preview (Top 10 Rows):\n{preview}"
|
54 |
)
|
55 |
return output_file, info_message
|
56 |
|
57 |
+
# Enhanced custom CSS for a more visually appealing interface
|
58 |
custom_css = """
|
59 |
body {
|
60 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
61 |
+
font-family: 'Poppins', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
62 |
}
|
63 |
+
|
64 |
.gradio-container {
|
65 |
+
max-width: 950px;
|
66 |
margin: 40px auto;
|
67 |
+
padding: 30px;
|
68 |
background-color: #ffffff;
|
69 |
+
border-radius: 16px;
|
70 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
71 |
+
}
|
72 |
+
|
73 |
+
h1 {
|
74 |
+
color: #3a4149;
|
75 |
+
font-size: 2.5rem;
|
76 |
+
text-align: center;
|
77 |
+
margin-bottom: 5px;
|
78 |
+
font-weight: 600;
|
79 |
+
}
|
80 |
+
|
81 |
+
h2 {
|
82 |
+
color: #5a6570;
|
83 |
+
font-size: 1.2rem;
|
84 |
+
text-align: center;
|
85 |
+
margin-bottom: 25px;
|
86 |
+
font-weight: 400;
|
87 |
+
}
|
88 |
+
|
89 |
+
.header-icon {
|
90 |
+
font-size: 3rem;
|
91 |
+
text-align: center;
|
92 |
+
margin-bottom: 10px;
|
93 |
+
color: #4285f4;
|
94 |
+
}
|
95 |
+
|
96 |
+
.instruction-box {
|
97 |
+
background-color: #f8f9fa;
|
98 |
+
border-left: 4px solid #4285f4;
|
99 |
+
padding: 15px;
|
100 |
+
margin-bottom: 25px;
|
101 |
+
border-radius: 6px;
|
102 |
+
}
|
103 |
+
|
104 |
+
.instruction-step {
|
105 |
+
margin: 8px 0;
|
106 |
+
padding-left: 10px;
|
107 |
+
}
|
108 |
+
|
109 |
+
.file-box {
|
110 |
+
border: 2px dashed #ddd;
|
111 |
border-radius: 12px;
|
112 |
+
padding: 20px;
|
113 |
+
transition: all 0.3s ease;
|
114 |
}
|
115 |
+
|
116 |
+
.file-box:hover {
|
117 |
+
border-color: #4285f4;
|
118 |
+
box-shadow: 0 5px 15px rgba(66, 133, 244, 0.15);
|
119 |
+
}
|
120 |
+
|
121 |
+
.conversion-radio label {
|
122 |
+
padding: 10px 15px;
|
123 |
+
margin: 5px;
|
124 |
+
border-radius: 8px;
|
125 |
+
border: 1px solid #eaeaea;
|
126 |
+
transition: all 0.2s ease;
|
127 |
}
|
128 |
+
|
129 |
+
.conversion-radio input:checked + label {
|
130 |
+
background-color: #e8f0fe;
|
131 |
+
border-color: #4285f4;
|
132 |
+
color: #4285f4;
|
133 |
}
|
134 |
+
|
135 |
+
.convert-button {
|
136 |
+
background: linear-gradient(to right, #4285f4, #34a853) !important;
|
137 |
color: white !important;
|
138 |
border: none !important;
|
139 |
+
padding: 12px 25px !important;
|
140 |
font-size: 16px !important;
|
141 |
+
font-weight: 500 !important;
|
142 |
+
border-radius: 30px !important;
|
143 |
cursor: pointer;
|
144 |
+
margin: 20px auto !important;
|
145 |
+
display: block !important;
|
146 |
+
box-shadow: 0 4px 12px rgba(66, 133, 244, 0.25) !important;
|
147 |
+
}
|
148 |
+
|
149 |
+
.convert-button:hover {
|
150 |
+
box-shadow: 0 6px 16px rgba(66, 133, 244, 0.4) !important;
|
151 |
+
transform: translateY(-2px);
|
152 |
+
}
|
153 |
+
|
154 |
+
.footer {
|
155 |
+
text-align: center;
|
156 |
+
margin-top: 30px;
|
157 |
+
color: #70757a;
|
158 |
+
font-size: 0.9rem;
|
159 |
+
}
|
160 |
+
|
161 |
+
.preview-box {
|
162 |
+
background-color: #f8f9fa;
|
163 |
+
border-radius: 8px;
|
164 |
+
padding: 15px;
|
165 |
+
font-family: monospace;
|
166 |
+
white-space: pre-wrap;
|
167 |
+
max-height: 400px;
|
168 |
+
overflow-y: auto;
|
169 |
+
}
|
170 |
+
|
171 |
+
.info-tag {
|
172 |
+
display: inline-block;
|
173 |
+
background-color: #e8f0fe;
|
174 |
+
color: #4285f4;
|
175 |
+
padding: 4px 10px;
|
176 |
+
border-radius: 20px;
|
177 |
+
font-size: 0.85rem;
|
178 |
+
margin-right: 8px;
|
179 |
+
margin-bottom: 8px;
|
180 |
}
|
181 |
+
|
182 |
+
.divider {
|
183 |
+
height: 1px;
|
184 |
+
background: linear-gradient(to right, transparent, #ddd, transparent);
|
185 |
+
margin: 25px 0;
|
186 |
}
|
187 |
"""
|
188 |
|
189 |
+
with gr.Blocks(css=custom_css, title="DataFormat Converter") as demo:
|
190 |
+
gr.HTML('<div class="header-icon">📊</div>')
|
191 |
+
gr.Markdown("# DataFormat Converter")
|
192 |
+
gr.Markdown("## Seamlessly convert between CSV and Parquet formats with just a few clicks")
|
193 |
+
|
194 |
+
gr.HTML('<div class="divider"></div>')
|
195 |
|
196 |
with gr.Row():
|
197 |
+
with gr.Column():
|
198 |
+
gr.HTML("""
|
199 |
+
<div class="instruction-box">
|
200 |
+
<h3>How It Works</h3>
|
201 |
+
<div class="instruction-step">1. Upload your CSV or Parquet file</div>
|
202 |
+
<div class="instruction-step">2. Select the conversion direction</div>
|
203 |
+
<div class="instruction-step">3. Click "Convert" and download your transformed file</div>
|
204 |
+
</div>
|
205 |
+
|
206 |
+
<div class="info-section">
|
207 |
+
<div class="info-tag">Fast Conversion</div>
|
208 |
+
<div class="info-tag">Data Preview</div>
|
209 |
+
<div class="info-tag">No Size Limits</div>
|
210 |
+
<div class="info-tag">Maintains Structure</div>
|
211 |
+
</div>
|
212 |
+
""")
|
213 |
+
|
214 |
+
gr.HTML("""
|
215 |
+
<div style="margin-top: 25px;">
|
216 |
+
<h3>Why Convert?</h3>
|
217 |
+
<p>Parquet files offer significant advantages for data storage and analysis:</p>
|
218 |
+
<ul>
|
219 |
+
<li>Smaller file size (up to 87% reduction)</li>
|
220 |
+
<li>Faster query performance</li>
|
221 |
+
<li>Column-oriented storage</li>
|
222 |
+
<li>Better compression</li>
|
223 |
+
</ul>
|
224 |
+
<p>CSV files are useful for:</p>
|
225 |
+
<ul>
|
226 |
+
<li>Universal compatibility</li>
|
227 |
+
<li>Human readability</li>
|
228 |
+
<li>Simple integration with many tools</li>
|
229 |
+
</ul>
|
230 |
+
</div>
|
231 |
+
""")
|
232 |
|
233 |
+
with gr.Column():
|
234 |
+
with gr.Box(elem_classes=["file-box"]):
|
235 |
+
input_file = gr.File(label="Upload Your File")
|
236 |
+
conversion_type = gr.Radio(
|
237 |
+
choices=["CSV to Parquet", "Parquet to CSV"],
|
238 |
+
label="Select Conversion Type",
|
239 |
+
value="CSV to Parquet",
|
240 |
+
elem_classes=["conversion-radio"]
|
241 |
+
)
|
242 |
+
convert_button = gr.Button("Convert Now", elem_classes=["convert-button"])
|
243 |
+
|
244 |
+
with gr.Accordion("Conversion Results", open=False):
|
245 |
+
output_file = gr.File(label="Download Converted File")
|
246 |
+
|
247 |
+
with gr.Accordion("Data Preview", open=True):
|
248 |
+
preview = gr.Textbox(
|
249 |
+
label="File Information and Preview",
|
250 |
+
lines=15,
|
251 |
+
elem_classes=["preview-box"]
|
252 |
+
)
|
253 |
|
254 |
+
gr.HTML('<div class="divider"></div>')
|
|
|
|
|
255 |
|
256 |
+
gr.HTML("""
|
257 |
+
<div class="footer">
|
258 |
+
<p>DataFormat Converter © 2025 | Built with Gradio | An efficient tool for data professionals</p>
|
259 |
+
</div>
|
260 |
+
""")
|
261 |
+
|
262 |
+
convert_button.click(
|
263 |
+
fn=convert_file,
|
264 |
+
inputs=[input_file, conversion_type],
|
265 |
+
outputs=[output_file, preview]
|
266 |
+
)
|
267 |
|
268 |
demo.launch()
|