File size: 1,698 Bytes
21ac63b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Example script for using the dots.ocr Space via API

Requirements:
    pip install gradio_client

Usage:
    python api_example.py --image path/to/image.jpg
"""

import argparse
from gradio_client import Client

def main():
    parser = argparse.ArgumentParser(description='Process document with dots.ocr API')
    parser.add_argument('--image', required=True, help='Path to image file')
    parser.add_argument('--space-url', default='YOUR_SPACE_URL_HERE', 
                        help='Hugging Face Space URL')
    parser.add_argument('--prompt-type', default='Full Layout + OCR (English)',
                        choices=['Full Layout + OCR (English)', 'OCR Only', 
                                'Layout Detection Only', 'Custom'],
                        help='Type of processing to perform')
    parser.add_argument('--custom-prompt', default='',
                        help='Custom prompt (only used if prompt-type is Custom)')
    
    args = parser.parse_args()
    
    print(f"Connecting to Space: {args.space_url}")
    client = Client(args.space_url)
    
    print(f"Processing image: {args.image}")
    print(f"Prompt type: {args.prompt_type}")
    
    result = client.predict(
        image=args.image,
        prompt_type=args.prompt_type,
        custom_prompt=args.custom_prompt,
        api_name="/predict"
    )
    
    print("\n" + "="*80)
    print("RESULT:")
    print("="*80)
    print(result)
    
    # Optionally save to file
    output_file = args.image.rsplit('.', 1)[0] + '_ocr_result.txt'
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(result)
    print(f"\nResult saved to: {output_file}")

if __name__ == "__main__":
    main()