File size: 10,465 Bytes
fbe0a52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
"""
Sample client for GLM-OCR API
Demonstrates various usage patterns
"""

import requests
import json
import sys
from pathlib import Path
from typing import Dict, List, Optional

class OCRClient:
    """Client for GLM-OCR API"""
    
    def __init__(self, base_url: str = "http://localhost:7860"):
        self.base_url = base_url
        self.session = requests.Session()
    
    def health_check(self) -> Dict:
        """Check if API is running"""
        try:
            response = self.session.get(f"{self.base_url}/health")
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            print(f"❌ Health check failed: {e}")
            return None
    
    def get_info(self) -> Dict:
        """Get API information"""
        try:
            response = self.session.get(f"{self.base_url}/api/info")
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            print(f"❌ Failed to get info: {e}")
            return None
    
    def extract_text(self, file_path: str) -> Optional[Dict]:
        """
        Extract text, tables, and formulas from document.
        
        Args:
            file_path: Path to document file
        
        Returns:
            Extraction results or None if failed
        """
        if not Path(file_path).exists():
            print(f"❌ File not found: {file_path}")
            return None
        
        try:
            with open(file_path, 'rb') as f:
                files = {'file': f}
                response = self.session.post(
                    f"{self.base_url}/api/ocr/extract",
                    files=files,
                    timeout=120
                )
            
            response.raise_for_status()
            return response.json()
        
        except requests.RequestException as e:
            print(f"❌ Extraction failed: {e}")
            return None
    
    def extract_structured(
        self,
        file_path: str,
        schema: Dict
    ) -> Optional[Dict]:
        """
        Extract structured information using JSON schema.
        
        Args:
            file_path: Path to document file
            schema: JSON schema defining extraction structure
        
        Returns:
            Extraction results or None if failed
        """
        if not Path(file_path).exists():
            print(f"❌ File not found: {file_path}")
            return None
        
        try:
            with open(file_path, 'rb') as f:
                files = {'file': f}
                data = {'schema': json.dumps(schema)}
                
                response = self.session.post(
                    f"{self.base_url}/api/ocr/extract-structured",
                    files=files,
                    data=data,
                    timeout=120
                )
            
            response.raise_for_status()
            return response.json()
        
        except requests.RequestException as e:
            print(f"❌ Structured extraction failed: {e}")
            return None
    
    def batch_extract(self, file_paths: List[str]) -> Optional[Dict]:
        """
        Process multiple files in batch.
        
        Args:
            file_paths: List of file paths to process
        
        Returns:
            Batch results or None if failed
        """
        if len(file_paths) > 10:
            print("❌ Maximum 10 files per batch")
            return None
        
        files = []
        for file_path in file_paths:
            if not Path(file_path).exists():
                print(f"⚠️  File not found: {file_path}")
                continue
            
            try:
                files.append(('files', open(file_path, 'rb')))
            except IOError as e:
                print(f"⚠️  Cannot open {file_path}: {e}")
        
        if not files:
            print("❌ No valid files to process")
            return None
        
        try:
            response = self.session.post(
                f"{self.base_url}/api/ocr/batch",
                files=files,
                timeout=300
            )
            
            # Close all file handles
            for _, file_obj in files:
                file_obj.close()
            
            response.raise_for_status()
            return response.json()
        
        except requests.RequestException as e:
            print(f"❌ Batch extraction failed: {e}")
            return None


def print_extraction_results(results: Dict):
    """Pretty print extraction results"""
    print("\n" + "="*80)
    print(f"πŸ“„ File: {results['filename']}")
    print(f"πŸ“‹ Type: {results['file_type']}")
    print(f"πŸ“‘ Pages: {results['total_pages']}")
    print("="*80)
    
    for page in results['pages']:
        print(f"\nπŸ“– Page {page['page_number']}:")
        print("-" * 80)
        
        if page.get('table'):
            print("\nπŸ”· TABLE CONTENT:")
            print(page['table'][:500] + ("..." if len(page['table']) > 500 else ""))
        
        if page.get('text'):
            print("\nπŸ“ TEXT CONTENT:")
            print(page['text'][:500] + ("..." if len(page['text']) > 500 else ""))
        
        if page.get('formula'):
            print("\nπŸ“ FORMULA CONTENT:")
            print(page['formula'][:500] + ("..." if len(page['formula']) > 500 else ""))


