Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Function to remove duplicates and fix JSON
|
| 5 |
def process_json(json_data):
|
|
@@ -7,9 +30,14 @@ def process_json(json_data):
|
|
| 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(
|
| 13 |
|
| 14 |
# Check if data is a list of dictionaries
|
| 15 |
if not isinstance(data, list):
|
|
@@ -21,10 +49,10 @@ def process_json(json_data):
|
|
| 21 |
# Convert the list back to JSON
|
| 22 |
cleaned_json = json.dumps(unique_data, indent=4)
|
| 23 |
|
| 24 |
-
return 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,10 +60,10 @@ 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,
|
| 36 |
|
| 37 |
-
if
|
| 38 |
-
return
|
| 39 |
|
| 40 |
# Return the cleaned JSON for display and as a downloadable file
|
| 41 |
return cleaned_json, "JSON is valid and cleaned.", download_json
|
|
@@ -45,8 +73,8 @@ 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
|
| 50 |
)
|
| 51 |
|
| 52 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
+
import jsonfix
|
| 4 |
+
import json5
|
| 5 |
+
|
| 6 |
+
# Function to attempt to fix JSON using jsonfix and json5
|
| 7 |
+
def fix_json(json_data):
|
| 8 |
+
try:
|
| 9 |
+
# First, try parsing with json5 which is more lenient
|
| 10 |
+
parsed_data = json5.loads(json_data)
|
| 11 |
+
|
| 12 |
+
# Try to pretty-print (indent) the parsed data
|
| 13 |
+
fixed_json = json.dumps(parsed_data, indent=4)
|
| 14 |
+
return fixed_json, "JSON fixed using json5."
|
| 15 |
+
|
| 16 |
+
except Exception as e:
|
| 17 |
+
# If json5 fails, try jsonfix to attempt to auto-correct
|
| 18 |
+
try:
|
| 19 |
+
fixed_json = jsonfix.fix_data(json_data)
|
| 20 |
+
parsed_data = json.loads(fixed_json) # Validate it as valid JSON
|
| 21 |
+
fixed_json = json.dumps(parsed_data, indent=4)
|
| 22 |
+
return fixed_json, "JSON fixed using jsonfix."
|
| 23 |
+
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return None, f"Failed to fix JSON: {str(e)}"
|
| 26 |
|
| 27 |
# Function to remove duplicates and fix JSON
|
| 28 |
def process_json(json_data):
|
|
|
|
| 30 |
if not json_data.strip():
|
| 31 |
return None, "Error: The uploaded file is empty.", None
|
| 32 |
|
| 33 |
+
# Attempt to fix the JSON
|
| 34 |
+
fixed_json, fix_message = fix_json(json_data)
|
| 35 |
+
if fixed_json is None:
|
| 36 |
+
return None, fix_message, None
|
| 37 |
+
|
| 38 |
try:
|
| 39 |
+
# Parse the fixed JSON string to a list of dictionaries
|
| 40 |
+
data = json.loads(fixed_json)
|
| 41 |
|
| 42 |
# Check if data is a list of dictionaries
|
| 43 |
if not isinstance(data, list):
|
|
|
|
| 49 |
# Convert the list back to JSON
|
| 50 |
cleaned_json = json.dumps(unique_data, indent=4)
|
| 51 |
|
| 52 |
+
return cleaned_json, f"JSON is valid and cleaned. {fix_message}", cleaned_json
|
| 53 |
|
| 54 |
except json.JSONDecodeError as e:
|
| 55 |
+
return None, f"Invalid JSON after fixing: {e}", None
|
| 56 |
|
| 57 |
# Function to handle file uploads and processing
|
| 58 |
def process_file(uploaded_file):
|
|
|
|
| 60 |
json_data = uploaded_file # This is already the content of the file as a string
|
| 61 |
|
| 62 |
# Process the JSON data
|
| 63 |
+
cleaned_json, message, download_json = process_json(json_data)
|
| 64 |
|
| 65 |
+
if message:
|
| 66 |
+
return cleaned_json, message, download_json
|
| 67 |
|
| 68 |
# Return the cleaned JSON for display and as a downloadable file
|
| 69 |
return cleaned_json, "JSON is valid and cleaned.", download_json
|
|
|
|
| 73 |
fn=process_file,
|
| 74 |
inputs=gr.File(label="Upload your JSON file"),
|
| 75 |
outputs=[gr.JSON(label="Fixed JSON"), "text", gr.File(label="Download cleaned JSON file")],
|
| 76 |
+
title="Advanced JSON Cleaner and Fixer",
|
| 77 |
+
description="Upload a JSON file to automatically fix, remove duplicates, and download the cleaned version."
|
| 78 |
)
|
| 79 |
|
| 80 |
iface.launch()
|