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