Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import chardet
|
4 |
from io import BytesIO
|
5 |
|
6 |
-
def detect_encoding(file_bytes):
|
7 |
-
"""Detect the encoding of the file."""
|
8 |
-
# Use chardet to detect encoding
|
9 |
-
result = chardet.detect(file_bytes)
|
10 |
-
return result['encoding']
|
11 |
-
|
12 |
def convert_file(input_file, conversion_type):
|
13 |
-
#
|
14 |
if input_file is None:
|
15 |
-
return None, "
|
16 |
|
17 |
-
#
|
18 |
try:
|
19 |
-
#
|
20 |
file_bytes = input_file.read()
|
21 |
file_name = input_file.name
|
22 |
except AttributeError:
|
23 |
-
#
|
24 |
file_name = input_file
|
25 |
with open(file_name, "rb") as f:
|
26 |
file_bytes = f.read()
|
@@ -31,62 +24,61 @@ def convert_file(input_file, conversion_type):
|
|
31 |
converted_format = None
|
32 |
|
33 |
try:
|
34 |
-
#
|
35 |
if conversion_type == "CSV to Parquet":
|
36 |
if file_extension != "csv":
|
37 |
-
return None, "
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
df = pd.read_csv(BytesIO(file_bytes), encoding=encoding)
|
45 |
-
except Exception as e:
|
46 |
-
# If that fails, try with other common encodings
|
47 |
-
for enc in ['utf-8', 'latin1', 'iso-8859-1', 'cp1252']:
|
48 |
-
try:
|
49 |
-
df = pd.read_csv(BytesIO(file_bytes), encoding=enc)
|
50 |
-
encoding = enc
|
51 |
-
break
|
52 |
-
except:
|
53 |
-
continue
|
54 |
-
if df is None:
|
55 |
-
return None, f"Failed to read CSV with any encoding. Error: {str(e)}"
|
56 |
|
57 |
output_file = "output.parquet"
|
58 |
df.to_parquet(output_file, index=False)
|
59 |
converted_format = "Parquet"
|
60 |
|
61 |
-
#
|
62 |
elif conversion_type == "Parquet to CSV":
|
63 |
if file_extension != "parquet":
|
64 |
-
return None, "
|
65 |
|
66 |
df = pd.read_parquet(BytesIO(file_bytes))
|
67 |
output_file = "output.csv"
|
68 |
df.to_csv(output_file, index=False, encoding='utf-8')
|
69 |
converted_format = "CSV"
|
70 |
else:
|
71 |
-
return None, "
|
72 |
|
73 |
-
#
|
74 |
preview = df.head(10).to_string(index=False)
|
75 |
info_message = (
|
76 |
-
f"
|
77 |
-
f"
|
78 |
)
|
79 |
-
if conversion_type == "CSV to Parquet":
|
80 |
-
info_message += f"
|
81 |
|
82 |
-
info_message += f"\
|
83 |
|
84 |
return output_file, info_message
|
85 |
|
86 |
except Exception as e:
|
87 |
-
return None, f"
|
88 |
|
89 |
-
#
|
90 |
custom_css = """
|
91 |
body {
|
92 |
background-color: #f4f4f4;
|
@@ -120,33 +112,33 @@ h1, h2 {
|
|
120 |
}
|
121 |
"""
|
122 |
|
123 |
-
with gr.Blocks(css=custom_css, title="CSV <-> Parquet
|
124 |
-
gr.Markdown("# CSV <-> Parquet
|
125 |
-
gr.Markdown("
|
126 |
|
127 |
with gr.Row():
|
128 |
with gr.Column(scale=1):
|
129 |
-
input_file = gr.File(label="
|
130 |
with gr.Column(scale=1):
|
131 |
conversion_type = gr.Radio(
|
132 |
choices=["CSV to Parquet", "Parquet to CSV"],
|
133 |
-
label="
|
134 |
-
value="CSV to Parquet" #
|
135 |
)
|
136 |
|
137 |
-
convert_button = gr.Button("
|
138 |
|
139 |
with gr.Row():
|
140 |
-
output_file = gr.File(label="
|
141 |
-
preview = gr.Textbox(label="
|
142 |
|
143 |
convert_button.click(fn=convert_file, inputs=[input_file, conversion_type], outputs=[output_file, preview])
|
144 |
|
145 |
gr.Markdown("""
|
146 |
-
###
|
147 |
-
-
|
148 |
-
- Parquet
|
149 |
-
-
|
150 |
""")
|
151 |
|
152 |
if __name__ == "__main__":
|
|
|
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 |
+
# νμΌμ΄ μ
λ‘λλμλμ§ νμΈ
|
7 |
if input_file is None:
|
8 |
+
return None, "νμΌμ μ
λ‘λν΄ μ£ΌμΈμ."
|
9 |
|
10 |
+
# νμΌ λ΄μ© μ½κΈ°
|
11 |
try:
|
12 |
+
# νμΌ κ°μ²΄μμ μ½κΈ° μλ
|
13 |
file_bytes = input_file.read()
|
14 |
file_name = input_file.name
|
15 |
except AttributeError:
|
16 |
+
# AttributeErrorκ° λ°μνλ©΄ input_fileμ νμΌ κ²½λ‘λ‘ μ²λ¦¬
|
17 |
file_name = input_file
|
18 |
with open(file_name, "rb") as f:
|
19 |
file_bytes = f.read()
|
|
|
24 |
converted_format = None
|
25 |
|
26 |
try:
|
27 |
+
# λ³ν: CSVμμ ParquetμΌλ‘
|
28 |
if conversion_type == "CSV to Parquet":
|
29 |
if file_extension != "csv":
|
30 |
+
return None, "CSVμμ ParquetμΌλ‘ λ³ννλ €λ©΄ CSV νμΌμ μ
λ‘λν΄ μ£ΌμΈμ."
|
31 |
+
|
32 |
+
# λ€μν μΈμ½λ©μ μλ (chardet μμ΄)
|
33 |
+
encodings_to_try = ['utf-8', 'latin1', 'iso-8859-1', 'cp1252']
|
34 |
+
encoding = None
|
35 |
|
36 |
+
for enc in encodings_to_try:
|
37 |
+
try:
|
38 |
+
df = pd.read_csv(BytesIO(file_bytes), encoding=enc)
|
39 |
+
encoding = enc
|
40 |
+
break
|
41 |
+
except UnicodeDecodeError:
|
42 |
+
continue
|
43 |
+
except Exception as e:
|
44 |
+
return None, f"CSV μ½κΈ° μ€λ₯: {str(e)}"
|
45 |
|
46 |
+
if df is None:
|
47 |
+
return None, "μΌλ°μ μΈ μΈμ½λ©μΌλ‘ CSVλ₯Ό μ½μ§ λͺ»νμ΅λλ€. νμΌμ΄ νΉμ΄ν μΈμ½λ©μ μ¬μ©ν μ μμ΅λλ€."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
output_file = "output.parquet"
|
50 |
df.to_parquet(output_file, index=False)
|
51 |
converted_format = "Parquet"
|
52 |
|
53 |
+
# λ³ν: Parquetμμ CSVλ‘
|
54 |
elif conversion_type == "Parquet to CSV":
|
55 |
if file_extension != "parquet":
|
56 |
+
return None, "Parquetμμ CSVλ‘ λ³ννλ €λ©΄ Parquet νμΌμ μ
λ‘λν΄ μ£ΌμΈμ."
|
57 |
|
58 |
df = pd.read_parquet(BytesIO(file_bytes))
|
59 |
output_file = "output.csv"
|
60 |
df.to_csv(output_file, index=False, encoding='utf-8')
|
61 |
converted_format = "CSV"
|
62 |
else:
|
63 |
+
return None, "μλͺ»λ λ³ν μ νμ΄ μ νλμμ΅λλ€."
|
64 |
|
65 |
+
# μμ 10κ° νμ 미리보기 μμ±
|
66 |
preview = df.head(10).to_string(index=False)
|
67 |
info_message = (
|
68 |
+
f"μ
λ ₯ νμΌ: {file_name}\n"
|
69 |
+
f"λ³νλ νμΌ νμ: {converted_format}\n"
|
70 |
)
|
71 |
+
if conversion_type == "CSV to Parquet" and encoding:
|
72 |
+
info_message += f"μ¬μ©λ μΈμ½λ©: {encoding}\n"
|
73 |
|
74 |
+
info_message += f"\n미리보기 (μμ 10κ° ν):\n{preview}"
|
75 |
|
76 |
return output_file, info_message
|
77 |
|
78 |
except Exception as e:
|
79 |
+
return None, f"λ³ν μ€ μ€λ₯ λ°μ: {str(e)}"
|
80 |
|
81 |
+
# λͺ¨λνκ³ μΈλ ¨λ μ€νμΌμ μοΏ½οΏ½οΏ½ μ¬μ©μ μ μ CSS
|
82 |
custom_css = """
|
83 |
body {
|
84 |
background-color: #f4f4f4;
|
|
|
112 |
}
|
113 |
"""
|
114 |
|
115 |
+
with gr.Blocks(css=custom_css, title="CSV <-> Parquet λ³νκΈ°") as demo:
|
116 |
+
gr.Markdown("# CSV <-> Parquet λ³νκΈ°")
|
117 |
+
gr.Markdown("CSV λλ Parquet νμΌμ μ
λ‘λνκ³ λ³ν μ νμ μ ννμΈμ. μ±μ νμΌμ λ°λ νμμΌλ‘ λ³ννκ³ μμ 10κ° νμ 미리보기λ₯Ό νμν©λλ€.")
|
118 |
|
119 |
with gr.Row():
|
120 |
with gr.Column(scale=1):
|
121 |
+
input_file = gr.File(label="CSV λλ Parquet νμΌ μ
λ‘λ")
|
122 |
with gr.Column(scale=1):
|
123 |
conversion_type = gr.Radio(
|
124 |
choices=["CSV to Parquet", "Parquet to CSV"],
|
125 |
+
label="λ³ν μ ν",
|
126 |
+
value="CSV to Parquet" # κΈ°λ³Έκ° μ€μ
|
127 |
)
|
128 |
|
129 |
+
convert_button = gr.Button("λ³ν", elem_classes=["gradio-button"])
|
130 |
|
131 |
with gr.Row():
|
132 |
+
output_file = gr.File(label="λ³νλ νμΌ")
|
133 |
+
preview = gr.Textbox(label="미리보기 (μμ 10κ° ν)", lines=15)
|
134 |
|
135 |
convert_button.click(fn=convert_file, inputs=[input_file, conversion_type], outputs=[output_file, preview])
|
136 |
|
137 |
gr.Markdown("""
|
138 |
+
### μ°Έκ³ :
|
139 |
+
- μ΄ λ³νκΈ°λ μΌλ°μ μΈ CSV μΈμ½λ©(UTF-8, Latin-1, ISO-8859-1, CP1252)μ μλν©λλ€
|
140 |
+
- Parquet νμΌμ CSVλ³΄λ€ λ°μ΄ν° νμ
μ λ μ 보쑴ν©λλ€
|
141 |
+
- 미리보기λ λ°μ΄ν°μ μ²μ 10νλ§ νμν©λλ€
|
142 |
""")
|
143 |
|
144 |
if __name__ == "__main__":
|