File size: 3,598 Bytes
6abf58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import json
import gradio as gr

from llm_output_parser import parse_json, parse_jsons, parse_xml


def parse_single_json(text):
    """Parse a single JSON object from text."""
    try:
        result = parse_json(text)
        return json.dumps(result, indent=2)
    except (ValueError, TypeError) as e:
        return f"Error parsing JSON: {str(e)}"


def parse_multiple_jsons(text):
    """Parse multiple JSON objects from text."""
    try:
        results = parse_jsons(text)
        formatted_results = []

        for i, result in enumerate(results):
            formatted_results.append(f"JSON {i+1}:\n{json.dumps(result, indent=2)}")

        return "\n\n".join(formatted_results)
    except (ValueError, TypeError) as e:
        return f"Error parsing JSONs: {str(e)}"


def parse_xml_to_json(text):
    """Parse XML from text and convert to JSON format."""
    try:
        result = parse_xml(text)
        return json.dumps(result, indent=2)
    except (ValueError, TypeError) as e:
        return f"Error parsing XML: {str(e)}"


def process_text(text, parser_type):
    """Process text based on selected parser type."""
    if not text.strip():
        return "Please enter some text to parse."

    if parser_type == "Single JSON":
        return parse_single_json(text)
    elif parser_type == "Multiple JSONs":
        return parse_multiple_jsons(text)
    elif parser_type == "XML":
        return parse_xml_to_json(text)
    else:
        return "Invalid parser type selected."


# Example texts for the interface
example_json = """
```json
{
  "name": "John Doe",
  "age": 30,
  "isEmployed": true,
  "address": {
    "street": "123 Main St",
    "city": "Anytown"
  }
}
```
"""

example_multiple_jsons = """
Here are some JSON objects:

```json
{"id": 1, "name": "Product A"}
```

And another one:

```json
{"id": 2, "name": "Product B"}
```
"""

example_xml = """
```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <person id="1">
    <name>John Doe</name>
    <age>30</age>
    <address>
      <street>123 Main St</street>
      <city>Anytown</city>
    </address>
  </person>
</root>
```
"""

# Create Gradio interface
with gr.Blocks(title="LLM Output Parser") as demo:
    gr.Markdown("# LLM Output Parser")
    gr.Markdown("Extract structured data from text containing JSON or XML")

    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Input Text",
                placeholder="Paste text containing JSON or XML here...",
                lines=15,
            )
            parser_type = gr.Radio(
                choices=["Single JSON", "Multiple JSONs", "XML"],
                label="Parser Type",
                value="Single JSON",
            )
            parse_button = gr.Button("Parse", variant="primary")

        with gr.Column():
            output_text = gr.Textbox(label="Parsed Result", lines=15)

    # Examples
    with gr.Accordion("Example Inputs", open=False):
        gr.Examples(
            examples=[
                [example_json, "Single JSON"],
                [example_multiple_jsons, "Multiple JSONs"],
                [example_xml, "XML"],
            ],
            inputs=[input_text, parser_type],
        )

    # Set up event handler
    parse_button.click(
        fn=process_text, inputs=[input_text, parser_type], outputs=output_text
    )

    gr.Markdown(
        "## How to use\n"
        "1. Paste text containing JSON or XML\n"
        "2. Select the parser type\n"
        "3. Click 'Parse' to extract structured data"
    )

if __name__ == "__main__":
    demo.launch()