Spaces:
Running
Running
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() | |