Spaces:
Sleeping
Sleeping
import io | |
import os | |
import openai | |
import re | |
import sqlite3 | |
import base64 | |
import calendar | |
import json | |
import time | |
import uuid | |
from reportlab.platypus import SimpleDocTemplate, Paragraph | |
from reportlab.lib.styles import getSampleStyleSheet | |
import streamlit as st | |
from streamlit_js_eval import streamlit_js_eval | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
from langchain.vectorstores.azuresearch import AzureSearch | |
from azure.storage.blob import BlobServiceClient | |
from azure.cosmos import CosmosClient, exceptions | |
from PyPDF2 import PdfReader | |
import openai | |
import sendgrid | |
from sendgrid.helpers.mail import Mail, Attachment, FileContent, FileName, FileType, Disposition | |
from twilio.rest import Client | |
import ssl | |
ssl._create_default_https_context = ssl._create_unverified_context | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
openai.api_base = "https://tensora-oai-sweden.openai.azure.com/" | |
openai.api_type = "azure" | |
openai.api_version = "2023-12-01-preview" | |
connection_string = os.getenv("CONNECTION") | |
blob_service_client = BlobServiceClient.from_connection_string(connection_string) | |
def upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs, pre_generated_bool, custom_questions): | |
try: | |
container_name = "jobdescriptions" | |
# json_blob_name = f"{pdf_name}_jsondata.json" | |
pdf_blob_name_jobdescription = f"{pdf_name}.pdf" | |
container_client = blob_service_client.get_container_client(container_name) | |
# json_blob_client = container_client.get_blob_client(json_blob_name) | |
# json_blob_client.upload_blob(json_data.encode('utf-8'), overwrite=True) | |
pdf_blob_client = container_client.get_blob_client(pdf_blob_name_jobdescription) | |
pdf_blob_client.upload_blob(pdf_data_jobdescription, overwrite=True) | |
upload_job_db_item(pdf_name,len(pdf_data_cvs),json.loads(json_data),pre_generated_bool, custom_questions) | |
if pre_generated_bool: | |
for i,question in enumerate(custom_questions): | |
question_nr_for_id = i+1 | |
question_id = pdf_name + "-question-nr-" + str(question_nr_for_id)+str(calendar.timegm(time.gmtime())) | |
upload_question_db_item(question_id, pdf_name, question,st.session_state["job_string"]) | |
links = [] | |
names = [] | |
for i,cv in enumerate(pdf_data_cvs): | |
cv_nr_for_id = i+1 | |
cv_session_state_string = "cv-"+str(cv_nr_for_id) | |
session_state_name = st.session_state["final_candidates"][i][0].metadata["name"] | |
names.append(session_state_name) | |
cv_id = pdf_name + "-cv-nr-" + str(cv_nr_for_id)+str(calendar.timegm(time.gmtime())) | |
upload_db_item(session_state_name, json.loads(json_data), pdf_name, cv_id) | |
pdf_blob_name_cv = f"{cv_id}.pdf" | |
pdf_blob_client = container_client.get_blob_client(pdf_blob_name_cv) | |
pdf_blob_client.upload_blob(pdf_data_cvs[i], overwrite=True) | |
links.append("https://tensora.ai/workgenius/cv-evaluation2/?job="+cv_id) | |
return links | |
except Exception as e: | |
print(f"Fehler beim Hochladen der Daten: {str(e)}") | |
return [] | |
def upload_job_db_item(id, number_of_applicants, data, pre_generated_bool, custom_questions): | |
endpoint = "https://wg-candidate-data.documents.azure.com:443/" | |
key = os.getenv("CONNECTION_DB") | |
client = CosmosClient(endpoint, key) | |
database = client.get_database_client("ToDoList") | |
container = database.get_container_client("JobData") | |
job_item = { | |
"id": id, | |
'partitionKey' : 'wg-job-data-v1', | |
"title": data["title"], | |
"number_of_applicants": number_of_applicants, | |
"every_interview_conducted": False, | |
"evaluation_email": data["email"], | |
"question_one": data["question_one"], | |
"question_two": data["question_two"], | |
"question_three": data["question_three"], | |
"pre_generated": pre_generated_bool, | |
"custom_questions": custom_questions | |
} | |
try: | |
# Fügen Sie das Element in den Container ein | |
container.create_item(body=job_item) | |
print("Eintrag erfolgreich in die Cosmos DB eingefügt. Container: Job Data") | |
except exceptions.CosmosHttpResponseError as e: | |
print(f"Fehler beim Schreiben in die Cosmos DB: {str(e)}") | |
except Exception as e: | |
print(f"Allgemeiner Fehler: {str(e)}") | |
def upload_db_item(name, data, job_description_id, cv_id): | |
endpoint = "https://wg-candidate-data.documents.azure.com:443/" | |
key = os.getenv("CONNECTION_DB") | |
client = CosmosClient(endpoint, key) | |
database = client.get_database_client("ToDoList") | |
container = database.get_container_client("Items") | |
candidate_item = { | |
"id": cv_id, | |
'partitionKey' : 'wg-candidate-data-v1', | |
"name": name, | |
"title": data["title"], | |
"interview_conducted": False, | |
"ai_summary": "", | |
"evaluation_email": data["email"], | |
"question_one": data["question_one"], | |
"question_two": data["question_two"], | |
"question_three": data["question_three"], | |
"job_description_id": job_description_id, | |
} | |
try: | |
# Fügen Sie das Element in den Container ein | |
container.create_item(body=candidate_item) | |
print("Eintrag erfolgreich in die Cosmos DB eingefügt. Container: Items(candidate Data)") | |
except exceptions.CosmosHttpResponseError as e: | |
print(f"Fehler beim Schreiben in die Cosmos DB: {str(e)}") | |
except Exception as e: | |
print(f"Allgemeiner Fehler: {str(e)}") | |
def upload_question_db_item(id, job_id, question, job_content): | |
endpoint = "https://wg-candidate-data.documents.azure.com:443/" | |
key = os.getenv("CONNECTION_DB") | |
client = CosmosClient(endpoint, key) | |
database = client.get_database_client("ToDoList") | |
container = database.get_container_client("Questions") | |
question_item = { | |
"id": id, | |
"partitionKey" : "wg-question-data-v1", | |
"job_id": job_id, | |
"question_content": question, | |
"job_description": job_content, | |
} | |
try: | |
# Fügen Sie das Element in den Container ein | |
container.create_item(body=question_item) | |
print("Eintrag erfolgreich in die Cosmos DB eingefügt. Container: Questions(Question Data)") | |
except exceptions.CosmosHttpResponseError as e: | |
print(f"Fehler beim Schreiben in die Cosmos DB: {str(e)}") | |
except Exception as e: | |
print(f"Allgemeiner Fehler: {str(e)}") | |
st.markdown( | |
""" | |
<style> | |
[data-testid=column]{ | |
text-align: center; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
} | |
h3{ | |
text-align: left; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True, | |
) | |
with open("sys_prompt_frontend.txt") as f: | |
sys_prompt = f.read() | |
with open("sys_prompt_job_optimization.txt") as j: | |
sys_prompt_optimization = j.read() | |
def adjust_numbering(lst): | |
return [f"{i + 1}. {item.split('. ', 1)[1]}" for i, item in enumerate(lst)] | |
def generate_candidate_mail(candidate, chat_link)-> str: | |
candidate_first_name = candidate[0].metadata["name"].split(" ")[0] | |
prompt = f"You are a professional recruiter who has selected a suitable candidate on the basis of a job description. Your task is to write two to three sentences about the applicant and explain why we think they are suitable for the job. The text will then be used in an e-mail to the applicant, so please address it to them. Please start the e-mail with 'Dear {candidate_first_name}'. I'll write the end of the mail myself." | |
try: | |
res = openai.ChatCompletion.create( | |
engine="gpt-4", | |
temperature=0.2, | |
messages=[ | |
{ | |
"role": "system", | |
"content": prompt, | |
}, | |
{"role": "system", "content": "Job description: "+st.session_state["job_string"]+"; Resume: "+candidate[0].page_content} | |
], | |
) | |
# print(res.choices[0]["message"]["content"]) | |
except Exception as e: | |
# Iterativ die Anfrage wiederholen und 200 Chars von hinten vom Resume weglassen | |
max_retries = 5 | |
retries = 0 | |
while retries < max_retries: | |
try: | |
# Reduziere die Länge des Resume um 200 Chars von hinten | |
candidate[0].page_content = candidate[0].page_content[:-200] | |
# Neue Anfrage senden | |
res = openai.ChatCompletion.create( | |
engine="gpt-4", | |
temperature=0.2, | |
messages=[ | |
{ | |
"role": "system", | |
"content": prompt, | |
}, | |
{"role": "system", "content": "Job description: " + st.session_state["job_string"] + "; Resume: " + candidate[0].page_content} | |
], | |
) | |
# print(res.choices[0]["message"]["content"]) | |
# Wenn die Anfrage erfolgreich ist, den Schleifen-Iterator beenden | |
break | |
except Exception as e: | |
# Bei erneuter Ausnahme die Schleife fortsetzen | |
retries += 1 | |
if retries == max_retries: | |
# Falls die maximale Anzahl von Wiederholungen erreicht ist, handle die Ausnahme entsprechend | |
print("Max retries reached. Unable to get a valid response.") | |
return "The CV was too long to generate a Mail" | |
# Hier kannst du zusätzlichen Code für den Fall implementieren, dass die maximale Anzahl von Wiederholungen erreicht wurde. | |
# Optional: Füge eine Wartezeit zwischen den Anfragen hinzu, um API-Beschränkungen zu respektieren | |
time.sleep(1) | |
output_string = f"""{res.choices[0]["message"]["content"]} | |
We have added the job description to the mail attachment. | |
If you are interested in the position, please click on the following link, answer a few questions from our chatbot for about 10-15 minutes and we will get back to you. | |
Link to the interview chatbot: {chat_link} | |
Sincerely, | |
WorkGenius | |
""" | |
print("Mail generated") | |
return output_string | |
def generate_job_bullets(job)->str: | |
prompt = "You are a professional recruiter whose task is to summarize the provided job description in the most important 5 key points. The key points should have a maximum of 8 words. The only thing you should return are the bullet points." | |
try: | |
res = openai.ChatCompletion.create( | |
engine="gpt-4", | |
temperature=0.2, | |
messages=[ | |
{ | |
"role": "system", | |
"content": prompt, | |
}, | |
{"role": "system", "content": "Job description: "+job} | |
], | |
) | |
# print(res.choices[0]["message"]["content"]) | |
output_string = f"""{res.choices[0]["message"]["content"]}""" | |
# print(output_string) | |
return output_string | |
except Exception as e: | |
print(f"Fehler beim generieren der Bullets: {str(e)}") | |
def check_keywords_in_content(database_path, table_name, input_id, keywords): | |
# Verbindung zur Datenbank herstellen | |
conn = sqlite3.connect(database_path) | |
cursor = conn.cursor() | |
# SQL-Abfrage, um die Zeile mit der angegebenen ID abzurufen | |
cursor.execute(f'SELECT * FROM {table_name} WHERE id = ?', (input_id,)) | |
# Ergebnis abrufen | |
row = cursor.fetchone() | |
# Wenn die Zeile nicht gefunden wurde, False zurückgeben | |
if not row: | |
conn.close() | |
print("ID not found") | |
return False | |
# Überprüfen, ob die Keywords in der Spalte content enthalten sind (case-insensitive) | |
content = row[1].lower() # Annahme: content ist die zweite Spalte, und wir wandeln ihn in Kleinbuchstaben um | |
keywords_lower = [keyword.lower() for keyword in keywords] | |
contains_keywords = all(keyword in content for keyword in keywords_lower) | |
# Verbindung schließen | |
conn.close() | |
return contains_keywords | |
def clear_temp_candidates(): | |
if not st.session_state["final_candidates"]: | |
print("i am cleared") | |
st.session_state["docs_res"] = [] | |
def load_candidates(fillup): | |
with st.spinner("Load the candidates, this may take a moment..."): | |
# print(st.session_state["job_string"]) | |
filter_string = "" | |
query_string = "The following keywords must be included: " + text_area_params + " " + st.session_state["job_string"] | |
checked_candidates = [] | |
db_path = 'cvdb.db' | |
table_name = 'files' | |
candidates_per_search = 100 | |
target_candidates_count = 10 | |
current_offset = 0 | |
if st.session_state["screened"]: | |
filter_string = "amount_screenings gt 0 " | |
if st.session_state["handed"]: | |
if len(filter_string) > 0: | |
filter_string += "and amount_handoffs gt 0 " | |
else: | |
filter_string += "amount_handoffs gt 0 " | |
if st.session_state["placed"]: | |
if len(filter_string) > 0: | |
filter_string += "and amount_placed gt 0" | |
else: | |
filter_string += "amount_placed gt 0" | |
# print(filter_string) | |
if not fillup: | |
while len(checked_candidates) < target_candidates_count: | |
# # Führe eine similarity search durch und erhalte 100 Kandidaten | |
# if st.session_state["search_type"]: | |
# print("hybrid") | |
# # raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
# raw_candidates = st.session_state["db"].hybrid_search_with_score(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
# else: | |
# print("similarity") | |
# # raw_candidates = st.session_state["db"].similarity_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
# raw_candidates = st.session_state["db"].similarity_search_with_relevance_scores(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
#"Similarity", "Hybrid", "Semantic ranking" | |
if st.session_state["search_radio"] == "Similarity": | |
print("similarity") | |
# raw_candidates = st.session_state["db"].similarity_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
raw_candidates = st.session_state["db"].similarity_search_with_relevance_scores(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
elif st.session_state["search_radio"] == "Hybrid": | |
print("hybrid") | |
# raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
raw_candidates = st.session_state["db"].hybrid_search_with_score(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
elif st.session_state["search_radio"] == "Semantic ranking": | |
print("Semantic ranking") | |
print("Filter string"+filter_string) | |
print("query"+query_string) | |
print("offset: "+str(candidates_per_search+current_offset)) | |
# raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
try: | |
raw_candidates = st.session_state["db"].semantic_hybrid_search_with_score_and_rerank(query_string, k=50, filters=filter_string) | |
except Exception as e: | |
print(f"Fehler beim laden der Kandidaten: {str(e)}") | |
raw_candidates = [] | |
st.warning("Something went wrong. Please press 'Search candidates' again or reload the page.") | |
for candidate in raw_candidates[current_offset:]: | |
candidates_id = candidate[0].metadata["source"].split("/")[-1] | |
keyword_bool = check_keywords_in_content(db_path, table_name, candidates_id, text_area_params.split(',')) | |
if keyword_bool: | |
checked_candidates.append(candidate) | |
# Überprüfe, ob die Zielanzahl erreicht wurde und breche die Schleife ab, wenn ja | |
if len(checked_candidates) >= target_candidates_count: | |
break | |
current_offset += candidates_per_search | |
if current_offset == 600: | |
break | |
# Setze die Ergebnisse in der Session State Variable | |
st.session_state["docs_res"] = checked_candidates | |
st.session_state["candidate_offset"] = current_offset | |
if len(checked_candidates) == 0: | |
st.error("No candidates can be found with these keywords. Please adjust the keywords and try again.", icon="🚨") | |
else: | |
# Setze die Zielanzahl auf 10 | |
target_candidates_count = 10 | |
current_offset = st.session_state["candidate_offset"] | |
# Solange die Anzahl der überprüften Kandidaten kleiner als die Zielanzahl ist | |
while len(st.session_state["docs_res"]) < target_candidates_count: | |
# Führe eine similarity search durch und erhalte 100 Kandidaten | |
if st.session_state["search_radio"] == "Similarity": | |
print("similarity") | |
# raw_candidates = st.session_state["db"].similarity_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
raw_candidates = st.session_state["db"].similarity_search_with_relevance_scores(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
elif st.session_state["search_radio"] == "Hybrid": | |
print("hybrid") | |
# raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
raw_candidates = st.session_state["db"].hybrid_search_with_score(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
elif st.session_state["search_radio"] == "Semantic ranking": | |
print("Semantic ranking") | |
print("Filter string"+filter_string) | |
print("query"+query_string) | |
print("offset: "+str(candidates_per_search+current_offset)) | |
# raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string) | |
try: | |
raw_candidates = st.session_state["db"].semantic_hybrid_search_with_score_and_rerank(query_string, k=50, filters=filter_string) | |
except Exception as e: | |
print(f"Fehler beim laden der Kandidaten: {str(e)}") | |
raw_candidates = [] | |
st.warning("Something went wrong. Please press 'Search candidates' again or reload the page.") | |
temp_offset_add = 0 | |
for candidate in raw_candidates[current_offset:]: | |
candidates_id = candidate[0].metadata["source"].split("/")[-1] | |
keyword_bool = check_keywords_in_content(db_path, table_name, candidates_id, text_area_params.split(',')) | |
if keyword_bool: | |
st.session_state["docs_res"].append(candidate) | |
temp_offset_add += 1 | |
# Überprüfe, ob die Zielanzahl erreicht wurde und breche die Schleife ab, wenn ja | |
if len(st.session_state["docs_res"]) >= target_candidates_count: | |
st.session_state["candidate_offset"] = current_offset+temp_offset_add | |
break | |
current_offset += candidates_per_search | |
if current_offset == 900: | |
break | |
# Wenn die Liste immer noch leer ist, zeige eine Fehlermeldung an | |
if len(st.session_state["docs_res"]) == 0: | |
st.warning("No more candidates can be found.", icon="🔥") | |
if "similarity_search_string" not in st.session_state: | |
st.session_state["similarity_search_string"] = None | |
if "job_string" not in st.session_state: | |
st.session_state["job_string"] = None | |
if "docs_res" not in st.session_state: | |
st.session_state["docs_res"] = None | |
if "final_candidates" not in st.session_state: | |
st.session_state["final_candidates"] = None | |
if "final_question_string" not in st.session_state: | |
st.session_state["final_question_string"] = [] | |
if "ai_questions" not in st.session_state: | |
st.session_state["ai_questions"] = None | |
if "raw_job" not in st.session_state: | |
st.session_state["raw_job"] = None | |
if "optimized_job" not in st.session_state: | |
st.session_state["optimized_job"] = None | |
if "candidate_offset" not in st.session_state: | |
st.session_state["candidate_offset"] = 0 | |
if "db" not in st.session_state: | |
embedder = OpenAIEmbeddings(deployment="embedding", chunk_size=1) | |
embedding_function = embedder.embed_query | |
db = AzureSearch( | |
index_name="wg-cvs-data", | |
azure_search_endpoint=os.environ.get("AZURE_SEARCH_ENDPOINT"), | |
azure_search_key=os.environ.get("AZURE_SEARCH_KEY"), | |
embedding_function=embedding_function, | |
# fields=fields | |
) | |
st.session_state["db"] = db | |
col1, col2 = st.columns([2, 1]) | |
col1.title("Candidate Search") | |
col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg") | |
st.write("Please upload the job description for which you would like candidates to be proposed.") | |
uploaded_file_jobdescription = st.file_uploader("Upload the job description:", type=["pdf"], key="job") | |
# col_file, col_clear = st.columns([6,1]) | |
# with col_file: | |
# uploaded_file_jobdescription = st.file_uploader("Upload the job description:", type=["pdf"], key="job") | |
# with col_clear: | |
# if st.button("Clear", use_container_width=True): | |
# streamlit_js_eval(js_expressions="parent.window.location.reload()") | |
if st.session_state["job"]: | |
if not st.session_state["job_string"]: | |
if not st.session_state["optimized_job"]: | |
with st.spinner("Optimizing the job description. This may take a moment..."): | |
pdf_data_jobdescription = st.session_state["job"].read() | |
pdf_data_jobdescription_string = "" | |
pdf_reader_job = PdfReader(io.BytesIO(pdf_data_jobdescription)) | |
for page_num in range(len(pdf_reader_job.pages)): | |
page = pdf_reader_job.pages[page_num] | |
pdf_data_jobdescription_string += page.extract_text() | |
# st.session_state["pdf_data_jobdescription"] = pdf_data_jobdescription activate and add sessio state if data is needed | |
system_prompt_job = sys_prompt_optimization.format(job=pdf_data_jobdescription_string) | |
try: | |
res = openai.ChatCompletion.create( | |
engine="gpt-4", | |
temperature=0.2, | |
messages=[ | |
{ | |
"role": "system", | |
"content": system_prompt_job, | |
}, | |
], | |
) | |
# print(res.choices[0]["message"]["content"]) | |
output_string = f"""{res.choices[0]["message"]["content"]}""" | |
st.session_state["optimized_job"] = output_string | |
st.rerun() | |
except Exception as e: | |
print(f"Fehler beim generieren der optimierten JD: {str(e)}") | |
st.error("An error has occurred. Please reload the page or contact the admin.", icon="🚨") | |
# st.session_state["job_string"] = pdf_data_jobdescription_string | |
# print(output_string) | |
st.text_area("This is the AI-generated optimized job description. If necessary, change something to your liking:", value=st.session_state["optimized_job"], height=700, key="optimized_job_edited") | |
if st.button("Accept the job description"): | |
st.session_state["job_string"] = st.session_state["optimized_job_edited"] | |
st.rerun() | |
# st.write("Switch from a similarity search (default) to a hybrid search (activated)") | |
# st.toggle("Switch Search", key="search_type") | |
st.radio("Select a search variant",options=["Similarity", "Hybrid", "Semantic ranking"], key="search_radio",on_change=clear_temp_candidates) | |
st.write("Activate the following toggles to filter according to the respective properties:") | |
col_screening, col_handoff, col_placed = st.columns([1,1,1]) | |
with col_screening: | |
st.toggle("Screened", key="screened") | |
with col_handoff: | |
st.toggle("Handed over", key="handed") | |
with col_placed: | |
st.toggle("Placed", key="placed") | |
text_area_params = st.text_area(label="Add additional search parameters, which are separated by commas (e.g. master, phd, web developer, spanish)") | |
submit = st.button("Search candidates",disabled= True if st.session_state["final_candidates"] else False) | |
if not st.session_state["job"] and submit: | |
st.error("Please upload a job description to search for candidates") | |
if st.session_state["docs_res"] and submit: | |
load_candidates(False) | |
if (st.session_state["job_string"] and submit) or st.session_state["docs_res"]: | |
# if not st.session_state["job_string"]: | |
# pdf_data_jobdescription = st.session_state["job"].read() | |
# pdf_data_jobdescription_string = "" | |
# pdf_reader_job = PdfReader(io.BytesIO(pdf_data_jobdescription)) | |
# for page_num in range(len(pdf_reader_job.pages)): | |
# page = pdf_reader_job.pages[page_num] | |
# pdf_data_jobdescription_string += page.extract_text() | |
# # st.session_state["pdf_data_jobdescription"] = pdf_data_jobdescription activate and add sessio state if data is needed | |
# st.session_state["job_string"] = pdf_data_jobdescription_string | |
if not st.session_state["docs_res"]: | |
load_candidates(False) | |
if not st.session_state["final_candidates"]: | |
for i,doc in enumerate(st.session_state["docs_res"]): | |
# print(doc) | |
cols_final = st.columns([6,1]) | |
with cols_final[1]: | |
if st.button("Remove",use_container_width=True,key="btn_rm_cv_row_"+str(i)): | |
# st.write(doc.page_content) | |
st.session_state["docs_res"].pop(i) | |
st.rerun() | |
with cols_final[0]: | |
# st.subheader(doc.metadata["source"]) | |
if st.session_state["search_radio"] == "Similarity": | |
with st.expander(doc[0].metadata["name"]+" with a similarity percentage of: "+str(round(doc[1] * 100, 3))+ "%"): | |
st.write(doc[0].page_content) | |
elif st.session_state["search_radio"] == "Hybrid": | |
with st.expander(doc[0].metadata["name"]+" with a hybrid search score of: "+str(round(doc[1] * 100, 3))): | |
st.write(doc[0].page_content) | |
elif st.session_state["search_radio"] == "Semantic ranking": | |
with st.expander(doc[0].metadata["name"]+" with a rerank score of: "+str(round(doc[2] * 100, 3))): | |
st.write(doc[0].page_content) | |
if len(st.session_state["docs_res"])>=10: | |
if st.button("Accept candidates", key="accept_candidates_btn"): | |
print("hello") | |
st.session_state["final_candidates"] = st.session_state["docs_res"].copy() | |
st.rerun() | |
else: | |
col_accept, col_empty ,col_load_new = st.columns([2, 3, 2]) | |
with col_accept: | |
if st.button("Accept candidates", key="accept_candidates_btn"): | |
print("hello") | |
st.session_state["final_candidates"] = st.session_state["docs_res"].copy() | |
st.rerun() | |
with col_load_new: | |
if st.button("Load new candidates", key="load_new_candidates"): | |
print("loading new candidates") | |
load_candidates(True) | |
st.rerun() | |
else: | |
print("Now Questions") | |
st.subheader("Your Candidates:") | |
st.write(", ".join(candidate[0].metadata["name"] for candidate in st.session_state["final_candidates"])) | |
# for i,candidate in enumerate(st.session_state["final_candidates"]): | |
# st.write(candidate.metadata["source"]) | |
cv_strings = "; Next CV: ".join(candidate[0].page_content for candidate in st.session_state["final_candidates"]) | |
# print(len(cv_strings)) | |
system = sys_prompt.format(job=st.session_state["job_string"], resume=st.session_state["final_candidates"][0][0].page_content, n=15) | |
if not st.session_state["ai_questions"]: | |
try: | |
# st.write("The questions are generated. This may take a short moment...") | |
st.info("The questions are generated. This may take a short moment.", icon="ℹ️") | |
with st.spinner("Loading..."): | |
res = openai.ChatCompletion.create( | |
engine="gpt-4", | |
temperature=0.2, | |
messages=[ | |
{ | |
"role": "system", | |
"content": system, | |
}, | |
], | |
) | |
st.session_state["ai_questions"] = [item for item in res.choices[0]["message"]["content"].split("\n") if len(item) > 0] | |
for i,q in enumerate(res.choices[0]["message"]["content"].split("\n")): | |
st.session_state["disable_row_"+str(i)] = False | |
st.rerun() | |
except Exception as e: | |
print(f"Fehler beim generieren der Fragen: {str(e)}") | |
st.error("An error has occurred. Please reload the page or contact the admin.", icon="🚨") | |
else: | |
if len(st.session_state["final_question_string"]) <= 0: | |
for i,question in enumerate(st.session_state["ai_questions"]): | |
cols = st.columns([5,1]) | |
with cols[1]: | |
# if st.button("Accept",use_container_width=True,key="btn_accept_row_"+str(i)): | |
# print("accept") | |
# pattern = re.compile(r"^[1-9][0-9]?\.") | |
# questions_length = len(st.session_state["final_question_string"]) | |
# question_from_text_area = st.session_state["text_area_"+str(i)] | |
# question_to_append = str(questions_length+1)+"."+re.sub(pattern, "", question_from_text_area) | |
# st.session_state["final_question_string"].append(question_to_append) | |
# st.session_state["disable_row_"+str(i)] = True | |
# st.rerun() | |
if st.button("Delete",use_container_width=True,key="btn_del_row_"+str(i)): | |
print("delete") | |
st.session_state["ai_questions"].remove(question) | |
st.rerun() | |
with cols[0]: | |
st.text_area(label="Question "+str(i+1)+":",value=question,label_visibility="collapsed",key="text_area_"+str(i),disabled=st.session_state["disable_row_"+str(i)]) | |
st.write("If you are satisfied with the questions, then accept them. You can still sort them afterwards.") | |
if st.button("Accept all questions",use_container_width=True,key="accept_all_questions"): | |
for i,question in enumerate(st.session_state["ai_questions"]): | |
pattern = re.compile(r"^[1-9][0-9]?\.") | |
questions_length = len(st.session_state["final_question_string"]) | |
question_from_text_area = st.session_state["text_area_"+str(i)] | |
question_to_append = str(questions_length+1)+"."+re.sub(pattern, "", question_from_text_area) | |
st.session_state["final_question_string"].append(question_to_append) | |
st.session_state["disable_row_"+str(i)] = True | |
st.rerun() | |
for i,final_q in enumerate(st.session_state["final_question_string"]): | |
cols_final = st.columns([5,1]) | |
with cols_final[1]: | |
if st.button("Up",use_container_width=True,key="btn_up_row_"+str(i),disabled=True if i == 0 else False): | |
if i > 0: | |
# Tausche das aktuelle Element mit dem vorherigen Element | |
st.session_state.final_question_string[i], st.session_state.final_question_string[i - 1] = \ | |
st.session_state.final_question_string[i - 1], st.session_state.final_question_string[i] | |
st.session_state.final_question_string = adjust_numbering(st.session_state.final_question_string) | |
st.rerun() | |
if st.button("Down",use_container_width=True,key="btn_down_row_"+str(i), disabled=True if i == len(st.session_state["final_question_string"])-1 else False): | |
if i < len(st.session_state.final_question_string) - 1: | |
# Tausche das aktuelle Element mit dem nächsten Element | |
st.session_state.final_question_string[i], st.session_state.final_question_string[i + 1] = \ | |
st.session_state.final_question_string[i + 1], st.session_state.final_question_string[i] | |
st.session_state.final_question_string = adjust_numbering(st.session_state.final_question_string) | |
st.rerun() | |
with cols_final[0]: | |
st.write(final_q) | |
if len(st.session_state["final_question_string"])>0: | |
st.text_input("Enter the email address to which the test emails should be sent:",key="recruiter_mail") | |
st.text_input("Enter the phone number to which the test SMS should be sent (With country code, e.g. +1 for the USA or +49 for Germany):",key="recruiter_phone") | |
st.text_input("Enter the job title:", key="job_title") | |
if st.button("Submit", use_container_width=True): | |
with st.spinner("Generation and dispatch of mails. This process may take a few minutes..."): | |
sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API')) | |
# Sender- und Empfänger-E-Mail-Adressen | |
sender_email = "[email protected]" | |
receiver_email = st.session_state["recruiter_mail"] | |
print(receiver_email) | |
subject = "Mails for potential candidates for the following position: "+st.session_state["job_title"] | |
message = f"""Dear Recruiter, | |
enclosed in the text file you will find the e-mails that are sent to the potential candidates. | |
The subject of the mail would be the following: Are you interested in a new position as a {st.session_state["job_title"]}? | |
Sincerely, | |
Your Candidate-Search-Tool | |
""" | |
# SendGrid-E-Mail erstellen | |
message = Mail( | |
from_email=sender_email, | |
to_emails=receiver_email, | |
subject=subject, | |
plain_text_content=message, | |
) | |
data = { | |
"title": st.session_state["job_title"], | |
"email": st.session_state["recruiter_mail"], | |
"question_one": "", | |
"question_two": "", | |
"question_three": "", | |
} | |
json_data = json.dumps(data, ensure_ascii=False) | |
# Eine zufällige UUID generieren | |
random_uuid = uuid.uuid4() | |
# Die UUID als String darstellen | |
uuid_string = str(random_uuid) | |
pdf_name = uuid_string | |
cvs_data = [] | |
temp_pdf_file = "candidate_pdf.pdf" | |
for candidate in st.session_state["final_candidates"]: | |
styles = getSampleStyleSheet() | |
pdf = SimpleDocTemplate(temp_pdf_file) | |
flowables = [Paragraph(candidate[0].page_content, styles['Normal'])] | |
pdf.build(flowables) | |
with open(temp_pdf_file, 'rb') as pdf_file: | |
bytes_data = pdf_file.read() | |
cvs_data.append(bytes_data) | |
os.remove(temp_pdf_file) | |
candidate_links = upload_blob(pdf_name, json_data, st.session_state["job"].read(),cvs_data,True,st.session_state["final_question_string"]) | |
mail_txt_string = "" | |
for i, candidate in enumerate(st.session_state["final_candidates"]): | |
if i > 0: | |
mail_txt_string += "\n\nMail to the "+str(i+1)+". candidate: "+candidate[0].metadata["name"]+" "+candidate[0].metadata["candidateId"]+" \n\n" | |
else: | |
mail_txt_string += "Mail to the "+str(i+1)+". candidate: "+candidate[0].metadata["name"]+" "+candidate[0].metadata["candidateId"]+" \n\n" | |
mail_txt_string += generate_candidate_mail(candidate,candidate_links[i]) | |
# Summary in eine TXT Datei schreiben | |
mail_txt_path = "mailattachment.txt" | |
with open(mail_txt_path, 'wb') as summary_file: | |
summary_file.write(mail_txt_string.encode('utf-8')) | |
# Resume als Anhang hinzufügen | |
with open(mail_txt_path, 'rb') as summary_file: | |
encode_file_summary = base64.b64encode(summary_file.read()).decode() | |
summary_attachment = Attachment() | |
summary_attachment.file_content = FileContent(encode_file_summary) | |
summary_attachment.file_name = FileName('candidate_mails.txt') | |
summary_attachment.file_type = FileType('text/plain') | |
summary_attachment.disposition = Disposition('attachment') | |
message.attachment = summary_attachment | |
try: | |
response = sg.send(message) | |
print("E-Mail wurde erfolgreich gesendet. Statuscode:", response.status_code) | |
os.remove("mailattachment.txt") | |
except Exception as e: | |
print("Fehler beim Senden der E-Mail:", str(e)) | |
st.error("Unfortunately the mail dispatch did not work. Please reload the page and try again or contact the administrator. ", icon="🚨") | |
try: | |
bullets = generate_job_bullets(st.session_state["job_string"]) | |
client = Client(os.getenv("TWILIO_SID"), os.getenv("TWILIO_API")) | |
message_body = f"Dear candidate,\n\nare you interested in the following position: \n"+st.session_state["job_title"]+"\n\n"+bullets+"\n\nThen please answer with 'yes'\n\nSincerely,\n"+"WorkGenius" | |
message = client.messages.create( | |
to=st.session_state["recruiter_phone"], | |
from_="+1 857 214 8753", | |
body=message_body | |
) | |
print(f"Message sent with SID: {message.sid}") | |
st.success('The dispatch and the upload of the data was successful') | |
except Exception as e: | |
st.error("Unfortunately the SMS dispatch did not work. Please reload the page and try again or contact the administrator. ", icon="🚨") | |
print("Fehler beim Senden der SMS:", str(e)) | |