Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +59 -9
- app.py +221 -0
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -1,13 +1,63 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: π
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
|
| 8 |
-
python_version: '3.13'
|
| 9 |
-
app_file: app.py
|
| 10 |
-
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: JSON/CSV Converter
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: gradio
|
| 4 |
+
app_port: 7860
|
|
|
|
|
|
|
|
|
|
| 5 |
---
|
| 6 |
|
| 7 |
+
# π JSON/CSV Converter
|
| 8 |
+
|
| 9 |
+
Convert between JSON and CSV formats with customizable options.
|
| 10 |
+
|
| 11 |
+
## Features
|
| 12 |
+
|
| 13 |
+
- **JSON to CSV** - Convert JSON objects/arrays to CSV format
|
| 14 |
+
- **CSV to JSON** - Convert CSV data to JSON objects
|
| 15 |
+
- **Custom Delimiters** - Support for comma, semicolon, tab, pipe
|
| 16 |
+
- **Custom Quotes** - Choose quote characters for CSV
|
| 17 |
+
- **Type Preservation** - Automatically detects numbers, booleans, strings
|
| 18 |
+
- **Nested Objects** - Handles complex JSON structures
|
| 19 |
+
|
| 20 |
+
## How to Use
|
| 21 |
+
|
| 22 |
+
### JSON to CSV
|
| 23 |
+
1. Paste your JSON data
|
| 24 |
+
2. Choose delimiter and quote character
|
| 25 |
+
3. Click "Convert to CSV"
|
| 26 |
+
4. Copy the CSV output
|
| 27 |
+
|
| 28 |
+
### CSV to JSON
|
| 29 |
+
1. Paste your CSV data
|
| 30 |
+
2. Choose delimiter
|
| 31 |
+
3. Set if first row is header
|
| 32 |
+
4. Click "Convert to JSON"
|
| 33 |
+
5. Copy the JSON output
|
| 34 |
+
|
| 35 |
+
## Example
|
| 36 |
+
|
| 37 |
+
### JSON Input
|
| 38 |
+
```json
|
| 39 |
+
[
|
| 40 |
+
{"name": "John", "age": 30, "city": "New York"},
|
| 41 |
+
{"name": "Jane", "age": 25, "city": "Boston"}
|
| 42 |
+
]
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
### CSV Output
|
| 46 |
+
```csv
|
| 47 |
+
name,age,city
|
| 48 |
+
John,30,New York
|
| 49 |
+
Jane,25,Boston
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## Built With
|
| 53 |
+
|
| 54 |
+
- **Gradio** - For the interactive UI
|
| 55 |
+
- **Python** - For the backend logic
|
| 56 |
+
- **HuggingFace Spaces** - For free hosting
|
| 57 |
+
|
| 58 |
+
## License
|
| 59 |
+
|
| 60 |
+
MIT License - Free for personal and commercial use.
|
| 61 |
+
|
| 62 |
+
---
|
| 63 |
+
*Built by AI, tested by humans*
|
app.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
JSON to CSV Converter - HuggingFace Space
|
| 3 |
+
Convert JSON data to CSV format with customizable options.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import json
|
| 8 |
+
import csv
|
| 9 |
+
import io
|
| 10 |
+
|
| 11 |
+
def convert_json_to_csv(json_input, delimiter=",", quote_char='"', include_headers=True):
|
| 12 |
+
"""Convert JSON data to CSV format"""
|
| 13 |
+
if not json_input.strip():
|
| 14 |
+
return "Please enter JSON data", ""
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Parse JSON
|
| 18 |
+
data = json.loads(json_input)
|
| 19 |
+
|
| 20 |
+
# Handle different JSON structures
|
| 21 |
+
if isinstance(data, dict):
|
| 22 |
+
# If it's a single object, convert to list with one item
|
| 23 |
+
data = [data]
|
| 24 |
+
elif not isinstance(data, list):
|
| 25 |
+
return "β Invalid JSON structure: Expected object or array", ""
|
| 26 |
+
|
| 27 |
+
if not data:
|
| 28 |
+
return "β Empty JSON array", ""
|
| 29 |
+
|
| 30 |
+
# Get all unique keys from all objects
|
| 31 |
+
all_keys = []
|
| 32 |
+
for item in data:
|
| 33 |
+
if isinstance(item, dict):
|
| 34 |
+
for key in item.keys():
|
| 35 |
+
if key not in all_keys:
|
| 36 |
+
all_keys.append(key)
|
| 37 |
+
|
| 38 |
+
# Create CSV content
|
| 39 |
+
output = io.StringIO()
|
| 40 |
+
writer = csv.writer(output, delimiter=delimiter, quotechar=quote_char,
|
| 41 |
+
quoting=csv.QUOTE_MINIMAL)
|
| 42 |
+
|
| 43 |
+
# Add headers
|
| 44 |
+
if include_headers:
|
| 45 |
+
writer.writerow(all_keys)
|
| 46 |
+
|
| 47 |
+
# Add data rows
|
| 48 |
+
for item in data:
|
| 49 |
+
if isinstance(item, dict):
|
| 50 |
+
row = []
|
| 51 |
+
for key in all_keys:
|
| 52 |
+
value = item.get(key, "")
|
| 53 |
+
# Handle nested objects/arrays
|
| 54 |
+
if isinstance(value, (dict, list)):
|
| 55 |
+
value = json.dumps(value)
|
| 56 |
+
row.append(value)
|
| 57 |
+
writer.writerow(row)
|
| 58 |
+
else:
|
| 59 |
+
writer.writerow([item])
|
| 60 |
+
|
| 61 |
+
csv_content = output.getvalue()
|
| 62 |
+
output.close()
|
| 63 |
+
|
| 64 |
+
success_msg = f"β
Converted {len(data)} records to CSV\n"
|
| 65 |
+
success_msg += f"π {len(all_keys)} columns detected\n"
|
| 66 |
+
success_msg += f"π Columns: {', '.join(all_keys)}"
|
| 67 |
+
|
| 68 |
+
return success_msg, csv_content
|
| 69 |
+
|
| 70 |
+
except json.JSONDecodeError as e:
|
| 71 |
+
return f"β Invalid JSON: {str(e)}", ""
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"β Conversion error: {str(e)}", ""
|
| 74 |
+
|
| 75 |
+
def convert_csv_to_json(csv_input, first_row_is_header=True, delimiter=","):
|
| 76 |
+
"""Convert CSV data to JSON format"""
|
| 77 |
+
if not csv_input.strip():
|
| 78 |
+
return "Please enter CSV data", ""
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
# Parse CSV
|
| 82 |
+
lines = csv_input.strip().split('\n')
|
| 83 |
+
reader = csv.reader(lines, delimiter=delimiter)
|
| 84 |
+
|
| 85 |
+
rows = list(reader)
|
| 86 |
+
if not rows:
|
| 87 |
+
return "β Empty CSV data", ""
|
| 88 |
+
|
| 89 |
+
result = []
|
| 90 |
+
|
| 91 |
+
if first_row_is_header:
|
| 92 |
+
headers = rows[0]
|
| 93 |
+
data_rows = rows[1:]
|
| 94 |
+
else:
|
| 95 |
+
# Generate headers
|
| 96 |
+
max_cols = max(len(row) for row in rows)
|
| 97 |
+
headers = [f"column_{i}" for i in range(max_cols)]
|
| 98 |
+
data_rows = rows
|
| 99 |
+
|
| 100 |
+
# Convert rows to objects
|
| 101 |
+
for row in data_rows:
|
| 102 |
+
if row: # Skip empty rows
|
| 103 |
+
obj = {}
|
| 104 |
+
for i, header in enumerate(headers):
|
| 105 |
+
if i < len(row):
|
| 106 |
+
value = row[i]
|
| 107 |
+
# Try to convert to appropriate type
|
| 108 |
+
if value.lower() in ('true', 'false'):
|
| 109 |
+
value = value.lower() == 'true'
|
| 110 |
+
elif value.isdigit():
|
| 111 |
+
value = int(value)
|
| 112 |
+
else:
|
| 113 |
+
try:
|
| 114 |
+
value = float(value)
|
| 115 |
+
except ValueError:
|
| 116 |
+
pass
|
| 117 |
+
obj[header] = value
|
| 118 |
+
result.append(obj)
|
| 119 |
+
|
| 120 |
+
json_output = json.dumps(result, indent=2)
|
| 121 |
+
success_msg = f"β
Converted {len(result)} records to JSON\n"
|
| 122 |
+
success_msg += f"π {len(headers)} columns\n"
|
| 123 |
+
success_msg += f"π Headers: {', '.join(headers)}"
|
| 124 |
+
|
| 125 |
+
return success_msg, json_output
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
return f"β Conversion error: {str(e)}", ""
|
| 129 |
+
|
| 130 |
+
# Gradio interface
|
| 131 |
+
with gr.Blocks(title="JSON/CSV Converter") as demo:
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
# π JSON/CSV Converter
|
| 134 |
+
|
| 135 |
+
Convert between JSON and CSV formats with customizable options.
|
| 136 |
+
|
| 137 |
+
**Features:**
|
| 138 |
+
- JSON to CSV conversion
|
| 139 |
+
- CSV to JSON conversion
|
| 140 |
+
- Custom delimiters and quotes
|
| 141 |
+
- Handles nested objects and arrays
|
| 142 |
+
- Preserves data types
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
+
with gr.Tabs():
|
| 146 |
+
with gr.TabItem("JSON β CSV"):
|
| 147 |
+
with gr.Row():
|
| 148 |
+
with gr.Column():
|
| 149 |
+
json_input = gr.Textbox(
|
| 150 |
+
label="JSON Input",
|
| 151 |
+
placeholder='Enter JSON data (e.g., [{"name": "John", "age": 30}])',
|
| 152 |
+
lines=10
|
| 153 |
+
)
|
| 154 |
+
with gr.Row():
|
| 155 |
+
delimiter = gr.Dropdown(
|
| 156 |
+
choices=[",", ";", "\t", "|"],
|
| 157 |
+
value=",",
|
| 158 |
+
label="Delimiter"
|
| 159 |
+
)
|
| 160 |
+
quote_char = gr.Dropdown(
|
| 161 |
+
choices=['"', "'", ''],
|
| 162 |
+
value='"',
|
| 163 |
+
label="Quote Character"
|
| 164 |
+
)
|
| 165 |
+
include_headers = gr.Checkbox(
|
| 166 |
+
value=True,
|
| 167 |
+
label="Include Headers"
|
| 168 |
+
)
|
| 169 |
+
json_to_csv_btn = gr.Button("Convert to CSV", variant="primary")
|
| 170 |
+
|
| 171 |
+
with gr.Column():
|
| 172 |
+
json_result = gr.Textbox(label="Conversion Status", lines=3)
|
| 173 |
+
csv_output = gr.Textbox(
|
| 174 |
+
label="CSV Output",
|
| 175 |
+
lines=10,
|
| 176 |
+
interactive=False
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
with gr.TabItem("CSV β JSON"):
|
| 180 |
+
with gr.Row():
|
| 181 |
+
with gr.Column():
|
| 182 |
+
csv_input = gr.Textbox(
|
| 183 |
+
label="CSV Input",
|
| 184 |
+
placeholder="name,age,city\nJohn,30,New York\nJane,25,Boston",
|
| 185 |
+
lines=10
|
| 186 |
+
)
|
| 187 |
+
with gr.Row():
|
| 188 |
+
csv_delimiter = gr.Dropdown(
|
| 189 |
+
choices=[",", ";", "\t", "|"],
|
| 190 |
+
value=",",
|
| 191 |
+
label="Delimiter"
|
| 192 |
+
)
|
| 193 |
+
first_row_header = gr.Checkbox(
|
| 194 |
+
value=True,
|
| 195 |
+
label="First Row is Header"
|
| 196 |
+
)
|
| 197 |
+
csv_to_json_btn = gr.Button("Convert to JSON", variant="primary")
|
| 198 |
+
|
| 199 |
+
with gr.Column():
|
| 200 |
+
csv_result = gr.Textbox(label="Conversion Status", lines=3)
|
| 201 |
+
json_output = gr.Textbox(
|
| 202 |
+
label="JSON Output",
|
| 203 |
+
lines=10,
|
| 204 |
+
interactive=False
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# Event handlers
|
| 208 |
+
json_to_csv_btn.click(
|
| 209 |
+
fn=convert_json_to_csv,
|
| 210 |
+
inputs=[json_input, delimiter, quote_char, include_headers],
|
| 211 |
+
outputs=[json_result, csv_output]
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
csv_to_json_btn.click(
|
| 215 |
+
fn=convert_csv_to_json,
|
| 216 |
+
inputs=[csv_input, first_row_header, csv_delimiter],
|
| 217 |
+
outputs=[csv_result, json_output]
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
if __name__ == "__main__":
|
| 221 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|