Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,32 @@ import requests
|
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
def convert_parquet_to_jsonl(parquet_file=None, parquet_url=None):
|
|
|
|
| 7 |
if parquet_file is not None:
|
| 8 |
df = pd.read_parquet(parquet_file.name)
|
| 9 |
elif parquet_url is not None:
|
| 10 |
response = requests.get(parquet_url)
|
| 11 |
-
response.raise_for_status()
|
| 12 |
df = pd.read_parquet(BytesIO(response.content))
|
| 13 |
else:
|
| 14 |
raise ValueError("Either parquet_file or parquet_url must be provided")
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
output_file_path = "output.jsonl"
|
| 18 |
-
with open(output_file_path, "w") as f:
|
| 19 |
f.write(jsonl_data)
|
| 20 |
|
| 21 |
return output_file_path
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
def convert_parquet_to_jsonl(parquet_file=None, parquet_url=None):
|
| 7 |
+
# Read Parquet from file or 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()
|
| 13 |
df = pd.read_parquet(BytesIO(response.content))
|
| 14 |
else:
|
| 15 |
raise ValueError("Either parquet_file or parquet_url must be provided")
|
| 16 |
|
| 17 |
+
# Function to clean any invalid UTF-8 sequences by replacing them
|
| 18 |
+
def clean_string(val):
|
| 19 |
+
if isinstance(val, str):
|
| 20 |
+
# Re-encode string to UTF-8, replacing invalid characters
|
| 21 |
+
return val.encode("utf-8", errors="replace").decode("utf-8", errors="replace")
|
| 22 |
+
return val
|
| 23 |
+
|
| 24 |
+
# Apply cleaning to every cell in the DataFrame
|
| 25 |
+
df = df.applymap(clean_string)
|
| 26 |
+
|
| 27 |
+
# Now safely convert to JSON Lines
|
| 28 |
+
jsonl_data = df.to_json(orient="records", lines=True)
|
| 29 |
+
|
| 30 |
+
# Write out to a file
|
| 31 |
output_file_path = "output.jsonl"
|
| 32 |
+
with open(output_file_path, "w", encoding="utf-8") as f:
|
| 33 |
f.write(jsonl_data)
|
| 34 |
|
| 35 |
return output_file_path
|