Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,87 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
-
from models import CVExtracted, InsertedText, JobAndCV, ClassificationResult, InsertedLink
|
3 |
-
import os
|
4 |
-
from io import BytesIO
|
5 |
-
import extractor
|
6 |
-
from datetime import datetime
|
7 |
-
from PyPDF2 import PdfReader
|
8 |
-
import requests
|
9 |
-
import classificator
|
10 |
-
|
11 |
-
os.environ['TRANSFORMERS_CACHE'] = '/transformers_cache'
|
12 |
-
os.environ['HF_HOME'] = '/transformers_cache'
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
app = FastAPI()
|
17 |
-
@app.get("/", response_model=dict[str, str])
|
18 |
-
def getall():
|
19 |
-
return {"hello":"world"}
|
20 |
-
|
21 |
-
|
22 |
-
@app.post("/ext", response_model=CVExtracted)
|
23 |
-
async def extract(text: InsertedText):
|
24 |
-
dictresult = extractor.predict(text.text)
|
25 |
-
return CVExtracted(**dictresult)
|
26 |
-
|
27 |
-
|
28 |
-
@app.post("/classify", response_model=ClassificationResult)
|
29 |
-
async def classify(body:JobAndCV ):
|
30 |
-
mininmal_start = 0
|
31 |
-
maximal_end = 0
|
32 |
-
positions = []
|
33 |
-
userMajors = []
|
34 |
-
if len(body.cv.experiences) > 0:
|
35 |
-
mininmal_start = datetime.strptime(body.cv.experiences[0]['start'], "%Y-%m-%d").date() if body.cv.experiences[0].get('start') != None else datetime.today().date()
|
36 |
-
maximal_end = datetime.strptime(body.cv.experiences[0]['end'], "%Y-%m-%d").date()
|
37 |
-
for exp in body.cv.experiences:
|
38 |
-
positions.append(exp['position'])
|
39 |
-
if exp.get('end') == None:
|
40 |
-
exp['end'] = datetime.today().strftime("%Y-%m-%d")
|
41 |
-
if datetime.strptime(exp['start'], "%Y-%m-%d").date() < mininmal_start:
|
42 |
-
mininmal_start = datetime.strptime(exp['start'], "%Y-%m-%d").date()
|
43 |
-
if datetime.strptime(exp['end'], "%Y-%m-%d").date() > maximal_end:
|
44 |
-
maximal_end = datetime.strptime(exp['end'], "%Y-%m-%d").date()
|
45 |
-
else:
|
46 |
-
mininmal_start = 0
|
47 |
-
maximal_end = 0
|
48 |
-
|
49 |
-
for edu in body.cv.educations:
|
50 |
-
userMajors.append(edu['major'])
|
51 |
-
|
52 |
-
yoe = (maximal_end - mininmal_start).days//365
|
53 |
-
cv = {
|
54 |
-
"experiences": str(body.cv.experiences),
|
55 |
-
"positions": str(positions),
|
56 |
-
"userMajors": str(userMajors),
|
57 |
-
"skills": str(body.cv.skills),
|
58 |
-
"yoe": yoe
|
59 |
-
}
|
60 |
-
job = {
|
61 |
-
"jobDesc": body.job.jobDesc,
|
62 |
-
"role": body.job.role,
|
63 |
-
"majors": str(body.job.majors),
|
64 |
-
"skills": str(body.job.skills),
|
65 |
-
"minYoE": body.job.minYoE
|
66 |
-
}
|
67 |
-
results = classificator.predict(cv, job)
|
68 |
-
return ClassificationResult(**results)
|
69 |
-
|
70 |
-
@app.post("/cv", response_model=CVExtracted)
|
71 |
-
async def extract(link: InsertedLink):
|
72 |
-
response = requests.get(link.link)
|
73 |
-
if response.status_code == 200:
|
74 |
-
# Open the PDF from bytes in memory
|
75 |
-
pdf_reader = PdfReader(BytesIO(response.content))
|
76 |
-
number_of_pages = len(pdf_reader.pages)
|
77 |
-
# Optionally, read text from the first page
|
78 |
-
page = pdf_reader.pages[0]
|
79 |
-
text = page.extract_text()
|
80 |
-
for i in range(1, number_of_pages):
|
81 |
-
text+= '\n' + pdf_reader.pages[i].extract_text()
|
82 |
-
else:
|
83 |
-
#return error, make 500 because file server error
|
84 |
-
raise HTTPException(status_code=response.status_code, detail="File server error")
|
85 |
-
|
86 |
-
dictresult = extractor.predict(text)
|
87 |
return CVExtracted(**dictresult)
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from models import CVExtracted, InsertedText, JobAndCV, ClassificationResult, InsertedLink
|
3 |
+
import os
|
4 |
+
from io import BytesIO
|
5 |
+
import extractor
|
6 |
+
from datetime import datetime
|
7 |
+
from PyPDF2 import PdfReader
|
8 |
+
import requests
|
9 |
+
import classificator
|
10 |
+
|
11 |
+
os.environ['TRANSFORMERS_CACHE'] = '/transformers_cache'
|
12 |
+
os.environ['HF_HOME'] = '/transformers_cache'
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
app = FastAPI()
|
17 |
+
@app.get("/", response_model=dict[str, str])
|
18 |
+
def getall():
|
19 |
+
return {"hello":"world"}
|
20 |
+
|
21 |
+
|
22 |
+
@app.post("/ext", response_model=CVExtracted)
|
23 |
+
async def extract(text: InsertedText):
|
24 |
+
dictresult = extractor.predict(text.text)
|
25 |
+
return CVExtracted(**dictresult)
|
26 |
+
|
27 |
+
|
28 |
+
@app.post("/classify", response_model=ClassificationResult)
|
29 |
+
async def classify(body:JobAndCV ):
|
30 |
+
mininmal_start = 0
|
31 |
+
maximal_end = 0
|
32 |
+
positions = []
|
33 |
+
userMajors = []
|
34 |
+
if len(body.cv.experiences) > 0:
|
35 |
+
mininmal_start = datetime.strptime(body.cv.experiences[0]['start'], "%Y-%m-%d").date() if body.cv.experiences[0].get('start') != None else datetime.today().date()
|
36 |
+
maximal_end = datetime.strptime(body.cv.experiences[0]['end'], "%Y-%m-%d").date() if body.cv.experiences[0].get('end') != None else datetime.today().date()
|
37 |
+
for exp in body.cv.experiences:
|
38 |
+
positions.append(exp['position'])
|
39 |
+
if exp.get('end') == None:
|
40 |
+
exp['end'] = datetime.today().strftime("%Y-%m-%d")
|
41 |
+
if datetime.strptime(exp['start'], "%Y-%m-%d").date() < mininmal_start:
|
42 |
+
mininmal_start = datetime.strptime(exp['start'], "%Y-%m-%d").date()
|
43 |
+
if datetime.strptime(exp['end'], "%Y-%m-%d").date() > maximal_end:
|
44 |
+
maximal_end = datetime.strptime(exp['end'], "%Y-%m-%d").date()
|
45 |
+
else:
|
46 |
+
mininmal_start = 0
|
47 |
+
maximal_end = 0
|
48 |
+
|
49 |
+
for edu in body.cv.educations:
|
50 |
+
userMajors.append(edu['major'])
|
51 |
+
|
52 |
+
yoe = (maximal_end - mininmal_start).days//365
|
53 |
+
cv = {
|
54 |
+
"experiences": str(body.cv.experiences),
|
55 |
+
"positions": str(positions),
|
56 |
+
"userMajors": str(userMajors),
|
57 |
+
"skills": str(body.cv.skills),
|
58 |
+
"yoe": yoe
|
59 |
+
}
|
60 |
+
job = {
|
61 |
+
"jobDesc": body.job.jobDesc,
|
62 |
+
"role": body.job.role,
|
63 |
+
"majors": str(body.job.majors),
|
64 |
+
"skills": str(body.job.skills),
|
65 |
+
"minYoE": body.job.minYoE
|
66 |
+
}
|
67 |
+
results = classificator.predict(cv, job)
|
68 |
+
return ClassificationResult(**results)
|
69 |
+
|
70 |
+
@app.post("/cv", response_model=CVExtracted)
|
71 |
+
async def extract(link: InsertedLink):
|
72 |
+
response = requests.get(link.link)
|
73 |
+
if response.status_code == 200:
|
74 |
+
# Open the PDF from bytes in memory
|
75 |
+
pdf_reader = PdfReader(BytesIO(response.content))
|
76 |
+
number_of_pages = len(pdf_reader.pages)
|
77 |
+
# Optionally, read text from the first page
|
78 |
+
page = pdf_reader.pages[0]
|
79 |
+
text = page.extract_text()
|
80 |
+
for i in range(1, number_of_pages):
|
81 |
+
text+= '\n' + pdf_reader.pages[i].extract_text()
|
82 |
+
else:
|
83 |
+
#return error, make 500 because file server error
|
84 |
+
raise HTTPException(status_code=response.status_code, detail="File server error")
|
85 |
+
|
86 |
+
dictresult = extractor.predict(text)
|
87 |
return CVExtracted(**dictresult)
|