|
import gradio as gr |
|
import json |
|
|
|
def generate_carbone_tags_and_values(json_input): |
|
try: |
|
|
|
data = json.loads(json_input) |
|
|
|
|
|
def extract_keys_and_values(obj, parent_key=''): |
|
carbone_tags = [] |
|
carbone_tags_values = [] |
|
if isinstance(obj, dict): |
|
for k, v in obj.items(): |
|
full_key = f"{parent_key}.{k}" if parent_key else k |
|
tags, tags_values = extract_keys_and_values(v, full_key) |
|
carbone_tags.extend(tags) |
|
carbone_tags_values.extend(tags_values) |
|
elif isinstance(obj, list): |
|
for i, item in enumerate(obj): |
|
tags, tags_values = extract_keys_and_values(item, f"{parent_key}[{i}]") |
|
carbone_tags.extend(tags) |
|
carbone_tags_values.extend(tags_values) |
|
else: |
|
carbone_tag = f"{{d.{parent_key}}}" |
|
carbone_tags.append(carbone_tag) |
|
carbone_tags_values.append((carbone_tag, obj)) |
|
return carbone_tags, carbone_tags_values |
|
|
|
|
|
carbone_keys, carbone_keys_values = extract_keys_and_values(data) |
|
|
|
|
|
carbone_keys.sort() |
|
carbone_keys_values.sort(key=lambda x: x[0]) |
|
|
|
|
|
tags_output = "\n".join(carbone_keys) |
|
tags_values_output = "\n".join([f"{tag}: {value}" for tag, value in carbone_keys_values]) |
|
return tags_output, tags_values_output |
|
except json.JSONDecodeError: |
|
return "Invalid JSON. Please check your input.", "" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=generate_carbone_tags_and_values, |
|
inputs=gr.Textbox(lines=20, placeholder="Paste JSON here..."), |
|
outputs=[gr.Textbox(lines=20), gr.Textbox(lines=20)], |
|
title="JSON to Carbone Tags and Values Converter", |
|
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." |
|
) |
|
|
|
|
|
interface.launch() |
|
|