Views
No views yet
handler.py - Custom inference handler for HuggingFacerequirements.txt - Python dependenciesdeploy.py - Full deployment script (first time setup)update.py - Quick update script (fast iterations)test_endpoint.py - Comprehensive test suitequick_test.py - Simple quick test script1pip install huggingface_hub
2huggingface-cli loginpython deploy.py YOUR_USERNAMEhttps://huggingface.co/YOUR_USERNAME/deepseek-ocr-inferencepython update.py YOUR_USERNAME| Script | Use When | Speed | What it Does |
|---|---|---|---|
deploy.py | First time setup | ~2-3 min | Creates repo, copies config files from source, uploads your files |
update.py | Updating code/requirements | ~5-10 sec | Only uploads your local files |
deploy.py, use update.py for all future updates!1# Edit quick_test.py with your endpoint URL and token
2python quick_test.pypython test_endpoint.py --url YOUR_ENDPOINT_URL --token YOUR_HF_TOKEN --comprehensivepython test_endpoint.py --url YOUR_ENDPOINT_URL --token YOUR_HF_TOKEN --image path/to/image.jpg1curl https://YOUR_ENDPOINT_URL \
2 -H "Authorization: Bearer YOUR_HF_TOKEN" \
3 -H "Content-Type: application/json" \
4 -d '{
5 "inputs": "https://example.com/document.jpg",
6 "parameters": {
7 "prompt": "<image>\n<|grounding|>Convert the document to markdown."
8 }
9 }'quick_test.py1ENDPOINT_URL = "https://your-endpoint-url"
2HF_TOKEN = "hf_your_token"
3IMAGE_URL = "https://example.com/image.jpg" # or use LOCAL_IMAGEpython quick_test.py1# Run all test cases
2python test_endpoint.py --url YOUR_URL --token YOUR_TOKEN --comprehensive
3
4# Test with a specific image URL
5python test_endpoint.py --url YOUR_URL --token YOUR_TOKEN --image-url "https://example.com/doc.jpg"
6
7# Test with a local image
8python test_endpoint.py --url YOUR_URL --token YOUR_TOKEN --image path/to/document.pdf
9
10# Test with custom prompt
11python test_endpoint.py --url YOUR_URL --token YOUR_TOKEN \
12 --image-url "https://example.com/table.png" \
13 --prompt "<image>\n<|grounding|>Extract tables as markdown."1# Default - Markdown conversion
2"<image>\n<|grounding|>Convert the document to markdown."
3
4# Extract tables
5"<image>\n<|grounding|>Extract all tables as markdown tables."
6
7# Plain text only
8"<image>\n<|grounding|>Extract only the text without formatting."
9
10# Form extraction
11"<image>\n<|grounding|>Extract form fields and their values."
12
13# Structured extraction
14"<image>\n<|grounding|>Identify titles, headers, and body text."
15
16# Multilingual
17"<image>\n<|grounding|>Extract text in original language."POST https://YOUR_ENDPOINT_URLAuthorization: Bearer YOUR_HF_TOKEN
Content-Type: application/json1{
2 "inputs": "IMAGE_INPUT",
3 "parameters": {
4 "prompt": "CUSTOM_PROMPT",
5 "base_size": 1024,
6 "image_size": 640,
7 "crop_mode": true,
8 "save_results": false,
9 "test_compress": false
10 }
11}| Field | Type | Required | Default | Description |
|---|---|---|---|---|
inputs | string | Yes | - | Base64 encoded image, image URL, or data URI |
parameters.prompt | string | No | "<image>\n<|grounding|>Convert the document to markdown. " | Custom OCR prompt |
parameters.base_size | int | No | 1024 | Base image size for processing |
parameters.image_size | int | No | 640 | Crop image size |
parameters.crop_mode | bool | No | true | Whether to use crop mode |
parameters.save_results | bool | No | false | Save detailed results |
parameters.test_compress | bool | No | false | Test compression |
1[
2 {
3 "text": "# Document Title\n\nExtracted markdown content..."
4 }
5]1import requests
2
3url = "https://YOUR_ENDPOINT_URL"
4headers = {
5 "Authorization": "Bearer YOUR_HF_TOKEN",
6 "Content-Type": "application/json"
7}
8
9payload = {
10 "inputs": "https://example.com/invoice.pdf",
11 "parameters": {
12 "prompt": "<image>\n<|grounding|>Extract all text from this invoice."
13 }
14}
15
16response = requests.post(url, headers=headers, json=payload)
17result = response.json()
18print(result[0]["text"])1import base64
2import requests
3
4# Read and encode image
5with open("document.jpg", "rb") as f:
6 image_data = base64.b64encode(f.read()).decode()
7
8url = "https://YOUR_ENDPOINT_URL"
9headers = {
10 "Authorization": "Bearer YOUR_HF_TOKEN",
11 "Content-Type": "application/json"
12}
13
14payload = {
15 "inputs": image_data,
16 "parameters": {
17 "prompt": "<image>\n<|grounding|>Convert the document to markdown.",
18 "base_size": 1024,
19 "crop_mode": True
20 }
21}
22
23response = requests.post(url, headers=headers, json=payload)
24result = response.json()
25print(result[0]["text"])1import requests
2
3payload = {
4 "inputs": "data:image/jpeg;base64,/9j/4AAQSkZJRg...",
5 "parameters": {
6 "prompt": "<image>\n<|grounding|>Extract tables and text."
7 }
8}
9
10response = requests.post(
11 "https://YOUR_ENDPOINT_URL",
12 headers={
13 "Authorization": "Bearer YOUR_HF_TOKEN",
14 "Content-Type": "application/json"
15 },
16 json=payload
17)
18
19print(response.json()[0]["text"])1# Table extraction
2payload = {
3 "inputs": "https://example.com/table.png",
4 "parameters": {
5 "prompt": "<image>\n<|grounding|>Extract all tables as markdown tables."
6 }
7}
8
9# Form extraction
10payload = {
11 "inputs": "https://example.com/form.jpg",
12 "parameters": {
13 "prompt": "<image>\n<|grounding|>Extract form fields and values as JSON."
14 }
15}
16
17# Multilingual OCR
18payload = {
19 "inputs": "https://example.com/chinese.jpg",
20 "parameters": {
21 "prompt": "<image>\n<|grounding|>Extract text in original language."
22 }
23}1const endpoint = "https://YOUR_ENDPOINT_URL";
2const token = "YOUR_HF_TOKEN";
3
4async function ocr(imageUrl: string): Promise<string> {
5 const response = await fetch(endpoint, {
6 method: "POST",
7 headers: {
8 "Authorization": `Bearer ${token}`,
9 "Content-Type": "application/json",
10 },
11 body: JSON.stringify({
12 inputs: imageUrl,
13 parameters: {
14 prompt: "<image>\n<|grounding|>Convert the document to markdown.",
15 },
16 }),
17 });
18
19 const result = await response.json();
20 return result[0].text;
21}
22
23// Usage
24const text = await ocr("https://example.com/document.jpg");
25console.log(text);deploy.py and the model weights are uploaded.base_size or upgrade to a larger GPU instance.base_size for smaller documentshandler.py exists in your repository root and trust_remote_code=True is enabled in endpoint settings.handler.py line 19-22 to use a different model:1self.tokenizer = AutoTokenizer.from_pretrained(
2 "deepseek-ai/DeepSeek-OCR", # Change this
3 trust_remote_code=True
4)__call__ method to handle lists.