Spaces:
Sleeping
Sleeping
Update app.py
Browse files23 unmodified lines242526272829303 unmodified lines343536373839403 unmodified lines4445464748495019 unmodified lines707172737475767778798081828316 unmodified lines10010110210310410510610710810911011131 unmodified lines14314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617723 unmodified lines# Conversion: CSV to Parquetif conversion_type == "CSV to Parquet":if input_file is None or file_extension != "csv":raise ValueError("For CSV to Parquet conversion, please upload a CSV file.")df = pd.read_csv(BytesIO(file_bytes))output_file = "output.parquet"df.to_parquet(output_file, index=False)3 unmodified lines# Conversion: Parquet to CSVelif conversion_type == "Parquet to CSV":if input_file is None or file_extension != "parquet":raise ValueError("For Parquet to CSV conversion, please upload a Parquet file.")df = pd.read_parquet(BytesIO(file_bytes))output_file = "output.csv"df.to_csv(output_file, index=False)3 unmodified lines# Conversion: CSV to JSONLelif conversion_type == "CSV to JSONL":if input_file is None or file_extension != "csv":raise ValueError("For CSV to JSONL conversion, please upload a CSV file.")# Read CSV with latin1 encodingdf = pd.read_csv(BytesIO(file_bytes), encoding='latin1')output_file = "metadata.jsonl"19 unmodified lines# Use uploaded file if available; otherwise try the provided URLif input_file is not None:df = pd.read_parquet(BytesIO(file_bytes))# file_name is already set from the uploaded fileelif parquet_url:response = requests.get(parquet_url)response.raise_for_status() # Ensure the request was successfuldf = pd.read_parquet(BytesIO(response.content))file_name = "from_url.parquet"else:raise ValueError("For Parquet to JSONL conversion, please upload a file or provide a URL.")output_file = "output.jsonl"# Recursive function to decode bytes to UTF-8 strings16 unmodified linespreview_str = df.head(10).to_string(index=False)else:raise ValueError("Invalid conversion type selected.")info_message = (f"Input file: {file_name if file_name is not None else 'N/A'}\n"f"Converted file format: {converted_format}\n\n"f"Preview (Top 10 Rows):\n{preview_str}")return output_file, info_message31 unmodified lines}"""with gr.Blocks(css=custom_css, title="Comprehensive Dataset Converter") as demo:gr.Markdown("# Comprehensive Dataset Converter")gr.Markdown("Upload a CSV or Parquet file (or provide a Parquet file URL for Parquet to JSONL conversion) ""and select the conversion type. The app converts the file to the desired format and displays a preview of the top 10 rows.")with gr.Row():with gr.Column(scale=1):input_file = gr.File(label="Upload CSV or Parquet File")with gr.Column(scale=1):conversion_type = gr.Radio(choices=["CSV to Parquet", "Parquet to CSV", "CSV to JSONL", "Parquet to JSONL"],label="Conversion Type")# Optional URL input for Parquet to JSONL conversionparquet_url = gr.Textbox(label="Parquet File URL (Optional)", placeholder="Enter URL if not uploading a file")convert_button = gr.Button("Convert", elem_classes=["gradio-button"])with gr.Row():output_file = gr.File(label="Converted File")preview = gr.Textbox(label="Preview (Top 10 Rows)", lines=15)convert_button.click(fn=dataset_converter,inputs=[input_file, conversion_type, parquet_url],outputs=[output_file, preview])demo.launch()23 unmodified lines242526272829303 unmodified lines343536373839403 unmodified lines4445464748495019 unmodified lines7071727374757677787980818216 unmodified lines9910010110210310410510610710810911011131 unmodified lines14314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817923 unmodified lines# Conversion: CSV to Parquetif conversion_type == "CSV to Parquet":if input_file is None or file_extension != "csv":raise ValueError("For CSV to Parquet conversion, please upload a CSV file. π")df = pd.read_csv(BytesIO(file_bytes))output_file = "output.parquet"df.to_parquet(output_file, index=False)3 unmodified lines# Conversion: Parquet to CSVelif conversion_type == "Parquet to CSV":if input_file is None or file_extension != "parquet":raise ValueError("For Parquet to CSV conversion, please upload a Parquet file. π")df = pd.read_parquet(BytesIO(file_bytes))output_file = "output.csv"df.to_csv(output_file, index=False)3 unmodified lines# Conversion: CSV to JSONLelif conversion_type == "CSV to JSONL":if input_file is None or file_extension != "csv":raise ValueError("For CSV to JSONL conversion, please upload a CSV file. π")# Read CSV with latin1 encodingdf = pd.read_csv(BytesIO(file_bytes), encoding='latin1')output_file = "metadata.jsonl"19 unmodified lines# Use uploaded file if available; otherwise try the provided URLif input_file is not None:df = pd.read_parquet(BytesIO(file_bytes))elif parquet_url:response = requests.get(parquet_url)response.raise_for_status() # Ensure the request was successfuldf = pd.read_parquet(BytesIO(response.content))file_name = "from_url.parquet"else:raise ValueError("For Parquet to JSONL conversion, please upload a file or provide a URL. π")output_file = "output.jsonl"# Recursive function to decode bytes to UTF-8 strings16 unmodified linespreview_str = df.head(10).to_string(index=False)else:raise ValueError("Invalid conversion type selected. β οΈ")info_message = (f"Input file: {file_name if file_name is not None else 'N/A'}\n"f"Converted file format: {converted_format}\n\n"f"Preview (Top 10 Rows):\n{preview_str}\n\n""Community: https://discord.gg/openfreeai π")return output_file, info_message31 unmodified lines}"""with gr.Blocks(css=custom_css, title="Datasets Convertor") as demo:gr.Markdown("# Datasets Convertor π")gr.Markdown("Upload a CSV or Parquet file (or provide a Parquet file URL for Parquet to JSONL conversion) ""and select the conversion type. The app converts the file to the desired format and displays a preview of the top 10 rows. β¨")with gr.Row():with gr.Column(scale=1):input_file = gr.File(label="Upload CSV or Parquet File π")with gr.Column(scale=1):conversion_type = gr.Radio(choices=["CSV to Parquet", "Parquet to CSV", "CSV to JSONL", "Parquet to JSONL"],label="Conversion Type π")# Optional URL input for Parquet to JSONL conversionparquet_url = gr.Textbox(label="Parquet File URL (Optional) π", placeholder="Enter URL if not uploading a file")convert_button = gr.Button("Convert β‘", elem_classes=["gradio-button"])with gr.Row():output_file = gr.File(label="Converted File πΎ")preview = gr.Textbox(label="Preview (Top 10 Rows) π", lines=15)convert_button.click(fn=dataset_converter,inputs=[input_file, conversion_type, parquet_url],outputs=[output_file, preview])gr.Markdown("**Join our Community:** [https://discord.gg/openfreeai](https://discord.gg/openfreeai) π€")demo.launch()
app.py+18-16