Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
|
| 4 |
-
def minify_json(input_json):
|
| 5 |
try:
|
| 6 |
-
#
|
| 7 |
-
if
|
| 8 |
data = json.loads(input_json)
|
| 9 |
-
|
| 10 |
-
data = json.load(
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
# Minify the JSON
|
| 13 |
minified_json = json.dumps(data, separators=(',', ':'))
|
| 14 |
-
|
| 15 |
return minified_json, minified_json
|
| 16 |
-
except json.JSONDecodeError:
|
| 17 |
-
return "Invalid JSON format
|
| 18 |
|
| 19 |
# Define the Gradio interface
|
| 20 |
iface = gr.Interface(
|
| 21 |
fn=minify_json,
|
| 22 |
inputs=[
|
| 23 |
gr.Textbox(label="Input JSON (Text)", placeholder="Enter JSON here...", lines=10),
|
| 24 |
-
gr.File(label="Input JSON (File)", type="file"
|
| 25 |
],
|
| 26 |
outputs=[
|
| 27 |
gr.Textbox(label="Minified JSON (Text)", lines=10),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
|
| 4 |
+
def minify_json(input_json=None, input_file=None):
|
| 5 |
try:
|
| 6 |
+
# Determine input type (text or file)
|
| 7 |
+
if input_json:
|
| 8 |
data = json.loads(input_json)
|
| 9 |
+
elif input_file:
|
| 10 |
+
data = json.load(input_file)
|
| 11 |
+
else:
|
| 12 |
+
return "No input provided.", None
|
| 13 |
+
|
| 14 |
# Minify the JSON
|
| 15 |
minified_json = json.dumps(data, separators=(',', ':'))
|
|
|
|
| 16 |
return minified_json, minified_json
|
| 17 |
+
except json.JSONDecodeError as e:
|
| 18 |
+
return f"Invalid JSON format: {str(e)}", None
|
| 19 |
|
| 20 |
# Define the Gradio interface
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=minify_json,
|
| 23 |
inputs=[
|
| 24 |
gr.Textbox(label="Input JSON (Text)", placeholder="Enter JSON here...", lines=10),
|
| 25 |
+
gr.File(label="Input JSON (File)", type="file")
|
| 26 |
],
|
| 27 |
outputs=[
|
| 28 |
gr.Textbox(label="Minified JSON (Text)", lines=10),
|