Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
import jsonlines
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def convert_parquet_to_jsonl(file):
|
| 9 |
+
df = pd.read_parquet(file.name)
|
| 10 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jsonl')
|
| 11 |
+
df.to_json(output_file.name, orient='records', lines=True)
|
| 12 |
+
return output_file.name
|
| 13 |
+
|
| 14 |
+
def convert_json_to_jsonl(file):
|
| 15 |
+
with open(file.name, 'r') as f:
|
| 16 |
+
data = json.load(f)
|
| 17 |
+
|
| 18 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jsonl')
|
| 19 |
+
with jsonlines.open(output_file.name, mode='w') as writer:
|
| 20 |
+
if isinstance(data, list):
|
| 21 |
+
writer.write_all(data)
|
| 22 |
+
else:
|
| 23 |
+
writer.write(data)
|
| 24 |
+
|
| 25 |
+
return output_file.name
|
| 26 |
+
|
| 27 |
+
def convert_file(file):
|
| 28 |
+
if file.name.endswith('.parquet'):
|
| 29 |
+
return convert_parquet_to_jsonl(file)
|
| 30 |
+
elif file.name.endswith('.json'):
|
| 31 |
+
return convert_json_to_jsonl(file)
|
| 32 |
+
else:
|
| 33 |
+
return "Unsupported file format. Please upload a .parquet or .json file."
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=convert_file,
|
| 37 |
+
inputs=gr.File(label="Upload .parquet or .json file"),
|
| 38 |
+
outputs=gr.File(label="Download converted .jsonl file"),
|
| 39 |
+
title="Parquet/JSON to JSONL Converter",
|
| 40 |
+
description="Upload a .parquet or .json file to convert it to .jsonl format."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
iface.launch()
|