Spaces:
Build error
Build error
Merge branch 'main' into NhanHT
Browse files
app/configs/database.py
CHANGED
@@ -41,3 +41,21 @@ print("Firestore connected")
|
|
41 |
# Initialize Storage
|
42 |
firebase_bucket = storage.bucket(app=firebase_admin.get_app())
|
43 |
print("Storage connected")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Initialize Storage
|
42 |
firebase_bucket = storage.bucket(app=firebase_admin.get_app())
|
43 |
print("Storage connected")
|
44 |
+
|
45 |
+
# check if have question_tests collection
|
46 |
+
if "question_tests" not in [collection.id for collection in firebase_db.collections()]:
|
47 |
+
# Create a collection
|
48 |
+
firebase_db.collection("question_tests").add({"question_tests_description": "test", "question_tests_role": "test", "question_tests_url": "test"})
|
49 |
+
print("Collection question_tests created")
|
50 |
+
|
51 |
+
# check if have cvs collection
|
52 |
+
if "cvs" not in [collection.id for collection in firebase_db.collections()]:
|
53 |
+
# Create a collection
|
54 |
+
firebase_db.collection("cvs").add({"name_candidate": "test", "apply_position": "test", "cv_url": "test"})
|
55 |
+
print("Collection cvs created")
|
56 |
+
|
57 |
+
# check if have jds collection
|
58 |
+
if "jds" not in [collection.id for collection in firebase_db.collections()]:
|
59 |
+
# Create a collection
|
60 |
+
firebase_db.collection("jds").add({"position_applied_for": "test","jd_url": "test"})
|
61 |
+
print("Collection jds created")
|
app/modules/__init__.py
CHANGED
@@ -1,12 +1,18 @@
|
|
1 |
from fastapi import APIRouter
|
2 |
from app.modules.question_tests_retrieval import qtretrieval_router
|
3 |
-
from app.modules.crud_question_test import crud_question_tests_router
|
4 |
from app.modules.matching_cv import cvmatching_router
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
modules_router = APIRouter(prefix="/modules", tags=["modules"])
|
7 |
modules_router.include_router(qtretrieval_router)
|
8 |
-
modules_router.include_router(crud_question_tests_router)
|
9 |
modules_router.include_router(cvmatching_router)
|
|
|
|
|
|
|
10 |
|
11 |
@modules_router.get("/")
|
12 |
async def index():
|
|
|
1 |
from fastapi import APIRouter
|
2 |
from app.modules.question_tests_retrieval import qtretrieval_router
|
|
|
3 |
from app.modules.matching_cv import cvmatching_router
|
4 |
|
5 |
+
# crud import
|
6 |
+
from app.modules.crud_question_test import crud_question_tests_router
|
7 |
+
from app.modules.crud_cvs import crud_cvs_router
|
8 |
+
from app.modules.crud_jds import crud_jds_router
|
9 |
+
|
10 |
modules_router = APIRouter(prefix="/modules", tags=["modules"])
|
11 |
modules_router.include_router(qtretrieval_router)
|
|
|
12 |
modules_router.include_router(cvmatching_router)
|
13 |
+
modules_router.include_router(crud_question_tests_router)
|
14 |
+
modules_router.include_router(crud_cvs_router)
|
15 |
+
modules_router.include_router(crud_jds_router)
|
16 |
|
17 |
@modules_router.get("/")
|
18 |
async def index():
|
app/modules/crud_cvs/__init__.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, UploadFile, File
|
2 |
+
from typing import Annotated
|
3 |
+
|
4 |
+
from app.modules.crud_cvs.models.crud_cvs import get_all_cvs, create_cv, delete_cv
|
5 |
+
|
6 |
+
crud_cvs_router = APIRouter(prefix="/crud_cvs_router", tags=["crud_cvs_router"])
|
7 |
+
|
8 |
+
# [GET] all CVs
|
9 |
+
@crud_cvs_router.get("/")
|
10 |
+
async def index():
|
11 |
+
# Get all documents from the collection with id document
|
12 |
+
data = get_all_cvs()
|
13 |
+
return data
|
14 |
+
|
15 |
+
# [POST] add CV
|
16 |
+
@crud_cvs_router.post("/")
|
17 |
+
# only upload pdf or docx file
|
18 |
+
async def add_cv(name_candidate: str, apply_position: str, file_cv: Annotated[UploadFile, File(..., description="Upload cv file (upload .pdf or .docx file)")]):
|
19 |
+
try:
|
20 |
+
# take file_cv and cv_upload type file
|
21 |
+
file_cv_type = file_cv.filename.split(".")[-1]
|
22 |
+
if file_cv_type in ["pdf", "docx"]:
|
23 |
+
# create a new document
|
24 |
+
if create_cv({"name_candidate": name_candidate, "apply_position":apply_position, "cv_url": file_cv}):
|
25 |
+
return {"message": "CV added successfully"}
|
26 |
+
else:
|
27 |
+
return {"message": "Error while adding CV file to database"}
|
28 |
+
else:
|
29 |
+
return {"message": "File type not supported"}
|
30 |
+
except Exception as e:
|
31 |
+
return {"message": "Error", "error": str(e)}
|
32 |
+
|
33 |
+
# [DELETE] CV by id
|
34 |
+
@crud_cvs_router.delete("/{id}")
|
35 |
+
async def delete_cv_by_id(id: str):
|
36 |
+
# Delete a document by id
|
37 |
+
if delete_cv(id):
|
38 |
+
return {"message": f"CV have id {id} deleted successfully"}
|
39 |
+
else:
|
40 |
+
return {"message": "Error while deleting CV file from database"}
|
app/modules/crud_cvs/models/crud_cvs.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uuid
|
2 |
+
from app.configs.database import firebase_bucket, firebase_db
|
3 |
+
|
4 |
+
# CRUD operation
|
5 |
+
def upload_file_cvs(file):
|
6 |
+
re_name_file = str(uuid.uuid4()).replace("-","_") + "_" + file.filename
|
7 |
+
# upload file to firebase storage
|
8 |
+
blob = firebase_bucket.blob(re_name_file)
|
9 |
+
blob.upload_from_file(file.file)
|
10 |
+
# return gs link
|
11 |
+
return f"gs://{firebase_bucket.name}/{re_name_file}"
|
12 |
+
|
13 |
+
def remove_file_cvs(file_url):
|
14 |
+
# remove file from firebase storage using "gs://" link
|
15 |
+
blob = firebase_bucket.blob(file_url.split(f"gs://{firebase_bucket.name}/")[1])
|
16 |
+
blob.delete()
|
17 |
+
return True
|
18 |
+
|
19 |
+
def get_all_cvs():
|
20 |
+
# Get all documents from the collection
|
21 |
+
docs = firebase_db.collection("cvs").stream()
|
22 |
+
data = []
|
23 |
+
for doc in docs:
|
24 |
+
doc_data = doc.to_dict()
|
25 |
+
doc_data["id_cv"] = doc.id
|
26 |
+
data.append(doc_data)
|
27 |
+
return data
|
28 |
+
|
29 |
+
def get_cv_by_id(id):
|
30 |
+
# Get a document by id
|
31 |
+
doc = firebase_db.collection("cvs").document(id).get()
|
32 |
+
return doc.to_dict()
|
33 |
+
|
34 |
+
def create_cv(data):
|
35 |
+
# get file_cvs
|
36 |
+
file_cvs = data["cv_url"]
|
37 |
+
# upload file to firebase storage
|
38 |
+
file_url = upload_file_cvs(file_cvs)
|
39 |
+
# add file url to data
|
40 |
+
data["cv_url"] = file_url
|
41 |
+
# Create a new document
|
42 |
+
document_ref = firebase_db.collection("cvs").add(data)
|
43 |
+
# document_id = document_ref[1].id
|
44 |
+
return True
|
45 |
+
|
46 |
+
def delete_cv(id):
|
47 |
+
# Delete a file from firebase storage
|
48 |
+
file_url = get_cv_by_id(id)["cv_url"]
|
49 |
+
remove_file_cvs(file_url)
|
50 |
+
# Delete a document by id
|
51 |
+
firebase_db.collection("cvs").document(id).delete()
|
52 |
+
return True
|
app/modules/crud_jds/__init__.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, UploadFile, File
|
2 |
+
from typing import Annotated
|
3 |
+
|
4 |
+
from app.modules.crud_jds.models.crud_jds import get_all_jds, create_jd, delete_jd
|
5 |
+
|
6 |
+
crud_jds_router = APIRouter(prefix="/crud_jds_router", tags=["crud_jds_router"])
|
7 |
+
|
8 |
+
# [GET] all JDs
|
9 |
+
@crud_jds_router.get("/")
|
10 |
+
async def index():
|
11 |
+
# Get all documents from the collection with id document
|
12 |
+
data = get_all_jds()
|
13 |
+
return data
|
14 |
+
|
15 |
+
# [POST] add JD
|
16 |
+
@crud_jds_router.post("/")
|
17 |
+
# only upload pdf or json file
|
18 |
+
async def add_jd(position_applied_for: str, file_jd: Annotated[UploadFile, File(..., description="Upload jd file (upload .txt)")]):
|
19 |
+
try:
|
20 |
+
file_jd_type = file_jd.filename.split(".")[-1]
|
21 |
+
if file_jd_type in ["txt"]:
|
22 |
+
# create a new document
|
23 |
+
if create_jd({"position_applied_for": position_applied_for,"jd_url": file_jd}):
|
24 |
+
return {"message": "JD added successfully"}
|
25 |
+
else:
|
26 |
+
return {"message": "Error while adding JD file to database"}
|
27 |
+
else:
|
28 |
+
return {"message": "File type not supported"}
|
29 |
+
except Exception as e:
|
30 |
+
return {"message": "Error", "error": str(e)}
|
31 |
+
|
32 |
+
# [DELETE] JD by id
|
33 |
+
@crud_jds_router.delete("/{id}")
|
34 |
+
async def delete_jd_by_id(id: str):
|
35 |
+
# Delete a document by id
|
36 |
+
if delete_jd(id):
|
37 |
+
return {"message": f"JD have id {id} deleted successfully"}
|
38 |
+
else:
|
39 |
+
return {"message": "Error"}
|
app/modules/crud_jds/models/crud_jds.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uuid
|
2 |
+
from app.configs.database import firebase_bucket, firebase_db
|
3 |
+
|
4 |
+
|
5 |
+
# CRUD operation
|
6 |
+
def upload_file_jds(file):
|
7 |
+
re_name_file = str(uuid.uuid4()).replace("-","_") + "_" + file.filename
|
8 |
+
# upload file to firebase storage
|
9 |
+
blob = firebase_bucket.blob(re_name_file)
|
10 |
+
blob.upload_from_file(file.file)
|
11 |
+
# return gs link
|
12 |
+
return f"gs://{firebase_bucket.name}/{re_name_file}"
|
13 |
+
|
14 |
+
def remove_file_jds(file_url):
|
15 |
+
# remove file from firebase storage using "gs://" link
|
16 |
+
blob = firebase_bucket.blob(file_url.split(f"gs://{firebase_bucket.name}/")[1])
|
17 |
+
blob.delete()
|
18 |
+
return True
|
19 |
+
|
20 |
+
def get_all_jds():
|
21 |
+
# Get all documents from the collection
|
22 |
+
docs = firebase_db.collection("jds").stream()
|
23 |
+
data = []
|
24 |
+
for doc in docs:
|
25 |
+
doc_data = doc.to_dict()
|
26 |
+
doc_data["id_jd"] = doc.id
|
27 |
+
data.append(doc_data)
|
28 |
+
return data
|
29 |
+
|
30 |
+
def get_jd_by_id(id):
|
31 |
+
# Get a document by id
|
32 |
+
doc = firebase_db.collection("jds").document(id).get()
|
33 |
+
return doc.to_dict()
|
34 |
+
|
35 |
+
def create_jd(data):
|
36 |
+
# get file_jds
|
37 |
+
file_jds = data["jd_url"]
|
38 |
+
# upload file to firebase storage
|
39 |
+
file_url = upload_file_jds(file_jds)
|
40 |
+
# add file url to data
|
41 |
+
data["jd_url"] = file_url
|
42 |
+
# Create a new document
|
43 |
+
document_ref = firebase_db.collection("jds").add(data)
|
44 |
+
# document_id = document_ref[1].id
|
45 |
+
return True
|
46 |
+
|
47 |
+
def delete_jd(id):
|
48 |
+
# Delete a file from firebase storage
|
49 |
+
file_url = get_jd_by_id(id)["jd_url"]
|
50 |
+
remove_file_jds(file_url)
|
51 |
+
# Delete a document by id
|
52 |
+
firebase_db.collection("jds").document(id).delete()
|
53 |
+
return True
|
app/modules/matching_cv/models/matching_cv_logic.py
CHANGED
@@ -20,14 +20,6 @@ load_dotenv()
|
|
20 |
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY')
|
21 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
22 |
|
23 |
-
# def load cv from docx file
|
24 |
-
def load_cv_from_docx(file):
|
25 |
-
doc = docx.Document(file)
|
26 |
-
fullText = []
|
27 |
-
for para in doc.paragraphs:
|
28 |
-
fullText.append(para.text)
|
29 |
-
return '\n'.join(fullText)
|
30 |
-
|
31 |
# def matching cv and jd return percentage of matching using prompt template
|
32 |
def result_matching_cv_jd(cv_text, jd_text):
|
33 |
# create the prompt template
|
|
|
20 |
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY')
|
21 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# def matching cv and jd return percentage of matching using prompt template
|
24 |
def result_matching_cv_jd(cv_text, jd_text):
|
25 |
# create the prompt template
|