BonusLockSMith commited on
Commit
9d84da3
·
verified ·
1 Parent(s): 4b3c0b4

Deploy structured-extractor (30-in-15 #4)

Browse files
Files changed (6) hide show
  1. .env.example +7 -0
  2. Dockerfile +9 -0
  3. README.md +44 -4
  4. app.py +217 -0
  5. demo.png +0 -0
  6. requirements.txt +3 -0
.env.example ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # Pick ONE backend. For a public deploy, use a cloud API (not local Ollama).
2
+ # Option A — Claude API (recommended; uses forced tool-use for schema-valid JSON):
3
+ ANTHROPIC_API_KEY=sk-ant-...
4
+ ANTHROPIC_MODEL=claude-haiku-4-5
5
+ # Option B — local Ollama (dev only; uses constrained JSON decoding via format=schema):
6
+ # OLLAMA_URL=http://192.168.12.223:11434
7
+ # OLLAMA_MODEL=qwen2.5:7b
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Structured Data Extractor — runs on Hugging Face Spaces, Render, Railway, Fly.io, etc.
2
+ FROM python:3.11-slim
3
+ WORKDIR /app
4
+ COPY requirements.txt .
5
+ RUN pip install --no-cache-dir -r requirements.txt
6
+ COPY . .
7
+ ENV PORT=7860
8
+ EXPOSE 7860
9
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,10 +1,50 @@
1
  ---
2
- title: Structured Extractor
3
- emoji: 👀
4
  colorFrom: purple
5
- colorTo: blue
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Structured Data Extractor
3
+ emoji: 🧬
4
  colorFrom: purple
5
+ colorTo: indigo
6
  sdk: docker
7
+ app_port: 7860
8
  pinned: false
9
  ---
10
 
11
+ # Structured Data Extractor 🧬
12
+
13
+ Paste messy, unstructured text — an email signature, a receipt, a job posting, an event
14
+ invite — and get back **clean, schema-valid JSON**. Pick a preset (Contact, Invoice,
15
+ Event, Job) or define your own fields.
16
+
17
+ > Project **#4** of my *"30 AI Projects in 15 Days"* build-in-public challenge.
18
+
19
+ **▶ Live demo: https://extract.gritai.solutions**
20
+
21
+ ![demo](demo.png)
22
+
23
+ ## What makes it reliable
24
+ The output is **always valid JSON matching the schema** — because it uses Claude's
25
+ **forced tool-use**: the model is required to call a tool whose `input_schema` *is* the
26
+ target schema, so the response is structured and validated at the API layer (no brittle
27
+ "parse JSON out of prose"). Missing fields come back as `null` — it never invents values.
28
+ (The local Ollama fallback uses constrained JSON decoding via `format=<schema>`.)
29
+
30
+ ## What it demonstrates
31
+ - **Structured outputs / function-calling** — the core skill behind reliable AI pipelines.
32
+ - **Schema design** — presets plus on-the-fly custom field extraction.
33
+ - **Clean product UX** — one-click samples, syntax-highlighted JSON, copy to clipboard.
34
+
35
+ ## Presets
36
+ 👤 Contact · 🧾 Invoice/Receipt · 📅 Event · 💼 Job posting · ✨ Custom (your own fields)
37
+
38
+ ## Run locally
39
+ ```bash
40
+ pip install -r requirements.txt
41
+ export ANTHROPIC_API_KEY=sk-ant-... # or set OLLAMA_URL / OLLAMA_MODEL
42
+ python app.py # http://127.0.0.1:8500
43
+ ```
44
+
45
+ ## Deploy (always-on)
46
+ Ships with a `Dockerfile` — works on **Hugging Face Spaces**, Render, Railway, or Fly.io.
47
+ Set `ANTHROPIC_API_KEY` (+ optional `ANTHROPIC_MODEL=claude-haiku-4-5`) as a secret.
48
+
49
+ ---
50
+ Built by **Robert Lucyk** · [GritAI Solutions](https://gritai.solutions) · part of the 30-in-15 challenge.
app.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """Structured Data Extractor — paste messy text (an email, receipt, job post…) and get
3
+ clean, schema-valid JSON. Uses Claude's forced tool-use so the output always matches the
4
+ schema (Ollama fallback uses constrained JSON decoding).
5
+
6
+ pip install flask requests anthropic
7
+ python app.py # http://127.0.0.1:8500
8
+
9
+ Project #4 of the "30 Projects in 15 Days" challenge — GritAI.
10
+ """
11
+ import os, json
12
+ from flask import Flask, request, Response, jsonify
13
+
14
+ PORT = int(os.environ.get("PORT", "8500"))
15
+ ANTHROPIC_KEY = os.environ.get("ANTHROPIC_API_KEY")
16
+ ANTHROPIC_MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-sonnet-5")
17
+ OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://127.0.0.1:11434")
18
+ OLLAMA_MODEL = os.environ.get("OLLAMA_MODEL", "qwen2.5:7b")
19
+
20
+ S = lambda: {"type": ["string", "null"]}
21
+ N = lambda: {"type": ["number", "null"]}
22
+
23
+ SCHEMAS = {
24
+ "contact": {"label": "Contact", "emoji": "👤", "schema": {"type": "object", "properties": {
25
+ "name": S(), "title": S(), "company": S(), "email": S(), "phone": S(),
26
+ "website": S(), "address": S()}, "required": ["name"]}},
27
+ "invoice": {"label": "Invoice / Receipt", "emoji": "🧾", "schema": {"type": "object", "properties": {
28
+ "vendor": S(), "date": S(), "currency": S(), "subtotal": N(), "tax": N(), "total": N(),
29
+ "line_items": {"type": "array", "items": {"type": "object", "properties": {
30
+ "description": {"type": "string"}, "qty": N(), "unit_price": N(), "amount": N()}}}},
31
+ "required": ["vendor", "total"]}},
32
+ "event": {"label": "Event", "emoji": "📅", "schema": {"type": "object", "properties": {
33
+ "title": S(), "date": S(), "start_time": S(), "end_time": S(), "location": S(),
34
+ "organizer": S(), "description": S()}, "required": ["title"]}},
35
+ "job": {"label": "Job posting", "emoji": "💼", "schema": {"type": "object", "properties": {
36
+ "title": S(), "company": S(), "location": S(), "employment_type": S(), "salary_range": S(),
37
+ "responsibilities": {"type": "array", "items": {"type": "string"}},
38
+ "requirements": {"type": "array", "items": {"type": "string"}},
39
+ "apply_url": S()}, "required": ["title"]}},
40
+ }
41
+
42
+ SAMPLES = {
43
+ "contact": ("👤", "Email signature",
44
+ "thanks again! — Maya\n\nMaya R. Fitzgerald\nSr. Solutions Architect, Northwind Cloud\nmaya.fitzgerald@northwind.example | cell (415) 555-0182\n1200 Harbor Blvd, Suite 400, San Diego CA 92101\nnorthwindcloud.example"),
45
+ "invoice": ("🧾", "Receipt",
46
+ "RIVERSIDE HARDWARE\nInvoice #4821 — Mar 14, 2026\n\n2x Cordless drill @ $89.99 = $179.98\n1x Drill bit set @ $24.50 = $24.50\n3x Work gloves @ $12.00 = $36.00\n\nSubtotal: $240.48\nTax (8.25%): $19.84\nTOTAL: $260.32\nPaid: Visa ****4417"),
47
+ "job": ("💼", "Job posting",
48
+ "We're hiring a Senior Backend Engineer (Remote, US) at Lumina Labs. Comp: $150k-$185k + equity. You'll design and scale our API platform, own service reliability, and mentor two junior engineers. Must have: 5+ yrs backend, strong Python or Go, experience with distributed systems and Postgres. Nice to have: Kafka, k8s. Apply at lumina.example/careers/be-senior"),
49
+ }
50
+
51
+ def build_schema(key, custom_fields):
52
+ if key in SCHEMAS:
53
+ return SCHEMAS[key]["schema"]
54
+ if key == "custom":
55
+ props = {f.strip(): S() for f in (custom_fields or "").split(",") if f.strip()}
56
+ return {"type": "object", "properties": props}
57
+ return SCHEMAS["contact"]["schema"]
58
+
59
+ def extract(text, schema):
60
+ prompt = ("Extract structured data from the text below into the required schema. "
61
+ "Use null for anything not present — never invent values.\n\nTEXT:\n" + text[:12000])
62
+ if ANTHROPIC_KEY:
63
+ import anthropic
64
+ client = anthropic.Anthropic(api_key=ANTHROPIC_KEY)
65
+ r = client.messages.create(model=ANTHROPIC_MODEL, max_tokens=1500,
66
+ tools=[{"name": "extract", "description": "Return the structured data from the text.",
67
+ "input_schema": schema}],
68
+ tool_choice={"type": "tool", "name": "extract"},
69
+ messages=[{"role": "user", "content": prompt}])
70
+ for b in r.content:
71
+ if b.type == "tool_use":
72
+ return b.input
73
+ return {}
74
+ import requests
75
+ r = requests.post(f"{OLLAMA_URL}/api/chat", timeout=90, json={
76
+ "model": OLLAMA_MODEL, "stream": False, "format": schema,
77
+ "messages": [{"role": "user", "content": prompt}], "options": {"temperature": 0}})
78
+ r.raise_for_status()
79
+ return json.loads(r.json()["message"]["content"])
80
+
81
+ app = Flask(__name__)
82
+
83
+ @app.route("/")
84
+ def home():
85
+ return Response(PAGE, mimetype="text/html")
86
+
87
+ @app.route("/api/extract", methods=["POST"])
88
+ def api_extract():
89
+ b = request.get_json(force=True)
90
+ text = (b.get("text") or "").strip()
91
+ key = b.get("schema", "contact")
92
+ fields = b.get("fields", "")
93
+ if not text:
94
+ return jsonify(error="Paste some text to extract from."), 400
95
+ schema = build_schema(key, fields)
96
+ if not schema.get("properties"):
97
+ return jsonify(error="No fields to extract — add some custom fields (comma-separated)."), 400
98
+ try:
99
+ return jsonify(data=extract(text, schema))
100
+ except Exception as e:
101
+ import sys; print("extract error:", repr(e), file=sys.stderr, flush=True)
102
+ return jsonify(error="Extraction failed — please try again."), 200
103
+
104
+ PAGE = """<!doctype html><html lang="en"><head><meta charset="utf-8">
105
+ <title>Structured Data Extractor</title>
106
+ <meta name="viewport" content="width=device-width,initial-scale=1">
107
+ <style>
108
+ :root{--primary:#6d28d9;--accent:#7c3aed;--ink:#1c1830;--bg:#f4f2fb;--card:#fff;--muted:#7d7795;--line:#e7e3f4;
109
+ --code:#1a1626;--key:#c4b5fd;--str:#86efac;--num:#fca5a5;--bool:#7dd3fc;--nul:#94a3b8}
110
+ *{box-sizing:border-box}html,body{height:100%}
111
+ body{margin:0;font-family:'Segoe UI',system-ui,-apple-system,sans-serif;color:var(--ink);
112
+ background:radial-gradient(140% 90% at 50% 0%,#fff,var(--bg) 60%);display:flex;justify-content:center}
113
+ .app{width:100%;max-width:760px;min-height:100dvh;display:flex;flex-direction:column;padding:0 16px}
114
+ header{padding:26px 4px 12px;display:flex;align-items:center;gap:13px}
115
+ .hic{width:46px;height:46px;border-radius:13px;display:grid;place-items:center;font-size:23px;flex:none;color:#fff;
116
+ background:linear-gradient(135deg,var(--primary),var(--accent));box-shadow:0 10px 26px -10px rgba(109,40,217,.6)}
117
+ header h1{margin:0;font-size:23px;font-weight:800;letter-spacing:-.4px}
118
+ header p{margin:2px 0 0;color:var(--muted);font-size:13.5px}
119
+ .card{background:var(--card);border:1px solid var(--line);border-radius:16px;padding:16px;box-shadow:0 6px 22px rgba(28,24,48,.05)}
120
+ textarea{width:100%;min-height:150px;border:0;outline:none;resize:vertical;font-size:14.5px;line-height:1.5;color:var(--ink);font-family:inherit;background:transparent}
121
+ .samples{display:flex;gap:8px;flex-wrap:wrap;margin:12px 2px 4px}
122
+ .samp{display:inline-flex;align-items:center;gap:7px;background:var(--card);border:1px solid var(--line);border-radius:20px;
123
+ padding:7px 13px;font-size:12.5px;cursor:pointer;transition:.15s}
124
+ .samp:hover{border-color:var(--accent);color:var(--accent);transform:translateY(-1px)}
125
+ .row{display:flex;flex-wrap:wrap;gap:12px;align-items:center;justify-content:space-between;margin-top:14px}
126
+ .seg{display:inline-flex;flex-wrap:wrap;background:#efeafb;border-radius:12px;padding:3px;gap:2px}
127
+ .seg button{border:0;background:transparent;padding:8px 13px;border-radius:9px;font-size:13px;cursor:pointer;color:var(--muted);transition:.15s}
128
+ .seg button.on{background:#fff;color:var(--primary);box-shadow:0 1px 3px rgba(0,0,0,.1);font-weight:600}
129
+ .go{border:0;border-radius:12px;padding:12px 22px;font-size:15px;font-weight:600;cursor:pointer;color:#fff;
130
+ background:linear-gradient(135deg,var(--primary),var(--accent));transition:.15s}
131
+ .go:hover{filter:brightness(1.08)}.go:disabled{opacity:.5;cursor:default}
132
+ #customwrap{display:none;margin-top:12px}
133
+ #custom{width:100%;padding:11px 14px;border:1px solid var(--line);border-radius:11px;font-size:14px;outline:none;background:#faf9fe}
134
+ #custom:focus{border-color:var(--accent)}
135
+ .out{margin:18px 0 8px;display:none}.out.show{display:block}
136
+ .out .bar{display:flex;align-items:center;justify-content:space-between;margin-bottom:9px}
137
+ .out .bar h2{margin:0;font-size:13px;color:var(--muted);font-weight:600;letter-spacing:.5px;text-transform:uppercase}
138
+ .copy{border:1px solid var(--line);background:#fff;border-radius:9px;padding:6px 12px;font-size:12.5px;cursor:pointer;color:var(--primary)}
139
+ .copy:hover{border-color:var(--accent)}
140
+ pre#json{margin:0;background:var(--code);color:#e6e2f5;border-radius:14px;padding:16px 18px;overflow:auto;
141
+ font-family:'JetBrains Mono',Consolas,monospace;font-size:13px;line-height:1.6;white-space:pre;min-height:40px}
142
+ #json .key{color:var(--key)}#json .str{color:var(--str)}#json .num{color:var(--num)}#json .bool{color:var(--bool)}#json .null{color:var(--nul)}
143
+ .spin{display:inline-flex;gap:5px}.spin span{width:8px;height:8px;border-radius:50%;background:var(--accent);opacity:.5;animation:bd 1.2s infinite}
144
+ .spin span:nth-child(2){animation-delay:.2s}.spin span:nth-child(3){animation-delay:.4s}
145
+ @keyframes bd{0%,60%,100%{transform:translateY(0);opacity:.35}30%{transform:translateY(-5px);opacity:1}}
146
+ .foot{text-align:center;font-size:10.5px;color:var(--muted);padding:16px 0 22px}.foot b{color:#5b5478}
147
+ </style></head>
148
+ <body><div class="app">
149
+ <header><div class="hic">{ }</div><div><h1>Structured Data Extractor</h1>
150
+ <p>Paste messy text — get clean, schema-valid JSON back</p></div></header>
151
+ <div class="card">
152
+ <textarea id="src" placeholder="Paste an email signature, a receipt, a job posting, an event invite…"></textarea>
153
+ <div class="samples" id="samples"></div>
154
+ <div id="customwrap"><input id="custom" placeholder="Custom fields, comma-separated (e.g. name, budget, deadline, owner)"></div>
155
+ <div class="row">
156
+ <div class="seg" id="seg">
157
+ <button data-v="contact" class="on">👤 Contact</button>
158
+ <button data-v="invoice">🧾 Invoice</button>
159
+ <button data-v="event">📅 Event</button>
160
+ <button data-v="job">💼 Job</button>
161
+ <button data-v="custom">✨ Custom</button>
162
+ </div>
163
+ <button class="go" id="go" onclick="run()">Extract →</button>
164
+ </div>
165
+ </div>
166
+ <div class="out" id="out">
167
+ <div class="bar"><h2>Extracted JSON</h2><button class="copy" id="copy" onclick="copyJson()" style="display:none">Copy</button></div>
168
+ <pre id="json"></pre>
169
+ </div>
170
+ <div class="foot">Schema-valid JSON via forced tool-use · never invents values · <b>GritAI</b></div>
171
+ </div><script>
172
+ const SAMPLES=[["contact","👤","Email signature"],["invoice","🧾","Receipt"],["job","💼","Job posting"]];
173
+ const $=id=>document.getElementById(id);
174
+ const src=$('src'),go=$('go'),out=$('out'),jsonEl=$('json'),copy=$('copy'),customwrap=$('customwrap'),custom=$('custom');
175
+ let schema="contact",busy=false,lastJson="";
176
+ $('samples').innerHTML='<span style="color:var(--muted);font-size:12px;align-self:center">Try:</span>'+
177
+ SAMPLES.map(s=>`<span class="samp" onclick="loadSample('${s[0]}')">${s[1]} ${s[2]}</span>`).join('');
178
+ $('seg').addEventListener('click',e=>{const b=e.target.closest('button');if(!b)return;
179
+ schema=b.dataset.v;[...seg.children].forEach(x=>x.classList.toggle('on',x===b));
180
+ customwrap.style.display=schema==='custom'?'block':'none';});
181
+ function esc(s){return String(s).replace(/[&<>]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;'}[c]));}
182
+ function jhtml(v,ind){ind=ind||0;const pad=' '.repeat(ind),p2=' '.repeat(ind+1);
183
+ if(v===null)return '<span class="null">null</span>';
184
+ if(typeof v==='number')return '<span class="num">'+v+'</span>';
185
+ if(typeof v==='boolean')return '<span class="bool">'+v+'</span>';
186
+ if(typeof v==='string')return '<span class="str">"'+esc(v)+'"</span>';
187
+ if(Array.isArray(v)){if(!v.length)return '[]';return '[\\n'+v.map(x=>p2+jhtml(x,ind+1)).join(',\\n')+'\\n'+pad+']';}
188
+ const ks=Object.keys(v);if(!ks.length)return '{}';
189
+ return '{\\n'+ks.map(k=>p2+'<span class="key">"'+esc(k)+'"</span>: '+jhtml(v[k],ind+1)).join(',\\n')+'\\n'+pad+'}';}
190
+ function copyJson(){navigator.clipboard.writeText(lastJson);copy.textContent='Copied!';setTimeout(()=>copy.textContent='Copy',1400);}
191
+ async function loadSample(name){
192
+ const r=await fetch('/api/sample_text?name='+name);const d=await r.json();
193
+ if(d.text){src.value=d.text;schema=name;[...seg.children].forEach(x=>x.classList.toggle('on',x.dataset.v===name));
194
+ customwrap.style.display='none';run();}
195
+ }
196
+ async function run(){if(busy)return;const text=src.value.trim();if(!text){src.focus();return;}
197
+ busy=true;go.disabled=true;go.textContent='Extracting…';out.classList.add('show');copy.style.display='none';
198
+ jsonEl.innerHTML='<span class="spin"><span></span><span></span><span></span></span>';
199
+ try{const r=await fetch('/api/extract',{method:'POST',headers:{'Content-Type':'application/json'},
200
+ body:JSON.stringify({text,schema,fields:custom.value})});const d=await r.json();
201
+ if(d.error){jsonEl.textContent=d.error;}
202
+ else{lastJson=JSON.stringify(d.data,null,2);jsonEl.innerHTML=jhtml(d.data,0);copy.style.display='inline-block';}
203
+ }catch(e){jsonEl.textContent='Something went wrong — please try again.';}
204
+ busy=false;go.disabled=false;go.textContent='Extract →';}
205
+ </script></body></html>"""
206
+
207
+ @app.route("/api/sample_text")
208
+ def sample_text():
209
+ name = request.args.get("name", "")
210
+ if name in SAMPLES:
211
+ return jsonify(text=SAMPLES[name][2])
212
+ return jsonify(error="unknown"), 404
213
+
214
+ if __name__ == "__main__":
215
+ backend = "Claude API" if ANTHROPIC_KEY else f"Ollama ({OLLAMA_MODEL} @ {OLLAMA_URL})"
216
+ print(f"Structured Data Extractor on http://127.0.0.1:{PORT} [backend: {backend}]", flush=True)
217
+ app.run(host="0.0.0.0", port=PORT, threaded=True)
demo.png ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask>=3.0
2
+ requests>=2.31
3
+ anthropic>=0.40