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6 |
+
|
| 7 |
+
def parse_single_json(text):
|
| 8 |
+
"""Parse a single JSON object from text."""
|
| 9 |
+
try:
|
| 10 |
+
result = parse_json(text)
|
| 11 |
+
return json.dumps(result, indent=2)
|
| 12 |
+
except (ValueError, TypeError) as e:
|
| 13 |
+
return f"Error parsing JSON: {str(e)}"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def parse_multiple_jsons(text):
|
| 17 |
+
"""Parse multiple JSON objects from text."""
|
| 18 |
+
try:
|
| 19 |
+
results = parse_jsons(text)
|
| 20 |
+
formatted_results = []
|
| 21 |
+
|
| 22 |
+
for i, result in enumerate(results):
|
| 23 |
+
formatted_results.append(f"JSON {i+1}:\n{json.dumps(result, indent=2)}")
|
| 24 |
+
|
| 25 |
+
return "\n\n".join(formatted_results)
|
| 26 |
+
except (ValueError, TypeError) as e:
|
| 27 |
+
return f"Error parsing JSONs: {str(e)}"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def parse_xml_to_json(text):
|
| 31 |
+
"""Parse XML from text and convert to JSON format."""
|
| 32 |
+
try:
|
| 33 |
+
result = parse_xml(text)
|
| 34 |
+
return json.dumps(result, indent=2)
|
| 35 |
+
except (ValueError, TypeError) as e:
|
| 36 |
+
return f"Error parsing XML: {str(e)}"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def process_text(text, parser_type):
|
| 40 |
+
"""Process text based on selected parser type."""
|
| 41 |
+
if not text.strip():
|
| 42 |
+
return "Please enter some text to parse."
|
| 43 |
+
|
| 44 |
+
if parser_type == "Single JSON":
|
| 45 |
+
return parse_single_json(text)
|
| 46 |
+
elif parser_type == "Multiple JSONs":
|
| 47 |
+
return parse_multiple_jsons(text)
|
| 48 |
+
elif parser_type == "XML":
|
| 49 |
+
return parse_xml_to_json(text)
|
| 50 |
+
else:
|
| 51 |
+
return "Invalid parser type selected."
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Example texts for the interface
|
| 55 |
+
example_json = """
|
| 56 |
+
```json
|
| 57 |
+
{
|
| 58 |
+
"name": "John Doe",
|
| 59 |
+
"age": 30,
|
| 60 |
+
"isEmployed": true,
|
| 61 |
+
"address": {
|
| 62 |
+
"street": "123 Main St",
|
| 63 |
+
"city": "Anytown"
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
```
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
example_multiple_jsons = """
|
| 70 |
+
Here are some JSON objects:
|
| 71 |
+
|
| 72 |
+
```json
|
| 73 |
+
{"id": 1, "name": "Product A"}
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
And another one:
|
| 77 |
+
|
| 78 |
+
```json
|
| 79 |
+
{"id": 2, "name": "Product B"}
|
| 80 |
+
```
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
example_xml = """
|
| 84 |
+
```xml
|
| 85 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 86 |
+
<root>
|
| 87 |
+
<person id="1">
|
| 88 |
+
<name>John Doe</name>
|
| 89 |
+
<age>30</age>
|
| 90 |
+
<address>
|
| 91 |
+
<street>123 Main St</street>
|
| 92 |
+
<city>Anytown</city>
|
| 93 |
+
</address>
|
| 94 |
+
</person>
|
| 95 |
+
</root>
|
| 96 |
+
```
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
# Create Gradio interface
|
| 100 |
+
with gr.Blocks(title="LLM Output Parser") as demo:
|
| 101 |
+
gr.Markdown("# LLM Output Parser")
|
| 102 |
+
gr.Markdown("Extract structured data from text containing JSON or XML")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column():
|
| 106 |
+
input_text = gr.Textbox(
|
| 107 |
+
label="Input Text",
|
| 108 |
+
placeholder="Paste text containing JSON or XML here...",
|
| 109 |
+
lines=15,
|
| 110 |
+
)
|
| 111 |
+
parser_type = gr.Radio(
|
| 112 |
+
choices=["Single JSON", "Multiple JSONs", "XML"],
|
| 113 |
+
label="Parser Type",
|
| 114 |
+
value="Single JSON",
|
| 115 |
+
)
|
| 116 |
+
parse_button = gr.Button("Parse", variant="primary")
|
| 117 |
+
|
| 118 |
+
with gr.Column():
|
| 119 |
+
output_text = gr.Textbox(label="Parsed Result", lines=15)
|
| 120 |
+
|
| 121 |
+
# Examples
|
| 122 |
+
with gr.Accordion("Example Inputs", open=False):
|
| 123 |
+
gr.Examples(
|
| 124 |
+
examples=[
|
| 125 |
+
[example_json, "Single JSON"],
|
| 126 |
+
[example_multiple_jsons, "Multiple JSONs"],
|
| 127 |
+
[example_xml, "XML"],
|
| 128 |
+
],
|
| 129 |
+
inputs=[input_text, parser_type],
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# Set up event handler
|
| 133 |
+
parse_button.click(
|
| 134 |
+
fn=process_text, inputs=[input_text, parser_type], outputs=output_text
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
gr.Markdown(
|
| 138 |
+
"## How to use\n"
|
| 139 |
+
"1. Paste text containing JSON or XML\n"
|
| 140 |
+
"2. Select the parser type\n"
|
| 141 |
+
"3. Click 'Parse' to extract structured data"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo.launch()
|