File size: 1,887 Bytes
af165a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import os

def process_wod_document(file_path, wod_type):
    """
    Process a WOD document using the remote API.
    
    Args:
        file_path: Path to the PDF file to analyze
        wod_type: Type of WOD (e.g., 'REPLACEMENT', 'THERMAL', etc.)
    
    Returns:
        dict: Parsed response from the API or error information
    """
    url = "https://datasaur-ai--wod-document-analyzer-app.modal.run/process"
    
    payload = {'wod_type': wod_type}
    
    try:
        # Open the file for upload
        with open(file_path, 'rb') as file:
            files = [
                ('file', (os.path.basename(file_path), file, 'application/pdf'))
            ]
            headers = {
                'Authorization': f'Bearer {os.getenv("API_KEY")}'
            }
            
            response = requests.post(url, headers=headers, data=payload, files=files)
            
        # Parse the JSON response
        if response.status_code == 200:
            return json.loads(response.text)
        else:
            return {
                "status": "error",
                "message": f"API request failed with status code {response.status_code}",
                "error_details": response.text
            }
            
    except FileNotFoundError:
        return {
            "status": "error",
            "message": f"File not found: {file_path}"
        }
    except requests.RequestException as e:
        return {
            "status": "error",
            "message": f"Network error: {str(e)}"
        }
    except json.JSONDecodeError:
        return {
            "status": "error",
            "message": "Invalid JSON response from API",
            "response_text": response.text
        }
    except Exception as e:
        return {
            "status": "error",
            "message": f"Unexpected error: {str(e)}"
        }