Spaces:
Sleeping
Sleeping
Initial: JSON extractor for messy LLM output
Browse files- README.md +24 -5
- app.py +75 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,31 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: yellow
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: JSON Extractor
|
| 3 |
+
emoji: 🎯
|
| 4 |
colorFrom: yellow
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "5.49.1"
|
| 8 |
+
python_version: "3.12"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
+
license: mit
|
| 12 |
+
short_description: "Pull clean JSON out of messy LLM text."
|
| 13 |
+
tags:
|
| 14 |
+
- llm
|
| 15 |
+
- structured-output
|
| 16 |
+
- json
|
| 17 |
+
- parsing
|
| 18 |
+
- agentcast
|
| 19 |
---
|
| 20 |
|
| 21 |
+
# JSON Extractor
|
| 22 |
+
|
| 23 |
+
Paste messy LLM output, get clean JSON. Powered by [`agentcast`](https://pypi.org/project/agentcast-py/).
|
| 24 |
+
|
| 25 |
+
Handles fenced ` ```json ``` ` blocks, language-less fences, top-level arrays, inline JSON in prose, multi-line unfenced objects, and refusals (returns `null`).
|
| 26 |
+
|
| 27 |
+
## Related
|
| 28 |
+
|
| 29 |
+
- [`agentcast` on PyPI](https://pypi.org/project/agentcast-py/)
|
| 30 |
+
- [The Agent Reliability Stack](https://mukundakatta.github.io/agent-stack/)
|
| 31 |
+
- Companion dataset: [`llm-output-extraction-cases`](https://huggingface.co/datasets/mukunda1729/llm-output-extraction-cases)
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""JSON extractor — pull JSON out of messy LLM text.
|
| 2 |
+
|
| 3 |
+
Uses agentcast's tolerant extractor. Handles fenced blocks, inline JSON,
|
| 4 |
+
trailing prose, and unfenced multi-line objects.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from agentcast import extract_json
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def extract(messy: str):
|
| 13 |
+
if not messy.strip():
|
| 14 |
+
return "_Paste some text to extract JSON from._", ""
|
| 15 |
+
extracted = extract_json(messy)
|
| 16 |
+
if extracted is None:
|
| 17 |
+
return "❌ **No JSON found.**\n\nTry: fenced ` ```json ... ``` `, inline `{...}`, or top-level array `[...]`.", ""
|
| 18 |
+
pretty = json.dumps(extracted, indent=2, ensure_ascii=False)
|
| 19 |
+
summary = f"✅ **Extracted** ({type(extracted).__name__}, {len(pretty)} chars pretty-printed)"
|
| 20 |
+
return summary, pretty
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
with gr.Blocks(title="JSON Extractor — for messy LLM output", theme=gr.themes.Soft()) as demo:
|
| 24 |
+
gr.Markdown(
|
| 25 |
+
"""
|
| 26 |
+
# JSON Extractor
|
| 27 |
+
|
| 28 |
+
Paste messy LLM output, get clean JSON. Powered by [`agentcast`](https://pypi.org/project/agentcast-py/).
|
| 29 |
+
|
| 30 |
+
Handles:
|
| 31 |
+
- Fenced ` ```json ... ``` ` blocks
|
| 32 |
+
- Fenced blocks with no language tag
|
| 33 |
+
- Top-level arrays `[...]`
|
| 34 |
+
- Inline JSON in prose
|
| 35 |
+
- Multi-line unfenced objects
|
| 36 |
+
- Refusals → returns `null`
|
| 37 |
+
|
| 38 |
+
Test cases drawn from [`llm-output-extraction-cases`](https://huggingface.co/datasets/mukunda1729/llm-output-extraction-cases) (20 real-world patterns).
|
| 39 |
+
"""
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column():
|
| 44 |
+
txt = gr.Textbox(
|
| 45 |
+
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!',
|
| 46 |
+
label="Messy LLM output",
|
| 47 |
+
lines=12,
|
| 48 |
+
)
|
| 49 |
+
btn = gr.Button("Extract", variant="primary")
|
| 50 |
+
with gr.Column():
|
| 51 |
+
summary_out = gr.Markdown()
|
| 52 |
+
json_out = gr.Code(language="json", label="Extracted JSON")
|
| 53 |
+
btn.click(extract, inputs=txt, outputs=[summary_out, json_out])
|
| 54 |
+
|
| 55 |
+
gr.Examples(
|
| 56 |
+
examples=[
|
| 57 |
+
['Sure! Here is the answer:\n\n```json\n{"name": "Widget Pro", "price": 29.99}\n```\n\nLet me know!'],
|
| 58 |
+
['{"answer": 42}'],
|
| 59 |
+
['[{"k": 1}, {"k": 2}, {"k": 3}]'],
|
| 60 |
+
['Final:\n\n{\n "event": "login",\n "ts": "2026-04-26T12:00:00Z"\n}\n\nDone.'],
|
| 61 |
+
['I am sorry, I cannot answer that.'],
|
| 62 |
+
],
|
| 63 |
+
inputs=txt,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
gr.Markdown(
|
| 67 |
+
"""
|
| 68 |
+
---
|
| 69 |
+
Part of [The Agent Reliability Stack](https://mukundakatta.github.io/agent-stack/) · MIT licensed
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.49.1
|
| 2 |
+
huggingface_hub>=0.30,<1.0
|
| 3 |
+
agentcast-py>=0.1.0
|