def print_structured_results(results: Dict):
    """Pretty print structured extraction results"""
    print("\n" + "="*80)
    print(f"πŸ“„ File: {results['filename']}")
    print(f"πŸ“‹ Type: {results['file_type']}")
    print("="*80)
    
    for page in results['pages']:
        print(f"\nπŸ“– Page {page['page_number']}:")
        print("-" * 80)
        
        extracted = page.get('extracted_data', {})
        print(json.dumps(extracted, indent=2, ensure_ascii=False))


def main():
    """Main example function"""
    
    # Initialize client
    client = OCRClient()
    
    # Check API health
    print("πŸ” Checking API health...")
    health = client.health_check()
    if health:
        print(f"βœ… API is healthy: {health['status']}")
    else:
        print("❌ Cannot connect to API. Make sure it's running on http://localhost:7860")
        sys.exit(1)
    
    # Get API info
    print("\nπŸ“š Getting API information...")
    info = client.get_info()
    if info:
        print(f"βœ… API: {info['api_name']} v{info['version']}")
        print(f"   Model: {info['model']}")
        print(f"   Supported formats: {', '.join(info['supported_formats']['images'] + info['supported_formats']['documents'] + info['supported_formats']['pdfs'])}")
    
    # Example 1: Extract from a single image/document
    print("\n" + "="*80)
    print("EXAMPLE 1: Single File Extraction")
    print("="*80)
    
    # Replace with your actual file path
    sample_file = "sample_document.pdf"
    
    if Path(sample_file).exists():
        print(f"\nπŸ“€ Extracting from: {sample_file}")
        results = client.extract_text(sample_file)
        
        if results:
            print_extraction_results(results)
            
            # Save results to file
            output_file = Path(sample_file).stem + "_extracted.json"
            with open(output_file, 'w', encoding='utf-8') as f:
                json.dump(results, f, indent=2, ensure_ascii=False)
            print(f"\nπŸ’Ύ Results saved to: {output_file}")
    else:
        print(f"\n⚠️  Sample file not found: {sample_file}")
        print("   Create a test file or provide a file path")
    
    # Example 2: Structured extraction
    print("\n" + "="*80)
    print("EXAMPLE 2: Structured Information Extraction")
    print("="*80)
    
    # Define extraction schema for ID card
    id_schema = {
        "id_number": "",
        "full_name": "",
        "date_of_birth": "",
        "address": {
            "street": "",
            "city": "",
            "state": "",
            "zip_code": ""
        },
        "issue_date": "",
        "expiration_date": ""
    }
    
    # Replace with your ID document path
    id_file = "id_card.jpg"
    
    if Path(id_file).exists():
        print(f"\nπŸ“€ Extracting structured data from: {id_file}")
        results = client.extract_structured(id_file, id_schema)
        
        if results:
            print_structured_results(results)
            
            # Save results
            output_file = Path(id_file).stem + "_structured.json"
            with open(output_file, 'w', encoding='utf-8') as f:
                json.dump(results, f, indent=2, ensure_ascii=False)
            print(f"\nπŸ’Ύ Results saved to: {output_file}")
    else:
        print(f"\n⚠️  ID file not found: {id_file}")
        print("   For structured extraction, provide a relevant document")
    
    # Example 3: Batch processing
    print("\n" + "="*80)
    print("EXAMPLE 3: Batch Processing")
    print("="*80)
    
    # List of files to process
    files_to_batch = [
        "document1.pdf",
        "document2.jpg",
        "document3.docx"
    ]
    
    # Filter existing files
    existing_files = [f for f in files_to_batch if Path(f).exists()]
    
    if existing_files:
        print(f"\nπŸ“€ Processing batch: {len(existing_files)} files")
        results = client.batch_extract(existing_files)
        
        if results:
            print(f"βœ… Batch timestamp: {results['batch_timestamp']}")
            print(f"   Total files: {results['total_files']}")
            
            for file_result in results['results']:
                if file_result['status'] == 'success':
                    print(f"   βœ… {file_result['filename']} - {file_result['total_pages']} pages")
                else:
                    print(f"   ❌ {file_result['filename']} - {file_result.get('error', 'Unknown error')}")
            
            # Save batch results
            with open("batch_results.json", 'w', encoding='utf-8') as f:
                json.dump(results, f, indent=2, ensure_ascii=False)
            print(f"\nπŸ’Ύ Results saved to: batch_results.json")
    else:
        print(f"\n⚠️  No files found for batch processing")
        print("   Prepare test documents and update file paths in this script")
    
    print("\n" + "="*80)
    print("βœ… Examples complete!")
    print("="*80)


if __name__ == "__main__":
    main()