Oranblock commited on
Commit
f3f88db
·
verified ·
1 Parent(s): 305e35f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.inputs.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()