Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -75,32 +75,35 @@ def set_custom_css():
|
|
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=
|
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')
|
@@ -108,26 +111,51 @@ def get_docparser_data(file, api_key, parser_id) -> Optional[dict]:
|
|
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=
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
75 |
""", unsafe_allow_html=True)
|
76 |
|
77 |
def get_docparser_data(file, api_key, parser_id) -> Optional[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
try:
|
79 |
+
# First, upload the document
|
80 |
+
upload_url = f"https://api.docparser.com/v1/document/upload/{parser_id}"
|
81 |
+
|
82 |
+
# Create proper headers with base64 encoded API key
|
83 |
+
import base64
|
84 |
+
auth_string = base64.b64encode(f"{api_key}:".encode()).decode()
|
85 |
+
headers = {
|
86 |
+
'Authorization': f'Basic {auth_string}',
|
87 |
+
'Content-Type': 'multipart/form-data'
|
88 |
+
}
|
89 |
+
|
90 |
+
# Prepare the file for upload
|
91 |
+
files = {
|
92 |
+
'file': (file.name, file, 'application/pdf')
|
93 |
+
}
|
94 |
+
|
95 |
# Upload document
|
96 |
upload_response = requests.post(
|
97 |
+
upload_url,
|
98 |
+
headers={'Authorization': f'Basic {auth_string}'},
|
99 |
+
files=files
|
|
|
100 |
)
|
101 |
upload_response.raise_for_status()
|
102 |
|
103 |
# Get document ID from upload response
|
104 |
upload_data = upload_response.json()
|
105 |
if not isinstance(upload_data, list) or len(upload_data) == 0:
|
106 |
+
st.error(f"Invalid response from Docparser upload: {upload_data}")
|
107 |
return None
|
108 |
|
109 |
document_id = upload_data[0].get('id')
|
|
|
111 |
st.error("Failed to get document ID from upload response")
|
112 |
return None
|
113 |
|
114 |
+
# Wait a moment for processing
|
115 |
+
import time
|
116 |
+
time.sleep(2) # Give Docparser time to process the document
|
117 |
+
|
118 |
# Get parsed results
|
119 |
results_url = f"https://api.docparser.com/v1/results/{parser_id}/{document_id}"
|
120 |
results_response = requests.get(
|
121 |
results_url,
|
122 |
+
headers={'Authorization': f'Basic {auth_string}'}
|
123 |
)
|
124 |
results_response.raise_for_status()
|
125 |
|
126 |
# Handle results
|
127 |
results_data = results_response.json()
|
128 |
+
|
129 |
+
# Debug information
|
130 |
+
st.write("Debug - API Response:", results_data)
|
131 |
+
|
132 |
if isinstance(results_data, list) and len(results_data) > 0:
|
133 |
+
# Extract the relevant fields based on your Docparser parser configuration
|
134 |
+
parsed_data = {
|
135 |
+
'name': results_data[0].get('full_name', 'Unknown'),
|
136 |
+
'email': results_data[0].get('email', 'Unknown'),
|
137 |
+
'phone': results_data[0].get('phone', 'Unknown'),
|
138 |
+
'skills': [skill.strip() for skill in results_data[0].get('skills', '').split(',') if skill.strip()],
|
139 |
+
'certifications': results_data[0].get('certifications', []),
|
140 |
+
'experience_years': float(results_data[0].get('experience_years', 0)),
|
141 |
+
'degree': results_data[0].get('degree', 'Not specified'),
|
142 |
+
'institution': results_data[0].get('institution', 'Not specified'),
|
143 |
+
'year': results_data[0].get('graduation_year', 'Not specified'),
|
144 |
+
'summary': results_data[0].get('summary', 'No summary available'),
|
145 |
+
'projects': results_data[0].get('projects', [])
|
146 |
+
}
|
147 |
+
return parsed_data
|
148 |
else:
|
149 |
+
st.error(f"No parsed data received from Docparser: {results_data}")
|
150 |
return None
|
151 |
|
152 |
except requests.exceptions.HTTPError as http_err:
|
153 |
st.error(f"HTTP error occurred: {http_err}")
|
154 |
+
if hasattr(http_err, 'response') and http_err.response is not None:
|
155 |
st.error(f"Response content: {http_err.response.content}")
|
156 |
+
except json.JSONDecodeError as json_err:
|
157 |
+
st.error(f"JSON decode error: {json_err}")
|
158 |
+
st.error("Raw response content: " + str(upload_response.content if 'upload_response' in locals() else 'No response'))
|
159 |
except Exception as e:
|
160 |
st.error(f"Error fetching data from Docparser: {e}")
|
161 |
return None
|