Oranblock commited on
Commit
44497dc
·
verified ·
1 Parent(s): 7ce7698

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -31
app.py CHANGED
@@ -1,45 +1,48 @@
1
  import gradio as gr
2
  import json
3
- from jsonschema import validate, ValidationError, SchemaError
4
 
5
- # Define your JSON schema
6
- schema = {
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 dictionary
21
- json_object = json.loads(json_data)
22
 
23
- # Validate the JSON data against the schema
24
- validate(instance=json_object, schema=schema)
25
- return "JSON is valid."
26
-
 
 
 
 
 
 
 
 
27
  except json.JSONDecodeError as e:
28
- return f"Invalid JSON: {e}"
 
 
 
 
 
 
 
 
29
 
30
- except ValidationError as e:
31
- return f"JSON validation error: {e.message}"
32
 
33
- except SchemaError as e:
34
- return f"Schema error: {e}"
35
 
36
  # Gradio interface
37
  iface = gr.Interface(
38
- fn=validate_json,
39
- inputs=gr.Textbox(lines=20, placeholder="Paste JSON here..."),
40
- outputs="text",
41
- title="JSON Validator",
42
- description="Validate JSON against a predefined schema."
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()