Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,40 @@ import pandas as pd
|
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
# Read Parquet from
|
| 8 |
if parquet_file is not None:
|
| 9 |
df = pd.read_parquet(parquet_file.name)
|
| 10 |
elif parquet_url is not None:
|
| 11 |
response = requests.get(parquet_url)
|
| 12 |
-
response.raise_for_status()
|
| 13 |
df = pd.read_parquet(BytesIO(response.content))
|
| 14 |
else:
|
| 15 |
raise ValueError("Either parquet_file or parquet_url must be provided")
|
| 16 |
-
|
| 17 |
-
# Clean string columns to
|
| 18 |
for col in df.select_dtypes(include=["object"]).columns:
|
| 19 |
df[col] = df[col].apply(
|
| 20 |
-
lambda x: x.encode("utf-8", errors="replace").decode("utf-8", errors="replace")
|
|
|
|
| 21 |
)
|
| 22 |
-
|
| 23 |
-
# Convert to JSON Lines
|
| 24 |
-
jsonl_data = df.to_json(orient="records", lines=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
with open(output_file_path, "w", encoding="utf-8") as f:
|
| 29 |
-
f.write(
|
| 30 |
-
|
| 31 |
return output_file_path
|
| 32 |
|
| 33 |
demo = gr.Interface(
|
| 34 |
-
fn=
|
| 35 |
inputs=[gr.File(label="Parquet File"), gr.Textbox(label="Parquet File URL")],
|
| 36 |
-
outputs=[gr.File(label="
|
| 37 |
-
title="Parquet to
|
| 38 |
-
description="Convert a Parquet file to
|
| 39 |
)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
def convert_parquet_to_csv(parquet_file=None, parquet_url=None):
|
| 7 |
+
# Read the Parquet file either from an upload or a URL
|
| 8 |
if parquet_file is not None:
|
| 9 |
df = pd.read_parquet(parquet_file.name)
|
| 10 |
elif parquet_url is not None:
|
| 11 |
response = requests.get(parquet_url)
|
| 12 |
+
response.raise_for_status() # Check that the request was successful
|
| 13 |
df = pd.read_parquet(BytesIO(response.content))
|
| 14 |
else:
|
| 15 |
raise ValueError("Either parquet_file or parquet_url must be provided")
|
| 16 |
+
|
| 17 |
+
# Clean string columns to handle any invalid UTF-8 sequences
|
| 18 |
for col in df.select_dtypes(include=["object"]).columns:
|
| 19 |
df[col] = df[col].apply(
|
| 20 |
+
lambda x: x.encode("utf-8", errors="replace").decode("utf-8", errors="replace")
|
| 21 |
+
if isinstance(x, str) else x
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Convert the DataFrame to CSV format
|
| 25 |
+
csv_data = df.to_csv(index=False)
|
| 26 |
+
|
| 27 |
+
# Save the CSV data to a file
|
| 28 |
+
output_file_path = "output.csv"
|
| 29 |
with open(output_file_path, "w", encoding="utf-8") as f:
|
| 30 |
+
f.write(csv_data)
|
| 31 |
+
|
| 32 |
return output_file_path
|
| 33 |
|
| 34 |
demo = gr.Interface(
|
| 35 |
+
fn=convert_parquet_to_csv,
|
| 36 |
inputs=[gr.File(label="Parquet File"), gr.Textbox(label="Parquet File URL")],
|
| 37 |
+
outputs=[gr.File(label="CSV Output")],
|
| 38 |
+
title="Parquet to CSV Converter",
|
| 39 |
+
description="Convert a Parquet file to CSV format from a downloadable link or file upload"
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|