-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_cv.py
More file actions
181 lines (133 loc) · 4.42 KB
/
process_cv.py
File metadata and controls
181 lines (133 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
import gearman
import requests
import subprocess
import tempfile
import os
import base64
import json
import shutil
import time
import argparse
HTTPAUTH = requests.auth.HTTPDigestAuth("","")
ENV = {
"local": ['localhost:4730'],
"staging": [],
"production": []
}
def timeout(cmd, timeout=30):
c = subprocess.Popen(cmd)
t = 0
while t < timeout and c.poll() is None:
time.sleep(0.25)
t+=0.25
if c.poll() is None:
c.terminate()
returncode = -1
else:
returncode = c.poll()
return returncode
def get_task(taskUrl):
if "staging" in taskUrl:
r = requests.get(taskUrl,verify=False,auth=HTTPAUTH)
else:
r = requests.get(taskUrl,verify=False)
return json.loads(r.text)
def pdf_to_text(srcPath):
text = ""
print("pdf_to_text")
inputDir, newFilename = os.path.split(srcPath)
with tempfile.NamedTemporaryFile(prefix="process_cv",suffix=newFilename) as f:
cmd = ["pdftotext","-layout","-nopgbrk",srcPath,f.name]
subprocess.call(cmd)
text = f.read()
return text
def unoconv_doc_to_text(srcPath):
text = ""
print(srcPath)
inputDir, newFilename = os.path.split(srcPath)
with tempfile.NamedTemporaryFile(prefix="process_cv",suffix=newFilename) as f:
cmd = ["unoconv","-T","5","-f","txt","-o",f.name,srcPath]
print(cmd)
timeout(cmd,10)
text = f.read()
return text
def unoconv_doc_to_img_base64(srcPath):
result = None
inputDir, newFilename = os.path.split(srcPath)
with tempfile.NamedTemporaryFile(prefix="process_cv",suffix=newFilename) as f:
cmd = ["unoconv","-T","5","-f","pdf","-o",f.name,srcPath]
#subprocess.call(cmd)
timeout(cmd,10)
result = pdf_to_img_base64(f.name)
return result
def pdf_to_img_base64(srcPath):
result = None
outDir = tempfile.mkdtemp(prefix="process_cv")
outPrefix = outDir + "/img"
cmd = ["pdftoppm","-gray","-png",srcPath,outPrefix]
subprocess.call(cmd)
with tempfile.NamedTemporaryFile(prefix="process_cv") as f:
cmd = ["montage", "-border","1",
"-tile","1x",
"-geometry","750x",
outPrefix + "*",f.name]
#subprocess.call(cmd)
timeout(cmd,10)
result = base64.b64encode(f.read())
shutil.rmtree(outDir)
return result
def process_cv(taskUrl):
task = get_task(taskUrl)
endpoint = task["env"]["endpoint"]
text = ""
img_base64 = None
if task["mimetype"] == "application/pdf":
img_base64 = pdf_to_img_base64(task["filepath"])
text = pdf_to_text(task["filepath"])
elif task["mimetype"] in ["application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document"]:
text = unoconv_doc_to_text(task["filepath"])
img_base64 = unoconv_doc_to_img_base64(task["filepath"])
post_data = {
"doc_id": task["doc_id"],
"filename": "cvimg",
"mimetype": "image/png",
"text": text,
"img_base64": img_base64
}
payload = json.dumps(post_data)
if "staging" in endpoint:
is_posted = requests.post(endpoint+"/tasks/process_cv",data=payload,verify=False,auth=HTTPAUTH)
else:
is_posted = requests.post(endpoint+"/tasks/process_cv",data=payload,verify=False)
return json.dumps(is_posted.json)
def gearman_process_cv(gearman_worker, gearman_job):
taskUrl = gearman_job.data
try:
process_cv(taskUrl)
except Exception as e:
print(e)
return ""
class ProcessCVTask(gearman.GearmanWorker):
def on_job_execute(self, current_job):
print("Job started")
return super(ProcessCVTask, self).on_job_execute(current_job)
def on_job_exception(self, current_job, exc_info):
print("Job failed")
return super(ProcessCVTask, self).on_job_exception(current_job, exc_info)
def on_job_complete(self, current_job, job_result):
print("Job complete")
return super(ProcessCVTask, self).send_job_complete(current_job, job_result)
def after_poll(self, any_activity):
# Return True if you want to continue polling, replaces callback_fxn
return True
def run(env):
gm_worker = ProcessCVTask(ENV[env])
gm_worker.register_task("process_cv", gearman_process_cv)
# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
gm_worker.work()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Processor")
parser.add_argument("--env", help="Environment to run",required=True,choices=("local","staging","production"))
args = parser.parse_args()
run(args.env)