Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,17 @@ import json
|
|
| 3 |
|
| 4 |
# Function to remove duplicates and fix JSON
|
| 5 |
def process_json(json_data):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
# Parse the JSON string to a list of dictionaries
|
| 8 |
data = json.loads(json_data)
|
| 9 |
|
| 10 |
# Check if data is a list of dictionaries
|
| 11 |
if not isinstance(data, list):
|
| 12 |
-
return None, "Error: JSON is not a list of objects"
|
| 13 |
|
| 14 |
# Remove duplicates by converting the list to a set of tuples, then back to a list of dicts
|
| 15 |
unique_data = [dict(t) for t in {tuple(d.items()) for d in data}]
|
|
@@ -17,10 +21,10 @@ def process_json(json_data):
|
|
| 17 |
# Convert the list back to JSON
|
| 18 |
cleaned_json = json.dumps(unique_data, indent=4)
|
| 19 |
|
| 20 |
-
return cleaned_json, None
|
| 21 |
|
| 22 |
except json.JSONDecodeError as e:
|
| 23 |
-
return None, f"Invalid JSON: {e}"
|
| 24 |
|
| 25 |
# Function to handle file uploads and processing
|
| 26 |
def process_file(uploaded_file):
|
|
@@ -28,21 +32,21 @@ def process_file(uploaded_file):
|
|
| 28 |
json_data = uploaded_file # This is already the content of the file as a string
|
| 29 |
|
| 30 |
# Process the JSON data
|
| 31 |
-
cleaned_json, error = process_json(json_data)
|
| 32 |
|
| 33 |
if error:
|
| 34 |
-
return None, error
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
return cleaned_json, "
|
| 38 |
|
| 39 |
# Gradio interface
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=process_file,
|
| 42 |
inputs=gr.File(label="Upload your JSON file"),
|
| 43 |
-
outputs=[gr.File(label="Download cleaned JSON file")
|
| 44 |
-
title="JSON Cleaner",
|
| 45 |
-
description="Upload a JSON file to remove duplicates
|
| 46 |
)
|
| 47 |
|
| 48 |
iface.launch()
|
|
|
|
| 3 |
|
| 4 |
# Function to remove duplicates and fix JSON
|
| 5 |
def process_json(json_data):
|
| 6 |
+
# Check if the uploaded JSON data is empty
|
| 7 |
+
if not json_data.strip():
|
| 8 |
+
return None, "Error: The uploaded file is empty.", None
|
| 9 |
+
|
| 10 |
try:
|
| 11 |
# Parse the JSON string to a list of dictionaries
|
| 12 |
data = json.loads(json_data)
|
| 13 |
|
| 14 |
# Check if data is a list of dictionaries
|
| 15 |
if not isinstance(data, list):
|
| 16 |
+
return None, "Error: JSON is not a list of objects", None
|
| 17 |
|
| 18 |
# Remove duplicates by converting the list to a set of tuples, then back to a list of dicts
|
| 19 |
unique_data = [dict(t) for t in {tuple(d.items()) for d in data}]
|
|
|
|
| 21 |
# Convert the list back to JSON
|
| 22 |
cleaned_json = json.dumps(unique_data, indent=4)
|
| 23 |
|
| 24 |
+
return cleaned_json, None, cleaned_json
|
| 25 |
|
| 26 |
except json.JSONDecodeError as e:
|
| 27 |
+
return None, f"Invalid JSON: {e}", None
|
| 28 |
|
| 29 |
# Function to handle file uploads and processing
|
| 30 |
def process_file(uploaded_file):
|
|
|
|
| 32 |
json_data = uploaded_file # This is already the content of the file as a string
|
| 33 |
|
| 34 |
# Process the JSON data
|
| 35 |
+
cleaned_json, error, download_json = process_json(json_data)
|
| 36 |
|
| 37 |
if error:
|
| 38 |
+
return None, error, None
|
| 39 |
|
| 40 |
+
# Return the cleaned JSON for display and as a downloadable file
|
| 41 |
+
return cleaned_json, "JSON is valid and cleaned.", download_json
|
| 42 |
|
| 43 |
# Gradio interface
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=process_file,
|
| 46 |
inputs=gr.File(label="Upload your JSON file"),
|
| 47 |
+
outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")],
|
| 48 |
+
title="JSON Cleaner and Fixer",
|
| 49 |
+
description="Upload a JSON file to remove duplicates, fix its structure, and download the cleaned version."
|
| 50 |
)
|
| 51 |
|
| 52 |
iface.launch()
|