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()