Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,9 @@ import json
|
|
| 5 |
def minify_json(input_text, uploaded_file):
|
| 6 |
try:
|
| 7 |
if uploaded_file is not None:
|
| 8 |
-
# Correct handling of the uploaded file
|
| 9 |
input_text = uploaded_file["data"].decode("utf-8")
|
| 10 |
|
| 11 |
-
# Load JSON content from input text or file
|
| 12 |
json_content = json.loads(input_text)
|
| 13 |
-
# Minify JSON (compact representation)
|
| 14 |
minified_json = json.dumps(json_content, separators=(',', ':'))
|
| 15 |
return minified_json
|
| 16 |
except json.JSONDecodeError:
|
|
@@ -22,6 +19,24 @@ minify = gr.Interface(
|
|
| 22 |
"text"
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Function for the Should we translate? interface
|
| 26 |
def check_input(input_text, uploaded_file):
|
| 27 |
if uploaded_file is not None:
|
|
@@ -39,8 +54,8 @@ should_we_translate = gr.Interface(
|
|
| 39 |
"text"
|
| 40 |
)
|
| 41 |
|
| 42 |
-
# Creating a Tabbed Interface with
|
| 43 |
-
demo = gr.TabbedInterface([minify, should_we_translate], ["Minify JSON", "Should we translate?"])
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
demo.launch()
|
|
|
|
| 5 |
def minify_json(input_text, uploaded_file):
|
| 6 |
try:
|
| 7 |
if uploaded_file is not None:
|
|
|
|
| 8 |
input_text = uploaded_file["data"].decode("utf-8")
|
| 9 |
|
|
|
|
| 10 |
json_content = json.loads(input_text)
|
|
|
|
| 11 |
minified_json = json.dumps(json_content, separators=(',', ':'))
|
| 12 |
return minified_json
|
| 13 |
except json.JSONDecodeError:
|
|
|
|
| 19 |
"text"
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Function to minify JSON with each key on a new line
|
| 23 |
+
def minify_json_to_row(input_text, uploaded_file):
|
| 24 |
+
try:
|
| 25 |
+
if uploaded_file is not None:
|
| 26 |
+
input_text = uploaded_file["data"].decode("utf-8")
|
| 27 |
+
|
| 28 |
+
json_content = json.loads(input_text)
|
| 29 |
+
minified_json = json.dumps(json_content, indent=1).replace('\n ', '\n').lstrip()
|
| 30 |
+
return minified_json
|
| 31 |
+
except json.JSONDecodeError:
|
| 32 |
+
return "Invalid JSON input. Please provide a valid JSON."
|
| 33 |
+
|
| 34 |
+
minify_to_row = gr.Interface(
|
| 35 |
+
minify_json_to_row,
|
| 36 |
+
[gr.Textbox(label="Input JSON Text"), gr.File(label="Upload JSON File")],
|
| 37 |
+
"text"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
# Function for the Should we translate? interface
|
| 41 |
def check_input(input_text, uploaded_file):
|
| 42 |
if uploaded_file is not None:
|
|
|
|
| 54 |
"text"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Creating a Tabbed Interface with all functionalities
|
| 58 |
+
demo = gr.TabbedInterface([minify, should_we_translate, minify_to_row], ["Minify JSON", "Should we translate?", "Minify to row"])
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|