File size: 1,493 Bytes
d2b9031
49e25d2
 
ff86828
d2b9031
49e25d2
9abfad3
49e25d2
 
 
 
9abfad3
ff86828
49e25d2
 
ff86828
9faad6b
 
 
 
 
9abfad3
9faad6b
9abfad3
9faad6b
 
ff86828
9abfad3
7773ef1
ff86828
 
7773ef1
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
import gradio as gr
import pandas as pd
import requests
from io import BytesIO

def convert_parquet_to_jsonl(parquet_file=None, parquet_url=None):
    # Read Parquet from file or URL
    if parquet_file is not None:
        df = pd.read_parquet(parquet_file.name)
    elif parquet_url is not None:
        response = requests.get(parquet_url)
        response.raise_for_status()
        df = pd.read_parquet(BytesIO(response.content))
    else:
        raise ValueError("Either parquet_file or parquet_url must be provided")

    # Clean string columns to replace invalid UTF-8 sequences
    for col in df.select_dtypes(include=["object"]).columns:
        df[col] = df[col].apply(
            lambda x: x.encode("utf-8", errors="replace").decode("utf-8", errors="replace") if isinstance(x, str) else x
        )

    # Convert to JSON Lines
    jsonl_data = df.to_json(orient="records", lines=True)
    
    # Write the output to a file using UTF-8 encoding explicitly
    output_file_path = "output.jsonl"
    with open(output_file_path, "w", encoding="utf-8") as f:
        f.write(jsonl_data)

    return output_file_path

demo = gr.Interface(
    fn=convert_parquet_to_jsonl,
    inputs=[gr.File(label="Parquet File"), gr.Textbox(label="Parquet File URL")],
    outputs=[gr.File(label="JSONL Output")],
    title="Parquet to JSONL Converter",
    description="Convert a Parquet file to JSONL format from a downloadable link or file upload"
)

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