Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import os
|
4 |
-
from tempfile import TemporaryDirectory
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Create the Gradio interface
|
21 |
-
|
22 |
-
fn=
|
23 |
-
inputs=gr.
|
24 |
-
outputs=
|
25 |
-
title="
|
26 |
-
description="
|
27 |
)
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import json
|
|
|
|
|
3 |
|
4 |
+
def generate_carbone_tags_and_values(json_input):
|
5 |
+
try:
|
6 |
+
# Parse the JSON input
|
7 |
+
data = json.loads(json_input)
|
8 |
+
|
9 |
+
# Function to recursively extract keys, values, and format them in Carbone style
|
10 |
+
def extract_keys_and_values(obj, parent_key=''):
|
11 |
+
carbone_tags = []
|
12 |
+
carbone_tags_values = []
|
13 |
+
if isinstance(obj, dict):
|
14 |
+
for k, v in obj.items():
|
15 |
+
full_key = f"{parent_key}.{k}" if parent_key else k
|
16 |
+
tags, tags_values = extract_keys_and_values(v, full_key)
|
17 |
+
carbone_tags.extend(tags)
|
18 |
+
carbone_tags_values.extend(tags_values)
|
19 |
+
elif isinstance(obj, list):
|
20 |
+
for i, item in enumerate(obj):
|
21 |
+
tags, tags_values = extract_keys_and_values(item, f"{parent_key}[{i}]")
|
22 |
+
carbone_tags.extend(tags)
|
23 |
+
carbone_tags_values.extend(tags_values)
|
24 |
+
else:
|
25 |
+
carbone_tag = f"{{d.{parent_key}}}"
|
26 |
+
carbone_tags.append(carbone_tag)
|
27 |
+
carbone_tags_values.append((carbone_tag, obj))
|
28 |
+
return carbone_tags, carbone_tags_values
|
29 |
+
|
30 |
+
# Extract keys, values and format them
|
31 |
+
carbone_keys, carbone_keys_values = extract_keys_and_values(data)
|
32 |
+
|
33 |
+
# Sort the keys alphabetically
|
34 |
+
carbone_keys.sort()
|
35 |
+
carbone_keys_values.sort(key=lambda x: x[0])
|
36 |
+
|
37 |
+
# Format the outputs
|
38 |
+
tags_output = "\n".join(carbone_keys)
|
39 |
+
tags_values_output = "\n".join([f"{tag}: {value}" for tag, value in carbone_keys_values])
|
40 |
+
return tags_output, tags_values_output
|
41 |
+
except json.JSONDecodeError:
|
42 |
+
return "Invalid JSON. Please check your input.", ""
|
43 |
|
44 |
# Create the Gradio interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=generate_carbone_tags_and_values,
|
47 |
+
inputs=gr.Textbox(lines=20, placeholder="Paste JSON here..."),
|
48 |
+
outputs=[gr.Textbox(lines=20), gr.Textbox(lines=20)],
|
49 |
+
title="JSON to Carbone Tags and Values Converter",
|
50 |
+
description="Paste JSON to generate Carbone tags and their values. The first output box shows only tags, and the second shows tags with their corresponding values."
|
51 |
)
|
52 |
|
53 |
+
# Launch the interface
|
54 |
+
interface.launch()
|
|