File size: 2,677 Bytes
8df8d96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""JSON extractor — pull JSON out of messy LLM text.

Uses agentcast's tolerant extractor. Handles fenced blocks, inline JSON,
trailing prose, and unfenced multi-line objects.
"""

import json
import gradio as gr
from agentcast import extract_json


def extract(messy: str):
    if not messy.strip():
        return "_Paste some text to extract JSON from._", ""
    extracted = extract_json(messy)
    if extracted is None:
        return "❌ **No JSON found.**\n\nTry: fenced ` ```json ... ``` `, inline `{...}`, or top-level array `[...]`.", ""
    pretty = json.dumps(extracted, indent=2, ensure_ascii=False)
    summary = f"✅ **Extracted** ({type(extracted).__name__}, {len(pretty)} chars pretty-printed)"
    return summary, pretty


with gr.Blocks(title="JSON Extractor — for messy LLM output", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # JSON Extractor

        Paste messy LLM output, get clean JSON. Powered by [`agentcast`](https://pypi.org/project/agentcast-py/).

        Handles:
        - Fenced ` ```json ... ``` ` blocks
        - Fenced blocks with no language tag
        - Top-level arrays `[...]`
        - Inline JSON in prose
        - Multi-line unfenced objects
        - Refusals → returns `null`

        Test cases drawn from [`llm-output-extraction-cases`](https://huggingface.co/datasets/mukunda1729/llm-output-extraction-cases) (20 real-world patterns).
        """
    )

    with gr.Row():
        with gr.Column():
            txt = gr.Textbox(
                value='Sure! Here is the answer:\n\n```json\n{"name": "Widget Pro", "price": 29.99}\n```\n\nLet me know if you need anything else!',
                label="Messy LLM output",
                lines=12,
            )
            btn = gr.Button("Extract", variant="primary")
        with gr.Column():
            summary_out = gr.Markdown()
            json_out = gr.Code(language="json", label="Extracted JSON")
    btn.click(extract, inputs=txt, outputs=[summary_out, json_out])

    gr.Examples(
        examples=[
            ['Sure! Here is the answer:\n\n```json\n{"name": "Widget Pro", "price": 29.99}\n```\n\nLet me know!'],
            ['{"answer": 42}'],
            ['[{"k": 1}, {"k": 2}, {"k": 3}]'],
            ['Final:\n\n{\n  "event": "login",\n  "ts": "2026-04-26T12:00:00Z"\n}\n\nDone.'],
            ['I am sorry, I cannot answer that.'],
        ],
        inputs=txt,
    )

    gr.Markdown(
        """
        ---
        Part of [The Agent Reliability Stack](https://mukundakatta.github.io/agent-stack/) · MIT licensed
        """
    )


if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)