datasaur-dev commited on
Commit
af165a1
·
verified ·
1 Parent(s): 24db518

Create python_request.py

Browse files
Files changed (1) hide show
  1. python_request.py +62 -0
python_request.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import os
4
+
5
+ def process_wod_document(file_path, wod_type):
6
+ """
7
+ Process a WOD document using the remote API.
8
+
9
+ Args:
10
+ file_path: Path to the PDF file to analyze
11
+ wod_type: Type of WOD (e.g., 'REPLACEMENT', 'THERMAL', etc.)
12
+
13
+ Returns:
14
+ dict: Parsed response from the API or error information
15
+ """
16
+ url = "https://datasaur-ai--wod-document-analyzer-app.modal.run/process"
17
+
18
+ payload = {'wod_type': wod_type}
19
+
20
+ try:
21
+ # Open the file for upload
22
+ with open(file_path, 'rb') as file:
23
+ files = [
24
+ ('file', (os.path.basename(file_path), file, 'application/pdf'))
25
+ ]
26
+ headers = {
27
+ 'Authorization': f'Bearer {os.getenv("API_KEY")}'
28
+ }
29
+
30
+ response = requests.post(url, headers=headers, data=payload, files=files)
31
+
32
+ # Parse the JSON response
33
+ if response.status_code == 200:
34
+ return json.loads(response.text)
35
+ else:
36
+ return {
37
+ "status": "error",
38
+ "message": f"API request failed with status code {response.status_code}",
39
+ "error_details": response.text
40
+ }
41
+
42
+ except FileNotFoundError:
43
+ return {
44
+ "status": "error",
45
+ "message": f"File not found: {file_path}"
46
+ }
47
+ except requests.RequestException as e:
48
+ return {
49
+ "status": "error",
50
+ "message": f"Network error: {str(e)}"
51
+ }
52
+ except json.JSONDecodeError:
53
+ return {
54
+ "status": "error",
55
+ "message": "Invalid JSON response from API",
56
+ "response_text": response.text
57
+ }
58
+ except Exception as e:
59
+ return {
60
+ "status": "error",
61
+ "message": f"Unexpected error: {str(e)}"
62
+ }