Spaces:
Running
Running
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() | |