Spaces:
Running
Running
File size: 2,138 Bytes
d2b9031 49e25d2 ff86828 d2b9031 6a1564b 0df8fba 6a1564b 0df8fba 6a1564b 0df8fba 6a1564b 2bf1e25 6a1564b 0df8fba 6a1564b 0df8fba 6a1564b 2bf1e25 90f89f0 6a1564b 90f89f0 7773ef1 49e25d2 6a1564b 0df8fba 6a1564b 0df8fba 2bf1e25 90f89f0 fcd8f70 6a1564b 90f89f0 6a1564b 90f89f0 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 64 65 |
import gradio as gr
import pandas as pd
from io import BytesIO
def convert_file(input_file, conversion_type):
# Check if a file was uploaded
if input_file is None:
raise ValueError("Please upload a file.")
file_name = input_file.name
file_extension = file_name.lower().split('.')[-1]
file_bytes = input_file.read()
df = None
output_file = None
converted_format = None
# Conversion: CSV to Parquet
if conversion_type == "CSV to Parquet":
if 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)
converted_format = "Parquet"
# Conversion: Parquet to CSV
elif conversion_type == "Parquet to CSV":
if 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)
converted_format = "CSV"
else:
raise ValueError("Invalid conversion type selected.")
# Generate a preview of the top 10 rows
preview = df.head(10).to_string(index=False)
info_message = (
f"Input file: {file_name}\n"
f"Converted file format: {converted_format}\n\n"
f"Preview (Top 10 Rows):\n{preview}"
)
return output_file, info_message
demo = gr.Interface(
fn=convert_file,
inputs=[
gr.File(label="Upload 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)", lines=15)
],
title="CSV <-> Parquet Converter",
description=(
"Upload a CSV or Parquet file and select the conversion type. "
"The app converts the file to the opposite format and displays a preview of the top 10 rows."
)
)
if __name__ == "__main__":
demo.launch()
|