Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
def generate_carbone_tags(json_input):
|
| 5 |
+
try:
|
| 6 |
+
# Parse the JSON input
|
| 7 |
+
data = json.loads(json_input)
|
| 8 |
+
|
| 9 |
+
# Function to recursively extract keys and format them in Carbone style
|
| 10 |
+
def extract_keys(obj, parent_key=''):
|
| 11 |
+
carbone_tags = []
|
| 12 |
+
if isinstance(obj, dict):
|
| 13 |
+
for k, v in obj.items():
|
| 14 |
+
full_key = f"{parent_key}.{k}" if parent_key else k
|
| 15 |
+
carbone_tags.extend(extract_keys(v, full_key))
|
| 16 |
+
elif isinstance(obj, list):
|
| 17 |
+
for i, item in enumerate(obj):
|
| 18 |
+
carbone_tags.extend(extract_keys(item, f"{parent_key}[{i}]"))
|
| 19 |
+
else:
|
| 20 |
+
carbone_tags.append(f"{{d.{parent_key}}}")
|
| 21 |
+
return carbone_tags
|
| 22 |
+
|
| 23 |
+
# Extract keys and format them
|
| 24 |
+
carbone_keys = extract_keys(data)
|
| 25 |
+
return "\n".join(carbone_keys)
|
| 26 |
+
except json.JSONDecodeError:
|
| 27 |
+
return "Invalid JSON. Please check your input."
|
| 28 |
+
|
| 29 |
+
# Create the Gradio interface
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=generate_carbone_tags,
|
| 32 |
+
inputs=gr.Textbox(lines=10, placeholder="Paste JSON here..."),
|
| 33 |
+
outputs=gr.Textbox(lines=10),
|
| 34 |
+
title="JSON to Carbone Tag Converter",
|
| 35 |
+
description="Paste JSON and get Carbone format tags. Each key in the JSON will be converted to the Carbone tag format {d.key}."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the interface
|
| 39 |
+
interface.launch()
|