wod-analyzer / python_request.py
datasaur-dev's picture
Create python_request.py
af165a1 verified
raw
history blame
1.89 kB
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)}"
}