| import gradio as gr |
| import json |
|
|
| def minify_json(input_json=None, input_file=None): |
| try: |
| |
| if input_json: |
| data = json.loads(input_json) |
| elif input_file: |
| data = json.load(input_file) |
| else: |
| return "No input provided.", None |
|
|
| |
| minified_json = json.dumps(data, separators=(',', ':')) |
| return minified_json, minified_json |
| except json.JSONDecodeError as e: |
| return f"Invalid JSON format: {str(e)}", None |
|
|
| |
| iface = gr.Interface( |
| fn=minify_json, |
| inputs=[ |
| gr.Textbox(label="Input JSON (Text)", placeholder="Enter JSON here...", lines=10), |
| gr.File(label="Input JSON (File)", type="file") |
| ], |
| outputs=[ |
| gr.Textbox(label="Minified JSON (Text)", lines=10, type="str"), |
| gr.File(label="Minified JSON (Download)", type="json") |
| ], |
| title="JSON Minifier", |
| description="Upload a JSON file or paste JSON text to minify it." |
| ) |
|
|
| |
| if __name__ == "__main__": |
| iface.launch() |
|
|