Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
-
from jsonschema import validate, ValidationError, SchemaError
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
"type": "object",
|
| 8 |
-
"properties": {
|
| 9 |
-
"name": {"type": "string"},
|
| 10 |
-
"age": {"type": "integer", "minimum": 0},
|
| 11 |
-
"email": {"type": "string", "format": "email"},
|
| 12 |
-
},
|
| 13 |
-
"required": ["name", "age", "email"],
|
| 14 |
-
"additionalProperties": False
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
# Function to validate JSON data
|
| 18 |
-
def validate_json(json_data):
|
| 19 |
try:
|
| 20 |
-
# Parse the JSON string to a
|
| 21 |
-
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except json.JSONDecodeError as e:
|
| 28 |
-
return f"Invalid JSON: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
# Gradio interface
|
| 37 |
iface = gr.Interface(
|
| 38 |
-
fn=
|
| 39 |
-
inputs=gr.
|
| 40 |
-
outputs="text",
|
| 41 |
-
title="JSON
|
| 42 |
-
description="
|
| 43 |
)
|
| 44 |
|
| 45 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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}]
|
| 16 |
+
|
| 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):
|
| 27 |
+
# Read the uploaded file
|
| 28 |
+
json_data = uploaded_file.read().decode("utf-8")
|
| 29 |
+
|
| 30 |
+
# Process the JSON data
|
| 31 |
+
cleaned_json, error = process_json(json_data)
|
| 32 |
|
| 33 |
+
if error:
|
| 34 |
+
return None, error
|
| 35 |
|
| 36 |
+
# Provide a downloadable cleaned JSON file
|
| 37 |
+
return cleaned_json, "cleaned_json.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"), "text"],
|
| 44 |
+
title="JSON Cleaner",
|
| 45 |
+
description="Upload a JSON file to remove duplicates and fix its structure."
|
| 46 |
)
|
| 47 |
|
| 48 |
iface.launch()
|