minify / app.py
kivilaid's picture
Update app.py
4147187
Raw
History Blame
1.12 kB
import gradio as gr
import json
def minify_json(input_json=None, input_file=None):
try:
# Determine input type (text or file)
if input_json:
data = json.loads(input_json)
elif input_file:
data = json.load(input_file)
else:
return "No input provided.", None
# Minify the JSON
minified_json = json.dumps(data, separators=(',', ':'))
return minified_json, minified_json
except json.JSONDecodeError as e:
return f"Invalid JSON format: {str(e)}", None
# Define the Gradio interface
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."
)
# Launch the app
if __name__ == "__main__":
iface.launch()