openfree's picture
Update app.py
1fd0c30 verified
raw
history blame
2.34 kB
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()