Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,59 +3,63 @@ import pandas as pd
|
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
df = pd.read_csv(BytesIO(response.content))
|
| 23 |
-
else: # Parquet to CSV
|
| 24 |
-
df = pd.read_parquet(BytesIO(response.content))
|
| 25 |
-
else:
|
| 26 |
-
# νμΌ μ
λ‘λκ° μλ κ²½μ°
|
| 27 |
-
file_path = input_file.name
|
| 28 |
-
if conversion_type == "CSV to Parquet":
|
| 29 |
-
df = pd.read_csv(file_path)
|
| 30 |
-
else:
|
| 31 |
-
df = pd.read_parquet(file_path)
|
| 32 |
-
|
| 33 |
-
# λ³ν μ€ν: CSV to Parquet νΉμ Parquet to CSV
|
| 34 |
-
if conversion_type == "CSV to Parquet":
|
| 35 |
output_file = "output.parquet"
|
| 36 |
df.to_parquet(output_file, index=False)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
output_file = "output.csv"
|
| 39 |
df.to_csv(output_file, index=False)
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
preview = df.head(10).to_string(index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
return output_file,
|
| 45 |
|
| 46 |
demo = gr.Interface(
|
| 47 |
-
fn=
|
| 48 |
-
inputs=
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
],
|
| 53 |
outputs=[
|
| 54 |
-
gr.File(label="
|
| 55 |
-
gr.Textbox(label="
|
| 56 |
],
|
| 57 |
-
title="CSV <-> Parquet
|
| 58 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import requests
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
def convert_hf_dataset(file_url: str):
|
| 7 |
+
file_url = file_url.strip()
|
| 8 |
+
# Check that the URL is from Hugging Face
|
| 9 |
+
if "huggingface.co" not in file_url:
|
| 10 |
+
raise ValueError("Please provide a URL from Hugging Face datasets.")
|
| 11 |
|
| 12 |
+
# Ensure the URL has a scheme; if not, add "https://"
|
| 13 |
+
if not file_url.lower().startswith(("http://", "https://")):
|
| 14 |
+
file_url = "https://" + file_url
|
| 15 |
|
| 16 |
+
# Download the content from the URL
|
| 17 |
+
response = requests.get(file_url)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
content = response.content
|
| 20 |
+
|
| 21 |
+
# Determine file type from URL extension and convert accordingly
|
| 22 |
+
if file_url.lower().endswith(".csv"):
|
| 23 |
+
# If it's a CSV, read it and convert to Parquet
|
| 24 |
+
df = pd.read_csv(BytesIO(content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
output_file = "output.parquet"
|
| 26 |
df.to_parquet(output_file, index=False)
|
| 27 |
+
converted_format = "Parquet"
|
| 28 |
+
elif file_url.lower().endswith(".parquet"):
|
| 29 |
+
# If it's a Parquet file, read it and convert to CSV
|
| 30 |
+
df = pd.read_parquet(BytesIO(content))
|
| 31 |
output_file = "output.csv"
|
| 32 |
df.to_csv(output_file, index=False)
|
| 33 |
+
converted_format = "CSV"
|
| 34 |
+
else:
|
| 35 |
+
raise ValueError("The URL must point to a .csv or .parquet file.")
|
| 36 |
|
| 37 |
+
# Create a preview of the top 10 rows
|
| 38 |
preview = df.head(10).to_string(index=False)
|
| 39 |
+
info_message = (
|
| 40 |
+
f"Input file: {file_url.split('/')[-1]}\n"
|
| 41 |
+
f"Converted file format: {converted_format}\n\n"
|
| 42 |
+
f"Preview (Top 10 Rows):\n{preview}"
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
return output_file, info_message
|
| 46 |
|
| 47 |
demo = gr.Interface(
|
| 48 |
+
fn=convert_hf_dataset,
|
| 49 |
+
inputs=gr.Textbox(
|
| 50 |
+
label="Hugging Face Dataset URL",
|
| 51 |
+
placeholder="e.g., huggingface.co/datasets/username/dataset/filename.csv"
|
| 52 |
+
),
|
|
|
|
| 53 |
outputs=[
|
| 54 |
+
gr.File(label="Converted File"),
|
| 55 |
+
gr.Textbox(label="Preview (Top 10 Rows)", lines=15)
|
| 56 |
],
|
| 57 |
+
title="Hugging Face CSV <-> Parquet Converter",
|
| 58 |
+
description=(
|
| 59 |
+
"Enter the URL of a Hugging Face dataset file (must end with .csv or .parquet). "
|
| 60 |
+
"The app will automatically detect the file type, convert it to the opposite format, "
|
| 61 |
+
"and display a preview of the top 10 rows."
|
| 62 |
+
)
|
| 63 |
)
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|