kivilaid commited on
Commit
3158622
·
1 Parent(s): 171689f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
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
- # Load JSON from string or file
7
- if isinstance(input_json, str):
8
  data = json.loads(input_json)
9
- else: # file input
10
- data = json.load(input_json)
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", optional=True)
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),