Download api_example.py from upprize/ocr: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/upprize/ocr/resolve/main/api_example.py
- Command line
-
hf download hf://spaces/upprize/ocr/api_example.py
-
curl -L -o api_example.py https://huggingface.co/spaces/upprize/ocr/resolve/main/api_example.py
1.7 kB
| """ | |
| 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() | |