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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
name: extract-image-data
|
| 3 |
+
description: Extract structured data from document images through the electblake/image-data-extractor Gradio Space API. Use when calling its template selection, template generation, or image extraction endpoints from Python, JavaScript, or cURL.
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Extract Image Data (API)
|
| 7 |
+
|
| 8 |
+
This skill describes how to use the electblake/image-data-extractor Gradio Space programmatically.
|
| 9 |
+
|
| 10 |
+
## API Endpoints
|
| 11 |
+
|
| 12 |
+
### `/select_structured_json_template`
|
| 13 |
+
|
| 14 |
+
**Parameters:**
|
| 15 |
+
|
| 16 |
+
- `key` [Dropdown]: `Literal['Basic receipt', 'Advanced receipt', 'Basic bank statement', 'Advanced bank statement', 'Invoice with line items', 'Advanced global record']`, default: `Invoice with line items`
|
| 17 |
+
|
| 18 |
+
**Returns:**
|
| 19 |
+
|
| 20 |
+
- `Structured JSON template` [Textbox]: `str`
|
| 21 |
+
|
| 22 |
+
**Python:**
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
from gradio_client import Client
|
| 26 |
+
|
| 27 |
+
client = Client("electblake/image-data-extractor")
|
| 28 |
+
result = client.predict(
|
| 29 |
+
key="Invoice with line items",
|
| 30 |
+
api_name="/select_structured_json_template",
|
| 31 |
+
)
|
| 32 |
+
print(result)
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
**JavaScript:**
|
| 36 |
+
|
| 37 |
+
```javascript
|
| 38 |
+
import { Client } from "@gradio/client";
|
| 39 |
+
|
| 40 |
+
const client = await Client.connect("electblake/image-data-extractor");
|
| 41 |
+
const result = await client.predict("/select_structured_json_template", {
|
| 42 |
+
key: "Invoice with line items",
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
console.log(result.data);
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
**cURL:**
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
curl -X POST https://electblake-image-data-extractor.hf.space/call/v2/select_structured_json_template -s -H "Content-Type: application/json" \
|
| 52 |
+
-d '{"key": "Invoice with line items"}' \
|
| 53 |
+
| awk -F'"' '{ print $4}' \
|
| 54 |
+
| read EVENT_ID; curl -N https://electblake-image-data-extractor.hf.space/call/select_structured_json_template/$EVENT_ID
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### `/generate_template`
|
| 58 |
+
|
| 59 |
+
**Parameters:**
|
| 60 |
+
|
| 61 |
+
- `image` [Image]: `filepath` (required)
|
| 62 |
+
|
| 63 |
+
**Returns:**
|
| 64 |
+
|
| 65 |
+
- `Structured JSON template` [Textbox]: `str`
|
| 66 |
+
|
| 67 |
+
**Python:**
|
| 68 |
+
|
| 69 |
+
```python
|
| 70 |
+
from gradio_client import Client, handle_file
|
| 71 |
+
|
| 72 |
+
client = Client("electblake/image-data-extractor")
|
| 73 |
+
result = client.predict(
|
| 74 |
+
image=handle_file('https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'),
|
| 75 |
+
api_name="/generate_template",
|
| 76 |
+
)
|
| 77 |
+
print(result)
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
**JavaScript:**
|
| 81 |
+
|
| 82 |
+
```javascript
|
| 83 |
+
import { Client } from "@gradio/client";
|
| 84 |
+
|
| 85 |
+
const response_0 = await fetch("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png");
|
| 86 |
+
const exampleImage = await response_0.blob();
|
| 87 |
+
|
| 88 |
+
const client = await Client.connect("electblake/image-data-extractor");
|
| 89 |
+
const result = await client.predict("/generate_template", {
|
| 90 |
+
image: exampleImage,
|
| 91 |
+
});
|
| 92 |
+
|
| 93 |
+
console.log(result.data);
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
**cURL:**
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
FILE_PATH=$(curl -s -X POST https://electblake-image-data-extractor.hf.space/upload -F 'files=@/path/to/your/file' | tr -d '[]" ')
|
| 100 |
+
|
| 101 |
+
curl -X POST https://electblake-image-data-extractor.hf.space/call/v2/generate_template -s -H "Content-Type: application/json" \
|
| 102 |
+
-d '{"image": {"path": "'$FILE_PATH'", "meta": {"_type": "gradio.FileData"}}}' \
|
| 103 |
+
| awk -F'"' '{ print $4}' \
|
| 104 |
+
| read EVENT_ID; curl -N https://electblake-image-data-extractor.hf.space/call/generate_template/$EVENT_ID
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### `/extract`
|
| 108 |
+
|
| 109 |
+
**Parameters:**
|
| 110 |
+
|
| 111 |
+
- `image` [Image]: `filepath` (required)
|
| 112 |
+
- `text` [Textbox]: `str` (required)
|
| 113 |
+
- `template` [Textbox]: `str`, default: `{
|
| 114 |
+
"document_type": "verbatim-string",
|
| 115 |
+
"vendor": {
|
| 116 |
+
"name": "verbatim-string",
|
| 117 |
+
"address": "verbatim-string",
|
| 118 |
+
"email": "email-address",
|
| 119 |
+
"phone": "phone-number"
|
| 120 |
+
},
|
| 121 |
+
"invoice_number": "verbatim-string",
|
| 122 |
+
"invoice_date": "verbatim-string",
|
| 123 |
+
"line_items": [
|
| 124 |
+
{
|
| 125 |
+
"description": "verbatim-string",
|
| 126 |
+
"category": ["product", "service", "shipping", "tax", "other"],
|
| 127 |
+
"quantity": "integer",
|
| 128 |
+
"unit_price": "number",
|
| 129 |
+
"unit": "unit-code",
|
| 130 |
+
"amount": "number"
|
| 131 |
+
}
|
| 132 |
+
],
|
| 133 |
+
"subtotal": "number",
|
| 134 |
+
"tax": "number",
|
| 135 |
+
"total": "number",
|
| 136 |
+
"currency": "currency"
|
| 137 |
+
}`
|
| 138 |
+
- `enable_thinking` [Checkbox]: `bool`, default: `False`
|
| 139 |
+
|
| 140 |
+
**Returns:**
|
| 141 |
+
|
| 142 |
+
- `Structured JSON` [Json]: `str | float | bool | list | dict`
|
| 143 |
+
|
| 144 |
+
**Python:**
|
| 145 |
+
|
| 146 |
+
```python
|
| 147 |
+
from gradio_client import Client, handle_file
|
| 148 |
+
|
| 149 |
+
client = Client("electblake/image-data-extractor")
|
| 150 |
+
result = client.predict(
|
| 151 |
+
image=handle_file('https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png'),
|
| 152 |
+
text="Hello!!",
|
| 153 |
+
template="{
|
| 154 |
+
"document_type": "verbatim-string",
|
| 155 |
+
"vendor": {
|
| 156 |
+
"name": "verbatim-string",
|
| 157 |
+
"address": "verbatim-string",
|
| 158 |
+
"email": "email-address",
|
| 159 |
+
"phone": "phone-number"
|
| 160 |
+
},
|
| 161 |
+
"invoice_number": "verbatim-string",
|
| 162 |
+
"invoice_date": "verbatim-string",
|
| 163 |
+
"line_items": [
|
| 164 |
+
{
|
| 165 |
+
"description": "verbatim-string",
|
| 166 |
+
"category": ["product", "service", "shipping", "tax", "other"],
|
| 167 |
+
"quantity": "integer",
|
| 168 |
+
"unit_price": "number",
|
| 169 |
+
"unit": "unit-code",
|
| 170 |
+
"amount": "number"
|
| 171 |
+
}
|
| 172 |
+
],
|
| 173 |
+
"subtotal": "number",
|
| 174 |
+
"tax": "number",
|
| 175 |
+
"total": "number",
|
| 176 |
+
"currency": "currency"
|
| 177 |
+
}",
|
| 178 |
+
enable_thinking=False,
|
| 179 |
+
api_name="/extract",
|
| 180 |
+
)
|
| 181 |
+
print(result)
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
**JavaScript:**
|
| 185 |
+
|
| 186 |
+
```javascript
|
| 187 |
+
import { Client } from "@gradio/client";
|
| 188 |
+
|
| 189 |
+
const response_0 = await fetch("https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png");
|
| 190 |
+
const exampleImage = await response_0.blob();
|
| 191 |
+
|
| 192 |
+
const client = await Client.connect("electblake/image-data-extractor");
|
| 193 |
+
const result = await client.predict("/extract", {
|
| 194 |
+
image: exampleImage,
|
| 195 |
+
text: "Hello!!",
|
| 196 |
+
template: "{
|
| 197 |
+
"document_type": "verbatim-string",
|
| 198 |
+
"vendor": {
|
| 199 |
+
"name": "verbatim-string",
|
| 200 |
+
"address": "verbatim-string",
|
| 201 |
+
"email": "email-address",
|
| 202 |
+
"phone": "phone-number"
|
| 203 |
+
},
|
| 204 |
+
"invoice_number": "verbatim-string",
|
| 205 |
+
"invoice_date": "verbatim-string",
|
| 206 |
+
"line_items": [
|
| 207 |
+
{
|
| 208 |
+
"description": "verbatim-string",
|
| 209 |
+
"category": ["product", "service", "shipping", "tax", "other"],
|
| 210 |
+
"quantity": "integer",
|
| 211 |
+
"unit_price": "number",
|
| 212 |
+
"unit": "unit-code",
|
| 213 |
+
"amount": "number"
|
| 214 |
+
}
|
| 215 |
+
],
|
| 216 |
+
"subtotal": "number",
|
| 217 |
+
"tax": "number",
|
| 218 |
+
"total": "number",
|
| 219 |
+
"currency": "currency"
|
| 220 |
+
}",
|
| 221 |
+
enable_thinking: false,
|
| 222 |
+
});
|
| 223 |
+
|
| 224 |
+
console.log(result.data);
|
| 225 |
+
```
|
| 226 |
+
|
| 227 |
+
**cURL:**
|
| 228 |
+
|
| 229 |
+
```bash
|
| 230 |
+
FILE_PATH=$(curl -s -X POST https://electblake-image-data-extractor.hf.space/upload -F 'files=@/path/to/your/file' | tr -d '[]" ')
|
| 231 |
+
|
| 232 |
+
curl -X POST https://electblake-image-data-extractor.hf.space/call/v2/extract -s -H "Content-Type: application/json" \
|
| 233 |
+
-d '{"image": {"path": "'$FILE_PATH'", "meta": {"_type": "gradio.FileData"}}, "text": "Hello!!", "template": "{
|
| 234 |
+
"document_type": "verbatim-string",
|
| 235 |
+
"vendor": {
|
| 236 |
+
"name": "verbatim-string",
|
| 237 |
+
"address": "verbatim-string",
|
| 238 |
+
"email": "email-address",
|
| 239 |
+
"phone": "phone-number"
|
| 240 |
+
},
|
| 241 |
+
"invoice_number": "verbatim-string",
|
| 242 |
+
"invoice_date": "verbatim-string",
|
| 243 |
+
"line_items": [
|
| 244 |
+
{
|
| 245 |
+
"description": "verbatim-string",
|
| 246 |
+
"category": ["product", "service", "shipping", "tax", "other"],
|
| 247 |
+
"quantity": "integer",
|
| 248 |
+
"unit_price": "number",
|
| 249 |
+
"unit": "unit-code",
|
| 250 |
+
"amount": "number"
|
| 251 |
+
}
|
| 252 |
+
],
|
| 253 |
+
"subtotal": "number",
|
| 254 |
+
"tax": "number",
|
| 255 |
+
"total": "number",
|
| 256 |
+
"currency": "currency"
|
| 257 |
+
}", "enable_thinking": false}' \
|
| 258 |
+
| awk -F'"' '{ print $4}' \
|
| 259 |
+
| read EVENT_ID; curl -N https://electblake-image-data-extractor.hf.space/call/extract/$EVENT_ID
|
| 260 |
+
```
|
| 261 |
+
|
.agents/skills/extract-image-data/agents/openai.yaml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
interface:
|
| 2 |
+
display_name: "Extract Image Data (API)"
|
| 3 |
+
short_description: "Extract structured data from document images"
|
| 4 |
+
default_prompt: "Use $extract-image-data to extract structured data from a document image through the API."
|
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
|
| 3 |
import gradio as gr
|
|
@@ -29,6 +30,21 @@ structured_json_templates = {
|
|
| 29 |
default_structured_json_template = "Invoice with line items"
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
with gr.Blocks(title="Image Data Extractor") as demo:
|
| 33 |
gr.Markdown(
|
| 34 |
"# Image Data Extractor\n"
|
|
@@ -51,7 +67,9 @@ with gr.Blocks(title="Image Data Extractor") as demo:
|
|
| 51 |
info="Choose a starting structure, then edit it below.",
|
| 52 |
)
|
| 53 |
generate_template_button = gr.Button("Generate template from Image")
|
| 54 |
-
with gr.Accordion(
|
|
|
|
|
|
|
| 55 |
template = gr.Textbox(
|
| 56 |
label="Structured JSON template",
|
| 57 |
value=structured_json_templates[default_structured_json_template],
|
|
@@ -65,6 +83,20 @@ with gr.Blocks(title="Image Data Extractor") as demo:
|
|
| 65 |
open=True,
|
| 66 |
show_indices=True,
|
| 67 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
gr.Examples(
|
| 70 |
examples=[
|
|
@@ -93,7 +125,19 @@ with gr.Blocks(title="Image Data Extractor") as demo:
|
|
| 93 |
api_name="select_structured_json_template",
|
| 94 |
)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
generate_template_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
generate_template,
|
| 98 |
inputs=image,
|
| 99 |
outputs=template,
|
|
|
|
| 1 |
+
import zipfile
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
import gradio as gr
|
|
|
|
| 30 |
default_structured_json_template = "Invoice with line items"
|
| 31 |
|
| 32 |
|
| 33 |
+
def download_agentskill(include_gradio_skills):
|
| 34 |
+
with zipfile.ZipFile(
|
| 35 |
+
"extract-image-data.zip", "w", compression=zipfile.ZIP_DEFLATED
|
| 36 |
+
) as archive:
|
| 37 |
+
for path in Path(".agents/skills/extract-image-data").rglob("*"):
|
| 38 |
+
archive.write(path, path.relative_to(".agents/skills"))
|
| 39 |
+
if include_gradio_skills:
|
| 40 |
+
for skill_folder in (
|
| 41 |
+
Path(".agents/skills/gradio"),
|
| 42 |
+
Path(".agents/skills/hf-gradio"),
|
| 43 |
+
):
|
| 44 |
+
for path in skill_folder.rglob("*"):
|
| 45 |
+
archive.write(path, path.relative_to(".agents/skills"))
|
| 46 |
+
return "extract-image-data.zip"
|
| 47 |
+
|
| 48 |
with gr.Blocks(title="Image Data Extractor") as demo:
|
| 49 |
gr.Markdown(
|
| 50 |
"# Image Data Extractor\n"
|
|
|
|
| 67 |
info="Choose a starting structure, then edit it below.",
|
| 68 |
)
|
| 69 |
generate_template_button = gr.Button("Generate template from Image")
|
| 70 |
+
with gr.Accordion(
|
| 71 |
+
"Structured JSON template", open=False
|
| 72 |
+
) as template_accordion:
|
| 73 |
template = gr.Textbox(
|
| 74 |
label="Structured JSON template",
|
| 75 |
value=structured_json_templates[default_structured_json_template],
|
|
|
|
| 83 |
open=True,
|
| 84 |
show_indices=True,
|
| 85 |
)
|
| 86 |
+
gr.Markdown(
|
| 87 |
+
"## Install the agent skill\n"
|
| 88 |
+
"Download the ZIP, extract it, and place its skill folders in your "
|
| 89 |
+
"project's `.agents/skills/` directory. Agents should read the "
|
| 90 |
+
"`SKILL.md` and use it whenever a task requires structured extraction "
|
| 91 |
+
"from an image. Keep **Include Gradio Skills** checked to include the "
|
| 92 |
+
"supporting `gradio` and `hf-gradio` skills."
|
| 93 |
+
)
|
| 94 |
+
include_gradio_skills = gr.Checkbox(
|
| 95 |
+
label="Include Gradio Skills",
|
| 96 |
+
value=True,
|
| 97 |
+
)
|
| 98 |
+
download_agentskill_button = gr.Button("Download agent skill")
|
| 99 |
+
download_agentskill_file = gr.File(label="Agent skill ZIP")
|
| 100 |
|
| 101 |
gr.Examples(
|
| 102 |
examples=[
|
|
|
|
| 125 |
api_name="select_structured_json_template",
|
| 126 |
)
|
| 127 |
|
| 128 |
+
download_agentskill_button.click(
|
| 129 |
+
download_agentskill,
|
| 130 |
+
inputs=include_gradio_skills,
|
| 131 |
+
outputs=download_agentskill_file,
|
| 132 |
+
api_name="download_agentskill",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
generate_template_button.click(
|
| 136 |
+
lambda: gr.Accordion(open=True),
|
| 137 |
+
outputs=template_accordion,
|
| 138 |
+
api_visibility="private",
|
| 139 |
+
show_progress="hidden",
|
| 140 |
+
).then(
|
| 141 |
generate_template,
|
| 142 |
inputs=image,
|
| 143 |
outputs=template,
|