redhairedshanks1 commited on
Commit
bf161ca
·
1 Parent(s): 39d1de3

Update utilities/extract_tables.py

Browse files
Files changed (1) hide show
  1. utilities/extract_tables.py +53 -24
utilities/extract_tables.py CHANGED
@@ -1,25 +1,54 @@
1
- import os
2
- import requests
3
-
4
- EXTRACT_TABLES_API = "https://point9-extract-text-and-table.hf.space/api/tables" # Replace with your space URL
5
-
6
- def extract_tables_remote(state):
7
- filename = state["filename"]
8
- path = state["temp_files"][filename]
9
-
10
- with open(path, "rb") as f:
11
- files = {"file": (filename, f, "application/pdf")}
12
- data = {
13
- "filename": filename,
14
- "start_page": state.get("start_page", 1),
15
- "end_page": state.get("end_page", 1),
16
- }
17
- headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
18
- resp = requests.post(EXTRACT_TABLES_API, files=files, data=data, headers=headers)
19
-
20
- if resp.status_code != 200:
21
- raise RuntimeError(f"Extract tables API failed: {resp.text}")
22
-
23
- js = resp.json()
24
- state["tables"] = js.get("tables", js)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  return state
 
1
+ # import os
2
+ # import requests
3
+
4
+ # EXTRACT_TABLES_API = "https://point9-extract-text-and-table.hf.space/api/tables" # Replace with your space URL
5
+
6
+ # def extract_tables_remote(state):
7
+ # filename = state["filename"]
8
+ # path = state["temp_files"][filename]
9
+
10
+ # with open(path, "rb") as f:
11
+ # files = {"file": (filename, f, "application/pdf")}
12
+ # data = {
13
+ # "filename": filename,
14
+ # "start_page": state.get("start_page", 1),
15
+ # "end_page": state.get("end_page", 1),
16
+ # }
17
+ # headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
18
+ # resp = requests.post(EXTRACT_TABLES_API, files=files, data=data, headers=headers)
19
+
20
+ # if resp.status_code != 200:
21
+ # raise RuntimeError(f"Extract tables API failed: {resp.text}")
22
+
23
+ # js = resp.json()
24
+ # state["tables"] = js.get("tables", js)
25
+ # return state
26
+
27
+ import os
28
+ import requests
29
+
30
+ EXTRACT_TABLES_API = "https://point9-extract-text-and-table.hf.space/api/tables" # Replace with your space URL
31
+
32
+ def extract_tables_remote(state):
33
+ filename = state["filename"]
34
+ path = state["temp_files"][filename]
35
+
36
+ # Extract filename with extension from the path
37
+ filename_with_extension = os.path.basename(path)
38
+
39
+ with open(path, "rb") as f:
40
+ files = {"file": (filename_with_extension, f, "application/pdf")}
41
+ data = {
42
+ "filename": filename_with_extension, # Use filename with extension
43
+ "start_page": state.get("start_page", 1),
44
+ "end_page": state.get("end_page", 1),
45
+ }
46
+ headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_TOKEN')}"}
47
+ resp = requests.post(EXTRACT_TABLES_API, files=files, data=data, headers=headers)
48
+
49
+ if resp.status_code != 200:
50
+ raise RuntimeError(f"Extract tables API failed: {resp.text}")
51
+
52
+ js = resp.json()
53
+ state["tables"] = js.get("tables", js)
54
  return state