Spaces:
Sleeping
Sleeping
File size: 3,960 Bytes
5eaaba5 a6a1c75 5eaaba5 a6a1c75 5eaaba5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import json
import gradio as gr
from utils.classify import get_functions_for_descriptions
from utils.extract import extract_descriptions
from utils.fake import generate_mock_data
from utils.validate import validate_schema_structure
from utils.serialize_json import serialize_to_json
# Store globally so we don't have to recalculate if
# the schema has not changed
function_mappings = {}
def process_schema(schema):
"""Process the schema and return either mock data or an error message"""
global function_mappings
is_valid, result = validate_schema_structure(schema)
if not is_valid:
return (
None,
result, # If invalid this will contain the error message
)
if not function_mappings:
descriptions = extract_descriptions(result)
function_mappings = get_functions_for_descriptions(descriptions)
mock_data = generate_mock_data(result, function_mappings)
mock_json = serialize_to_json(mock_data, pretty=True)
return mock_json, None
def clear_function_mappings():
global function_mappings
function_mappings = {}
# Create a default schema example
default_schema = json.dumps(
{
"type": "object",
"description": "A person object",
"properties": {
"first_name": {"type": "string", "description": "The person's first name"},
"last_name": {"type": "string", "description": "The person's last name"},
"age": {"type": "integer", "minimum": 18, "maximum": 100},
"email": {"type": "string"},
"is_active": {"type": "boolean"},
"address": {
"type": "object",
"properties": {
"street": {"type": "string"},
"city": {"type": "string"},
"zip": {"type": "string"},
},
},
},
"required": ["first_name", "last_name", "age"],
},
indent=2,
)
# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Base()) as app:
gr.Markdown("# JSON Schema Mock Data Generator")
gr.Markdown(
"Enter a valid JSON schema and generate mock data that conforms to the schema."
)
with gr.Row():
with gr.Column():
schema_input = gr.Textbox(
label="JSON Schema",
value=default_schema,
lines=15,
max_lines=15,
placeholder="Enter your JSON schema here...",
)
generate_btn = gr.Button("Generate Mock Data", variant="primary")
with gr.Column():
mock_output = gr.Textbox(
label="Generated Mock JSON", lines=15, max_lines=15, interactive=False
)
error_output = gr.Textbox(label="Errors", visible=False, interactive=False)
def update_output(schema_str):
mock_data, error = process_schema(schema_str)
if error:
return {
mock_output: None,
error_output: error,
error_output: gr.update(visible=True),
}
else:
return {
mock_output: mock_data,
error_output: None,
error_output: gr.update(visible=False),
}
schema_input.change(fn=clear_function_mappings, inputs=[], outputs=[])
generate_btn.click(
fn=update_output,
inputs=[schema_input],
outputs=[mock_output, error_output, error_output],
)
gr.Markdown(
"""
## Notes
- Zero Shot Classification will run the first time a mock is generated for a schema. Subsequent generations will be instant.
- The schema must be valid JSON and comply with JSON Schema Draft 7
- Required keywords: `type` and `properties` (for object types)
- Currently Supported types: string, integer, boolean, array, and object
"""
)
if __name__ == "__main__":
app.launch()
|