Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -75,33 +75,59 @@ def set_custom_css():
|
|
75 |
""", unsafe_allow_html=True)
|
76 |
|
77 |
def get_docparser_data(file, api_key, parser_id) -> Optional[dict]:
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
try:
|
82 |
-
# Upload
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
if not document_id:
|
89 |
-
st.error("Failed to
|
90 |
return None
|
91 |
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
return data
|
103 |
except requests.exceptions.HTTPError as http_err:
|
104 |
st.error(f"HTTP error occurred: {http_err}")
|
|
|
|
|
105 |
except Exception as e:
|
106 |
st.error(f"Error fetching data from Docparser: {e}")
|
107 |
return None
|
|
|
75 |
""", unsafe_allow_html=True)
|
76 |
|
77 |
def get_docparser_data(file, api_key, parser_id) -> Optional[dict]:
|
78 |
+
# First, upload the document
|
79 |
+
upload_url = "https://api.docparser.com/v1/document/upload"
|
80 |
+
headers = {
|
81 |
+
'Authorization': f'Basic {api_key}'
|
82 |
+
}
|
83 |
+
files = {
|
84 |
+
'file': (file.name, file, 'application/pdf')
|
85 |
+
}
|
86 |
+
params = {
|
87 |
+
'parser_id': parser_id
|
88 |
+
}
|
89 |
+
|
90 |
try:
|
91 |
+
# Upload document
|
92 |
+
upload_response = requests.post(
|
93 |
+
upload_url,
|
94 |
+
headers=headers,
|
95 |
+
files=files,
|
96 |
+
params=params
|
97 |
+
)
|
98 |
+
upload_response.raise_for_status()
|
99 |
+
|
100 |
+
# Get document ID from upload response
|
101 |
+
upload_data = upload_response.json()
|
102 |
+
if not isinstance(upload_data, list) or len(upload_data) == 0:
|
103 |
+
st.error("Invalid response from Docparser upload")
|
104 |
+
return None
|
105 |
+
|
106 |
+
document_id = upload_data[0].get('id')
|
107 |
if not document_id:
|
108 |
+
st.error("Failed to get document ID from upload response")
|
109 |
return None
|
110 |
|
111 |
+
# Get parsed results
|
112 |
+
results_url = f"https://api.docparser.com/v1/results/{parser_id}/{document_id}"
|
113 |
+
results_response = requests.get(
|
114 |
+
results_url,
|
115 |
+
headers=headers
|
116 |
+
)
|
117 |
+
results_response.raise_for_status()
|
118 |
+
|
119 |
+
# Handle results
|
120 |
+
results_data = results_response.json()
|
121 |
+
if isinstance(results_data, list) and len(results_data) > 0:
|
122 |
+
return results_data[0]
|
123 |
+
else:
|
124 |
+
st.error("No parsed data received from Docparser")
|
125 |
+
return None
|
126 |
|
|
|
127 |
except requests.exceptions.HTTPError as http_err:
|
128 |
st.error(f"HTTP error occurred: {http_err}")
|
129 |
+
if http_err.response is not None:
|
130 |
+
st.error(f"Response content: {http_err.response.content}")
|
131 |
except Exception as e:
|
132 |
st.error(f"Error fetching data from Docparser: {e}")
|
133 |
return None
|