Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,33 @@ import requests
|
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
def convert_file(input_file, file_url, conversion_type):
|
| 7 |
-
#
|
| 8 |
if input_file is None and (file_url is None or file_url.strip() == ""):
|
| 9 |
-
raise ValueError("
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
response = requests.get(file_url)
|
| 20 |
response.raise_for_status()
|
| 21 |
if conversion_type == "CSV to Parquet":
|
| 22 |
df = pd.read_csv(BytesIO(response.content))
|
| 23 |
-
else:
|
| 24 |
df = pd.read_parquet(BytesIO(response.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
if conversion_type == "CSV to Parquet":
|
| 28 |
output_file = "output.parquet"
|
| 29 |
df.to_parquet(output_file, index=False)
|
|
@@ -31,7 +38,7 @@ def convert_file(input_file, file_url, conversion_type):
|
|
| 31 |
output_file = "output.csv"
|
| 32 |
df.to_csv(output_file, index=False)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
preview = df.head(10).to_string(index=False)
|
| 36 |
|
| 37 |
return output_file, preview
|
|
@@ -39,16 +46,16 @@ def convert_file(input_file, file_url, conversion_type):
|
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=convert_file,
|
| 41 |
inputs=[
|
| 42 |
-
gr.File(label="
|
| 43 |
-
gr.Textbox(label="
|
| 44 |
-
gr.Radio(choices=["CSV to Parquet", "Parquet to CSV"], label="
|
| 45 |
],
|
| 46 |
outputs=[
|
| 47 |
-
gr.File(label="
|
| 48 |
-
gr.Textbox(label="
|
| 49 |
],
|
| 50 |
-
title="CSV <-> Parquet
|
| 51 |
-
description="
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
def convert_file(input_file, file_url, conversion_type):
|
| 7 |
+
# νμΌ μ
λ‘λμ URL μ
λ ₯ λ λ€ μμΌλ©΄ μλ¬ λ°μ
|
| 8 |
if input_file is None and (file_url is None or file_url.strip() == ""):
|
| 9 |
+
raise ValueError("νμΌ μ
λ‘λ λλ URLμ μ 곡νμΈμ.")
|
| 10 |
|
| 11 |
+
df = None
|
| 12 |
+
|
| 13 |
+
# μ
λ‘λλ νμΌμ΄ μμΌλ©΄ URLμμ μ½κΈ°
|
| 14 |
+
if input_file is None:
|
| 15 |
+
file_url = file_url.strip()
|
| 16 |
+
# URL μ€ν΄μ΄ μμΌλ©΄ κΈ°λ³Έμ μΌλ‘ "https://"λ₯Ό μΆκ°
|
| 17 |
+
if not file_url.lower().startswith(("http://", "https://")):
|
| 18 |
+
file_url = "https://" + file_url
|
| 19 |
response = requests.get(file_url)
|
| 20 |
response.raise_for_status()
|
| 21 |
if conversion_type == "CSV to Parquet":
|
| 22 |
df = pd.read_csv(BytesIO(response.content))
|
| 23 |
+
else: # Parquet to CSV
|
| 24 |
df = pd.read_parquet(BytesIO(response.content))
|
| 25 |
+
else:
|
| 26 |
+
# νμΌ μ
λ‘λκ° μλ κ²½μ°
|
| 27 |
+
file_path = input_file.name
|
| 28 |
+
if conversion_type == "CSV to Parquet":
|
| 29 |
+
df = pd.read_csv(file_path)
|
| 30 |
+
else:
|
| 31 |
+
df = pd.read_parquet(file_path)
|
| 32 |
|
| 33 |
+
# λ³ν μ€ν: CSV to Parquet νΉμ Parquet to CSV
|
| 34 |
if conversion_type == "CSV to Parquet":
|
| 35 |
output_file = "output.parquet"
|
| 36 |
df.to_parquet(output_file, index=False)
|
|
|
|
| 38 |
output_file = "output.csv"
|
| 39 |
df.to_csv(output_file, index=False)
|
| 40 |
|
| 41 |
+
# μμ 10μ€ λ―Έλ¦¬λ³΄κΈ° μμ±
|
| 42 |
preview = df.head(10).to_string(index=False)
|
| 43 |
|
| 44 |
return output_file, preview
|
|
|
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=convert_file,
|
| 48 |
inputs=[
|
| 49 |
+
gr.File(label="μ
λ ₯ νμΌ (CSV λλ Parquet)"),
|
| 50 |
+
gr.Textbox(label="μ
λ ₯ νμΌ URL (μ ν)", placeholder="CSV λλ Parquet νμΌμ URLμ μ
λ ₯νμΈμ."),
|
| 51 |
+
gr.Radio(choices=["CSV to Parquet", "Parquet to CSV"], label="λ³ν μ ν")
|
| 52 |
],
|
| 53 |
outputs=[
|
| 54 |
+
gr.File(label="λ³νλ νμΌ"),
|
| 55 |
+
gr.Textbox(label="미리보기 (μμ 10μ€)")
|
| 56 |
],
|
| 57 |
+
title="CSV <-> Parquet λ³νκΈ°",
|
| 58 |
+
description="λ³ν μ νμ μ ννκ³ , νμΌμ μ
λ‘λνκ±°λ URLμ μ
λ ₯νμ¬ CSVμ Parquet νμΌμ μνΈ λ³νν©λλ€. μμ 10μ€ λ―Έλ¦¬λ³΄κΈ°λ μ 곡ν©λλ€."
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|