File size: 1,970 Bytes
d2b9031
49e25d2
 
ff86828
d2b9031
2bf1e25
 
 
 
 
 
 
 
 
 
 
 
49e25d2
2bf1e25
 
 
 
 
 
a92db70
2bf1e25
 
 
 
 
 
 
 
 
 
 
 
7773ef1
49e25d2
2bf1e25
fcd8f70
2bf1e25
 
 
 
 
 
 
fcd8f70
2bf1e25
 
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
import gradio as gr
import pandas as pd
import requests
from io import BytesIO

def convert_file(input_file, file_url, conversion_type):
    # Use the file provided by upload; if not, use the URL input.
    if input_file is None and (file_url is None or file_url.strip() == ""):
        raise ValueError("Please provide a file or a URL.")
    
    # Read the file into a DataFrame based on conversion type.
    if input_file is not None:
        file_path = input_file.name
        if conversion_type == "CSV to Parquet":
            df = pd.read_csv(file_path)
        else:  # Parquet to CSV
            df = pd.read_parquet(file_path)
    else:
        response = requests.get(file_url)
        response.raise_for_status()
        if conversion_type == "CSV to Parquet":
            df = pd.read_csv(BytesIO(response.content))
        else:
            df = pd.read_parquet(BytesIO(response.content))
    
    # Save the converted file.
    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)
    
    # Generate a preview of the top 10 rows.
    preview = df.head(10).to_string(index=False)
    
    return output_file, preview

demo = gr.Interface(
    fn=convert_file,
    inputs=[
        gr.File(label="Input File (CSV or Parquet)"),
        gr.Textbox(label="Input File URL (optional)", placeholder="Enter a URL to a CSV or Parquet file"),
        gr.Radio(choices=["CSV to Parquet", "Parquet to CSV"], label="Conversion Type")
    ],
    outputs=[
        gr.File(label="Converted File"),
        gr.Textbox(label="Preview (Top 10 Rows)")
    ],
    title="CSV <-> Parquet Converter",
    description="Choose a conversion type, upload a file or enter a URL, and convert between CSV and Parquet formats. A preview of the top 10 rows will be shown."
)

if __name__ == "__main__":
    demo.launch()