mbosse99 commited on
Commit
f086d61
·
1 Parent(s): ef04faf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +712 -0
app.py ADDED
@@ -0,0 +1,712 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+ import openai
4
+ import re
5
+ import sqlite3
6
+ import base64
7
+ import calendar
8
+ import json
9
+ import time
10
+ import uuid
11
+ from reportlab.platypus import SimpleDocTemplate, Paragraph
12
+ from reportlab.lib.styles import getSampleStyleSheet
13
+ import streamlit as st
14
+ from streamlit_js_eval import streamlit_js_eval
15
+ from langchain.embeddings.openai import OpenAIEmbeddings
16
+ from langchain.vectorstores.azuresearch import AzureSearch
17
+ from azure.storage.blob import BlobServiceClient
18
+ from azure.cosmos import CosmosClient, exceptions
19
+ from PyPDF2 import PdfReader
20
+ import openai
21
+ import sendgrid
22
+ from sendgrid.helpers.mail import Mail, Attachment, FileContent, FileName, FileType, Disposition
23
+ from twilio.rest import Client
24
+ import ssl
25
+ ssl._create_default_https_context = ssl._create_unverified_context
26
+
27
+ openai.api_key = os.getenv("OPENAI_API_KEY")
28
+ openai.api_base = "https://tensora-oai.openai.azure.com/"
29
+ openai.api_type = "azure"
30
+ openai.api_version = "2023-05-15"
31
+
32
+ connection_string = os.getenv("CONNECTION")
33
+ blob_service_client = BlobServiceClient.from_connection_string(connection_string)
34
+
35
+ def upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs, pre_generated_bool, custom_questions):
36
+ try:
37
+ container_name = "jobdescriptions"
38
+ # json_blob_name = f"{pdf_name}_jsondata.json"
39
+ pdf_blob_name_jobdescription = f"{pdf_name}.pdf"
40
+
41
+ container_client = blob_service_client.get_container_client(container_name)
42
+
43
+ # json_blob_client = container_client.get_blob_client(json_blob_name)
44
+ # json_blob_client.upload_blob(json_data.encode('utf-8'), overwrite=True)
45
+
46
+ pdf_blob_client = container_client.get_blob_client(pdf_blob_name_jobdescription)
47
+ pdf_blob_client.upload_blob(pdf_data_jobdescription, overwrite=True)
48
+
49
+ upload_job_db_item(pdf_name,len(pdf_data_cvs),json.loads(json_data),pre_generated_bool, custom_questions)
50
+ if pre_generated_bool:
51
+ for i,question in enumerate(custom_questions):
52
+ question_nr_for_id = i+1
53
+ question_id = pdf_name + "-question-nr-" + str(question_nr_for_id)+str(calendar.timegm(time.gmtime()))
54
+ upload_question_db_item(question_id, pdf_name, question,st.session_state["job_string"])
55
+ links = []
56
+ names = []
57
+ for i,cv in enumerate(pdf_data_cvs):
58
+
59
+ cv_nr_for_id = i+1
60
+ cv_session_state_string = "cv-"+str(cv_nr_for_id)
61
+ session_state_name = st.session_state["final_candidates"][i][0].metadata["name"]
62
+ names.append(session_state_name)
63
+ cv_id = pdf_name + "-cv-nr-" + str(cv_nr_for_id)+str(calendar.timegm(time.gmtime()))
64
+ upload_db_item(session_state_name, json.loads(json_data), pdf_name, cv_id)
65
+ pdf_blob_name_cv = f"{cv_id}.pdf"
66
+ pdf_blob_client = container_client.get_blob_client(pdf_blob_name_cv)
67
+ pdf_blob_client.upload_blob(pdf_data_cvs[i], overwrite=True)
68
+ links.append("https://tensora.ai/workgenius/cv-evaluation2/?job="+cv_id)
69
+
70
+ return links
71
+ except Exception as e:
72
+ print(f"Fehler beim Hochladen der Daten: {str(e)}")
73
+ return []
74
+
75
+ def upload_job_db_item(id, number_of_applicants, data, pre_generated_bool, custom_questions):
76
+ endpoint = "https://wg-candidate-data.documents.azure.com:443/"
77
+ key = os.getenv("CONNECTION_DB")
78
+ client = CosmosClient(endpoint, key)
79
+ database = client.get_database_client("ToDoList")
80
+ container = database.get_container_client("JobData")
81
+ job_item = {
82
+ "id": id,
83
+ 'partitionKey' : 'wg-job-data-v1',
84
+ "title": data["title"],
85
+ "number_of_applicants": number_of_applicants,
86
+ "every_interview_conducted": False,
87
+ "evaluation_email": data["email"],
88
+ "question_one": data["question_one"],
89
+ "question_two": data["question_two"],
90
+ "question_three": data["question_three"],
91
+ "pre_generated": pre_generated_bool,
92
+ "custom_questions": custom_questions
93
+ }
94
+ try:
95
+ # Fügen Sie das Element in den Container ein
96
+ container.create_item(body=job_item)
97
+ print("Eintrag erfolgreich in die Cosmos DB eingefügt. Container: Job Data")
98
+ except exceptions.CosmosHttpResponseError as e:
99
+ print(f"Fehler beim Schreiben in die Cosmos DB: {str(e)}")
100
+ except Exception as e:
101
+ print(f"Allgemeiner Fehler: {str(e)}")
102
+
103
+ def upload_db_item(name, data, job_description_id, cv_id):
104
+
105
+ endpoint = "https://wg-candidate-data.documents.azure.com:443/"
106
+ key = os.getenv("CONNECTION_DB")
107
+ client = CosmosClient(endpoint, key)
108
+ database = client.get_database_client("ToDoList")
109
+ container = database.get_container_client("Items")
110
+ candidate_item = {
111
+ "id": cv_id,
112
+ 'partitionKey' : 'wg-candidate-data-v1',
113
+ "name": name,
114
+ "title": data["title"],
115
+ "interview_conducted": False,
116
+ "ai_summary": "",
117
+ "evaluation_email": data["email"],
118
+ "question_one": data["question_one"],
119
+ "question_two": data["question_two"],
120
+ "question_three": data["question_three"],
121
+ "job_description_id": job_description_id,
122
+ }
123
+
124
+ try:
125
+ # Fügen Sie das Element in den Container ein
126
+ container.create_item(body=candidate_item)
127
+ print("Eintrag erfolgreich in die Cosmos DB eingefügt. Container: Items(candidate Data)")
128
+ except exceptions.CosmosHttpResponseError as e:
129
+ print(f"Fehler beim Schreiben in die Cosmos DB: {str(e)}")
130
+ except Exception as e:
131
+ print(f"Allgemeiner Fehler: {str(e)}")
132
+
133
+ def upload_question_db_item(id, job_id, question, job_content):
134
+ endpoint = "https://wg-candidate-data.documents.azure.com:443/"
135
+ key = os.getenv("CONNECTION_DB")
136
+ client = CosmosClient(endpoint, key)
137
+ database = client.get_database_client("ToDoList")
138
+ container = database.get_container_client("Questions")
139
+ question_item = {
140
+ "id": id,
141
+ "partitionKey" : "wg-question-data-v1",
142
+ "job_id": job_id,
143
+ "question_content": question,
144
+ "job_description": job_content,
145
+ }
146
+ try:
147
+ # Fügen Sie das Element in den Container ein
148
+ container.create_item(body=question_item)
149
+ print("Eintrag erfolgreich in die Cosmos DB eingefügt. Container: Questions(Question Data)")
150
+ except exceptions.CosmosHttpResponseError as e:
151
+ print(f"Fehler beim Schreiben in die Cosmos DB: {str(e)}")
152
+ except Exception as e:
153
+ print(f"Allgemeiner Fehler: {str(e)}")
154
+
155
+ st.markdown(
156
+ """
157
+ <style>
158
+ [data-testid=column]{
159
+ text-align: center;
160
+ display: flex;
161
+ align-items: center;
162
+ justify-content: center;
163
+ }
164
+ h3{
165
+ text-align: left;
166
+ }
167
+ </style>
168
+ """,
169
+ unsafe_allow_html=True,
170
+ )
171
+
172
+ with open("sys_prompt_frontend.txt") as f:
173
+ sys_prompt = f.read()
174
+ with open("sys_prompt_job_optimization.txt") as j:
175
+ sys_prompt_optimization = j.read()
176
+
177
+ def adjust_numbering(lst):
178
+ return [f"{i + 1}. {item.split('. ', 1)[1]}" for i, item in enumerate(lst)]
179
+
180
+ def generate_candidate_mail(candidate, chat_link)-> str:
181
+ candidate_first_name = candidate[0].metadata["name"].split(" ")[0]
182
+ 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."
183
+ try:
184
+ res = openai.ChatCompletion.create(
185
+ engine="gpt-4",
186
+ temperature=0.2,
187
+ messages=[
188
+ {
189
+ "role": "system",
190
+ "content": prompt,
191
+ },
192
+ {"role": "system", "content": "Job description: "+st.session_state["job_string"]+"; Resume: "+candidate[0].page_content}
193
+ ],
194
+ )
195
+ # print(res.choices[0]["message"]["content"])
196
+ except Exception as e:
197
+ # Iterativ die Anfrage wiederholen und 200 Chars von hinten vom Resume weglassen
198
+ max_retries = 5
199
+ retries = 0
200
+ while retries < max_retries:
201
+ try:
202
+ # Reduziere die Länge des Resume um 200 Chars von hinten
203
+ candidate[0].page_content = candidate[0].page_content[:-200]
204
+
205
+ # Neue Anfrage senden
206
+ res = openai.ChatCompletion.create(
207
+ engine="gpt-4",
208
+ temperature=0.2,
209
+ messages=[
210
+ {
211
+ "role": "system",
212
+ "content": prompt,
213
+ },
214
+ {"role": "system", "content": "Job description: " + st.session_state["job_string"] + "; Resume: " + candidate[0].page_content}
215
+ ],
216
+ )
217
+ # print(res.choices[0]["message"]["content"])
218
+
219
+ # Wenn die Anfrage erfolgreich ist, den Schleifen-Iterator beenden
220
+ break
221
+
222
+ except Exception as e:
223
+ # Bei erneuter Ausnahme die Schleife fortsetzen
224
+ retries += 1
225
+ if retries == max_retries:
226
+ # Falls die maximale Anzahl von Wiederholungen erreicht ist, handle die Ausnahme entsprechend
227
+ print("Max retries reached. Unable to get a valid response.")
228
+ return "The CV was too long to generate a Mail"
229
+ # Hier kannst du zusätzlichen Code für den Fall implementieren, dass die maximale Anzahl von Wiederholungen erreicht wurde.
230
+
231
+ # Optional: Füge eine Wartezeit zwischen den Anfragen hinzu, um API-Beschränkungen zu respektieren
232
+ time.sleep(1)
233
+
234
+ output_string = f"""{res.choices[0]["message"]["content"]}
235
+
236
+ We have added the job description to the mail attachment.
237
+ 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.
238
+
239
+ Link to the interview chatbot: {chat_link}
240
+
241
+ Sincerely,
242
+ WorkGenius
243
+ """
244
+ print("Mail generated")
245
+ return output_string
246
+
247
+ def generate_job_bullets(job)->str:
248
+ 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."
249
+ try:
250
+ res = openai.ChatCompletion.create(
251
+ engine="gpt-4",
252
+ temperature=0.2,
253
+ messages=[
254
+ {
255
+ "role": "system",
256
+ "content": prompt,
257
+ },
258
+ {"role": "system", "content": "Job description: "+job}
259
+ ],
260
+ )
261
+ # print(res.choices[0]["message"]["content"])
262
+ output_string = f"""{res.choices[0]["message"]["content"]}"""
263
+ # print(output_string)
264
+ return output_string
265
+ except Exception as e:
266
+ print(f"Fehler beim generieren der Bullets: {str(e)}")
267
+
268
+ def check_keywords_in_content(database_path, table_name, input_id, keywords):
269
+ # Verbindung zur Datenbank herstellen
270
+ conn = sqlite3.connect(database_path)
271
+ cursor = conn.cursor()
272
+
273
+ # SQL-Abfrage, um die Zeile mit der angegebenen ID abzurufen
274
+ cursor.execute(f'SELECT * FROM {table_name} WHERE id = ?', (input_id,))
275
+
276
+ # Ergebnis abrufen
277
+ row = cursor.fetchone()
278
+
279
+ # Wenn die Zeile nicht gefunden wurde, False zurückgeben
280
+ if not row:
281
+ conn.close()
282
+ print("ID not found")
283
+ return False
284
+
285
+ # Überprüfen, ob die Keywords in der Spalte content enthalten sind (case-insensitive)
286
+ content = row[1].lower() # Annahme: content ist die zweite Spalte, und wir wandeln ihn in Kleinbuchstaben um
287
+ keywords_lower = [keyword.lower() for keyword in keywords]
288
+
289
+ contains_keywords = all(keyword in content for keyword in keywords_lower)
290
+
291
+ # Verbindung schließen
292
+ conn.close()
293
+
294
+ return contains_keywords
295
+
296
+ def load_candidates(fillup):
297
+ with st.spinner("Load the candidates, this may take a moment..."):
298
+ # print(st.session_state["job_string"])
299
+ filter_string = ""
300
+ query_string = "The following keywords must be included: " + text_area_params + " " + st.session_state["job_string"]
301
+ checked_candidates = []
302
+ db_path = 'cvdb.db'
303
+ table_name = 'files'
304
+ candidates_per_search = 100
305
+ target_candidates_count = 10
306
+ current_offset = 0
307
+
308
+ if st.session_state["screened"]:
309
+ filter_string = "amount_screenings gt 0 "
310
+ if st.session_state["handed"]:
311
+ if len(filter_string) > 0:
312
+ filter_string += "and amount_handoffs gt 0 "
313
+ else:
314
+ filter_string += "amount_handoffs gt 0 "
315
+ if st.session_state["placed"]:
316
+ if len(filter_string) > 0:
317
+ filter_string += "and amount_placed gt 0"
318
+ else:
319
+ filter_string += "amount_placed gt 0"
320
+ # print(filter_string)
321
+ if not fillup:
322
+ while len(checked_candidates) < target_candidates_count:
323
+ # Führe eine similarity search durch und erhalte 100 Kandidaten
324
+ if st.session_state["search_type"]:
325
+ print("hybrid")
326
+ # raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string)
327
+ raw_candidates = st.session_state["db"].hybrid_search_with_score(query_string, k=candidates_per_search+current_offset, filters=filter_string)
328
+ else:
329
+ print("similarity")
330
+ # raw_candidates = st.session_state["db"].similarity_search(query_string, k=candidates_per_search+current_offset, filters=filter_string)
331
+ raw_candidates = st.session_state["db"].similarity_search_with_relevance_scores(query_string, k=candidates_per_search+current_offset, filters=filter_string)
332
+
333
+ for candidate in raw_candidates[current_offset:]:
334
+ candidates_id = candidate[0].metadata["source"].split("/")[-1]
335
+ keyword_bool = check_keywords_in_content(db_path, table_name, candidates_id, text_area_params.split(','))
336
+
337
+ if keyword_bool:
338
+ checked_candidates.append(candidate)
339
+
340
+ # Überprüfe, ob die Zielanzahl erreicht wurde und breche die Schleife ab, wenn ja
341
+ if len(checked_candidates) >= target_candidates_count:
342
+ break
343
+
344
+ current_offset += candidates_per_search
345
+ if current_offset == 600:
346
+ break
347
+ # Setze die Ergebnisse in der Session State Variable
348
+ st.session_state["docs_res"] = checked_candidates
349
+ st.session_state["candidate_offset"] = current_offset
350
+ if len(checked_candidates) == 0:
351
+ st.error("No candidates can be found with these keywords. Please adjust the keywords and try again.", icon="🚨")
352
+ else:
353
+ # Setze die Zielanzahl auf 10
354
+ target_candidates_count = 10
355
+
356
+ current_offset = st.session_state["candidate_offset"]
357
+
358
+ # Solange die Anzahl der überprüften Kandidaten kleiner als die Zielanzahl ist
359
+ while len(st.session_state["docs_res"]) < target_candidates_count:
360
+ # Führe eine similarity search durch und erhalte 100 Kandidaten
361
+ if st.session_state["search_type"]:
362
+ print("hybrid")
363
+ # raw_candidates = st.session_state["db"].hybrid_search(query_string, k=candidates_per_search+current_offset, filters=filter_string)
364
+ raw_candidates = st.session_state["db"].hybrid_search_with_score(query_string, k=candidates_per_search+current_offset, filters=filter_string)
365
+ else:
366
+ print("similarity")
367
+ # raw_candidates = st.session_state["db"].similarity_search(query_string, k=candidates_per_search+current_offset, filters=filter_string)
368
+ raw_candidates = st.session_state["db"].similarity_search_with_relevance_scores(query_string, k=candidates_per_search+current_offset, filters=filter_string)
369
+ temp_offset_add = 0
370
+ for candidate in raw_candidates[current_offset:]:
371
+ candidates_id = candidate[0].metadata["source"].split("/")[-1]
372
+ keyword_bool = check_keywords_in_content(db_path, table_name, candidates_id, text_area_params.split(','))
373
+
374
+ if keyword_bool:
375
+ st.session_state["docs_res"].append(candidate)
376
+ temp_offset_add += 1
377
+ # Überprüfe, ob die Zielanzahl erreicht wurde und breche die Schleife ab, wenn ja
378
+ if len(st.session_state["docs_res"]) >= target_candidates_count:
379
+ st.session_state["candidate_offset"] = current_offset+temp_offset_add
380
+ break
381
+
382
+ current_offset += candidates_per_search
383
+ if current_offset == 900:
384
+ break
385
+
386
+ # Wenn die Liste immer noch leer ist, zeige eine Fehlermeldung an
387
+ if len(st.session_state["docs_res"]) == 0:
388
+ st.warning("No more candidates can be found.", icon="🔥")
389
+
390
+ if "similarity_search_string" not in st.session_state:
391
+ st.session_state["similarity_search_string"] = None
392
+ if "job_string" not in st.session_state:
393
+ st.session_state["job_string"] = None
394
+ if "docs_res" not in st.session_state:
395
+ st.session_state["docs_res"] = None
396
+ if "final_candidates" not in st.session_state:
397
+ st.session_state["final_candidates"] = None
398
+ if "final_question_string" not in st.session_state:
399
+ st.session_state["final_question_string"] = []
400
+ if "ai_questions" not in st.session_state:
401
+ st.session_state["ai_questions"] = None
402
+ if "raw_job" not in st.session_state:
403
+ st.session_state["raw_job"] = None
404
+ if "optimized_job" not in st.session_state:
405
+ st.session_state["optimized_job"] = None
406
+ if "candidate_offset" not in st.session_state:
407
+ st.session_state["candidate_offset"] = 0
408
+ if "db" not in st.session_state:
409
+ embedder = OpenAIEmbeddings(deployment="text-embedding-ada-002", chunk_size=1)
410
+ embedding_function = embedder.embed_query
411
+
412
+ db = AzureSearch(
413
+ index_name="wg-cvs-data",
414
+ azure_search_endpoint=os.environ.get("AZURE_SEARCH_ENDPOINT"),
415
+ azure_search_key=os.environ.get("AZURE_SEARCH_KEY"),
416
+ embedding_function=embedding_function,
417
+ # fields=fields
418
+ )
419
+ st.session_state["db"] = db
420
+
421
+
422
+ col1, col2 = st.columns([2, 1])
423
+
424
+ col1.title("Candidate Search")
425
+ col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg")
426
+
427
+ st.write("Please upload the job description for which you would like candidates to be proposed.")
428
+ col_file, col_clear = st.columns([6,1])
429
+
430
+ with col_file:
431
+ uploaded_file_jobdescription = st.file_uploader("Upload the job description:", type=["pdf"], key="job")
432
+ with col_clear:
433
+ if st.button("Clear", use_container_width=True):
434
+ streamlit_js_eval(js_expressions="parent.window.location.reload()")
435
+
436
+ if st.session_state["job"]:
437
+ if not st.session_state["job_string"]:
438
+ if not st.session_state["optimized_job"]:
439
+ with st.spinner("Optimizing the job description. This may take a moment..."):
440
+ pdf_data_jobdescription = st.session_state["job"].read()
441
+ pdf_data_jobdescription_string = ""
442
+ pdf_reader_job = PdfReader(io.BytesIO(pdf_data_jobdescription))
443
+ for page_num in range(len(pdf_reader_job.pages)):
444
+ page = pdf_reader_job.pages[page_num]
445
+ pdf_data_jobdescription_string += page.extract_text()
446
+ # st.session_state["pdf_data_jobdescription"] = pdf_data_jobdescription activate and add sessio state if data is needed
447
+ system_prompt_job = sys_prompt_optimization.format(job=pdf_data_jobdescription_string)
448
+ try:
449
+ res = openai.ChatCompletion.create(
450
+ engine="gpt-4",
451
+ temperature=0.2,
452
+ messages=[
453
+ {
454
+ "role": "system",
455
+ "content": system_prompt_job,
456
+ },
457
+ ],
458
+ )
459
+ # print(res.choices[0]["message"]["content"])
460
+ output_string = f"""{res.choices[0]["message"]["content"]}"""
461
+ st.session_state["optimized_job"] = output_string
462
+ st.rerun()
463
+ except Exception as e:
464
+ print(f"Fehler beim generieren der optimierten JD: {str(e)}")
465
+ st.error("An error has occurred. Please reload the page or contact the admin.", icon="🚨")
466
+ # st.session_state["job_string"] = pdf_data_jobdescription_string
467
+ # print(output_string)
468
+ 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")
469
+ if st.button("Accept the job description"):
470
+ st.session_state["job_string"] = st.session_state["optimized_job_edited"]
471
+ st.rerun()
472
+
473
+ st.write("Switch from a similarity search (default) to a hybrid search (activated)")
474
+ st.toggle("Switch Search", key="search_type")
475
+
476
+ st.write("Activate the following toggles to filter according to the respective properties:")
477
+ col_screening, col_handoff, col_placed = st.columns([1,1,1])
478
+ with col_screening:
479
+ st.toggle("Screened", key="screened")
480
+ with col_handoff:
481
+ st.toggle("Handed over", key="handed")
482
+ with col_placed:
483
+ st.toggle("Placed", key="placed")
484
+
485
+ text_area_params = st.text_area(label="Add additional search parameters, which are separated by commas (e.g. master, phd, web developer, spanish)")
486
+
487
+ submit = st.button("Search candidates",disabled= True if st.session_state["final_candidates"] else False)
488
+
489
+
490
+ if not st.session_state["job"] and submit:
491
+ st.error("Please upload a job description to search for candidates")
492
+ if st.session_state["docs_res"] and submit:
493
+ load_candidates(False)
494
+ if (st.session_state["job_string"] and submit) or st.session_state["docs_res"]:
495
+ # if not st.session_state["job_string"]:
496
+ # pdf_data_jobdescription = st.session_state["job"].read()
497
+ # pdf_data_jobdescription_string = ""
498
+ # pdf_reader_job = PdfReader(io.BytesIO(pdf_data_jobdescription))
499
+ # for page_num in range(len(pdf_reader_job.pages)):
500
+ # page = pdf_reader_job.pages[page_num]
501
+ # pdf_data_jobdescription_string += page.extract_text()
502
+ # # st.session_state["pdf_data_jobdescription"] = pdf_data_jobdescription activate and add sessio state if data is needed
503
+ # st.session_state["job_string"] = pdf_data_jobdescription_string
504
+ if not st.session_state["docs_res"]:
505
+ load_candidates(False)
506
+ if not st.session_state["final_candidates"]:
507
+ for i,doc in enumerate(st.session_state["docs_res"]):
508
+ # print(doc)
509
+ cols_final = st.columns([6,1])
510
+ with cols_final[1]:
511
+ if st.button("Remove",use_container_width=True,key="btn_rm_cv_row_"+str(i)):
512
+ # st.write(doc.page_content)
513
+ st.session_state["docs_res"].pop(i)
514
+ st.rerun()
515
+ with cols_final[0]:
516
+ # st.subheader(doc.metadata["source"])
517
+ with st.expander(doc[0].metadata["name"]+" with a search score of: "+str(round(doc[1] * 100, 3))+"%"):
518
+ st.write(doc[0].page_content)
519
+ if len(st.session_state["docs_res"])>=10:
520
+ if st.button("Accept candidates", key="accept_candidates_btn"):
521
+ print("hello")
522
+ st.session_state["final_candidates"] = st.session_state["docs_res"].copy()
523
+ st.rerun()
524
+ else:
525
+ col_accept, col_empty ,col_load_new = st.columns([2, 3, 2])
526
+ with col_accept:
527
+ if st.button("Accept candidates", key="accept_candidates_btn"):
528
+ print("hello")
529
+ st.session_state["final_candidates"] = st.session_state["docs_res"].copy()
530
+ st.rerun()
531
+ with col_load_new:
532
+ if st.button("Load new candidates", key="load_new_candidates"):
533
+ print("loading new candidates")
534
+ load_candidates(True)
535
+ st.rerun()
536
+ else:
537
+ print("Now Questions")
538
+ st.subheader("Your Candidates:")
539
+ st.write(", ".join(candidate[0].metadata["name"] for candidate in st.session_state["final_candidates"]))
540
+ # for i,candidate in enumerate(st.session_state["final_candidates"]):
541
+ # st.write(candidate.metadata["source"])
542
+ cv_strings = "; Next CV: ".join(candidate[0].page_content for candidate in st.session_state["final_candidates"])
543
+ # print(len(cv_strings))
544
+ system = sys_prompt.format(job=st.session_state["job_string"], resume=st.session_state["final_candidates"][0][0].page_content, n=15)
545
+ if not st.session_state["ai_questions"]:
546
+ try:
547
+ # st.write("The questions are generated. This may take a short moment...")
548
+ st.info("The questions are generated. This may take a short moment.", icon="ℹ️")
549
+ with st.spinner("Loading..."):
550
+ res = openai.ChatCompletion.create(
551
+ engine="gpt-4",
552
+ temperature=0.2,
553
+ messages=[
554
+ {
555
+ "role": "system",
556
+ "content": system,
557
+ },
558
+ ],
559
+ )
560
+ st.session_state["ai_questions"] = [item for item in res.choices[0]["message"]["content"].split("\n") if len(item) > 0]
561
+ for i,q in enumerate(res.choices[0]["message"]["content"].split("\n")):
562
+ st.session_state["disable_row_"+str(i)] = False
563
+ st.rerun()
564
+ except Exception as e:
565
+ print(f"Fehler beim generieren der Fragen: {str(e)}")
566
+ st.error("An error has occurred. Please reload the page or contact the admin.", icon="🚨")
567
+ else:
568
+ if len(st.session_state["final_question_string"]) <= 0:
569
+ for i,question in enumerate(st.session_state["ai_questions"]):
570
+ cols = st.columns([5,1])
571
+ with cols[1]:
572
+ # if st.button("Accept",use_container_width=True,key="btn_accept_row_"+str(i)):
573
+ # print("accept")
574
+ # pattern = re.compile(r"^[1-9][0-9]?\.")
575
+ # questions_length = len(st.session_state["final_question_string"])
576
+ # question_from_text_area = st.session_state["text_area_"+str(i)]
577
+ # question_to_append = str(questions_length+1)+"."+re.sub(pattern, "", question_from_text_area)
578
+ # st.session_state["final_question_string"].append(question_to_append)
579
+ # st.session_state["disable_row_"+str(i)] = True
580
+ # st.rerun()
581
+ if st.button("Delete",use_container_width=True,key="btn_del_row_"+str(i)):
582
+ print("delete")
583
+ st.session_state["ai_questions"].remove(question)
584
+ st.rerun()
585
+ with cols[0]:
586
+ 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)])
587
+ st.write("If you are satisfied with the questions, then accept them. You can still sort them afterwards.")
588
+ if st.button("Accept all questions",use_container_width=True,key="accept_all_questions"):
589
+ for i,question in enumerate(st.session_state["ai_questions"]):
590
+ pattern = re.compile(r"^[1-9][0-9]?\.")
591
+ questions_length = len(st.session_state["final_question_string"])
592
+ question_from_text_area = st.session_state["text_area_"+str(i)]
593
+ question_to_append = str(questions_length+1)+"."+re.sub(pattern, "", question_from_text_area)
594
+ st.session_state["final_question_string"].append(question_to_append)
595
+ st.session_state["disable_row_"+str(i)] = True
596
+ st.rerun()
597
+ for i,final_q in enumerate(st.session_state["final_question_string"]):
598
+ cols_final = st.columns([5,1])
599
+ with cols_final[1]:
600
+ if st.button("Up",use_container_width=True,key="btn_up_row_"+str(i),disabled=True if i == 0 else False):
601
+ if i > 0:
602
+ # Tausche das aktuelle Element mit dem vorherigen Element
603
+ st.session_state.final_question_string[i], st.session_state.final_question_string[i - 1] = \
604
+ st.session_state.final_question_string[i - 1], st.session_state.final_question_string[i]
605
+ st.session_state.final_question_string = adjust_numbering(st.session_state.final_question_string)
606
+ st.rerun()
607
+ 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):
608
+ if i < len(st.session_state.final_question_string) - 1:
609
+ # Tausche das aktuelle Element mit dem nächsten Element
610
+ st.session_state.final_question_string[i], st.session_state.final_question_string[i + 1] = \
611
+ st.session_state.final_question_string[i + 1], st.session_state.final_question_string[i]
612
+ st.session_state.final_question_string = adjust_numbering(st.session_state.final_question_string)
613
+ st.rerun()
614
+ with cols_final[0]:
615
+ st.write(final_q)
616
+ if len(st.session_state["final_question_string"])>0:
617
+ st.text_input("Enter the email address to which the test emails should be sent:",key="recruiter_mail")
618
+ 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")
619
+ st.text_input("Enter the job title:", key="job_title")
620
+ if st.button("Submit", use_container_width=True):
621
+ with st.spinner("Generation and dispatch of mails. This process may take a few minutes..."):
622
+ sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API'))
623
+ # Sender- und Empfänger-E-Mail-Adressen
624
+ sender_email = "workgeniusjobevaluation@gmail.com"
625
+ receiver_email = st.session_state["recruiter_mail"]
626
+ print(receiver_email)
627
+ subject = "Mails for potential candidates for the following position: "+st.session_state["job_title"]
628
+ message = f"""Dear Recruiter,
629
+
630
+ enclosed in the text file you will find the e-mails that are sent to the potential candidates.
631
+
632
+ The subject of the mail would be the following: Are you interested in a new position as a {st.session_state["job_title"]}?
633
+
634
+ Sincerely,
635
+ Your Candidate-Search-Tool
636
+ """
637
+ # SendGrid-E-Mail erstellen
638
+ message = Mail(
639
+ from_email=sender_email,
640
+ to_emails=receiver_email,
641
+ subject=subject,
642
+ plain_text_content=message,
643
+ )
644
+ data = {
645
+ "title": st.session_state["job_title"],
646
+ "email": st.session_state["recruiter_mail"],
647
+ "question_one": "",
648
+ "question_two": "",
649
+ "question_three": "",
650
+ }
651
+ json_data = json.dumps(data, ensure_ascii=False)
652
+ # Eine zufällige UUID generieren
653
+ random_uuid = uuid.uuid4()
654
+
655
+ # Die UUID als String darstellen
656
+ uuid_string = str(random_uuid)
657
+
658
+ pdf_name = uuid_string
659
+ cvs_data = []
660
+ temp_pdf_file = "candidate_pdf.pdf"
661
+ for candidate in st.session_state["final_candidates"]:
662
+ styles = getSampleStyleSheet()
663
+ pdf = SimpleDocTemplate(temp_pdf_file)
664
+ flowables = [Paragraph(candidate[0].page_content, styles['Normal'])]
665
+ pdf.build(flowables)
666
+ with open(temp_pdf_file, 'rb') as pdf_file:
667
+ bytes_data = pdf_file.read()
668
+ cvs_data.append(bytes_data)
669
+ os.remove(temp_pdf_file)
670
+ candidate_links = upload_blob(pdf_name, json_data, st.session_state["job"].read(),cvs_data,True,st.session_state["final_question_string"])
671
+ mail_txt_string = ""
672
+ for i, candidate in enumerate(st.session_state["final_candidates"]):
673
+ if i > 0:
674
+ mail_txt_string += "\n\nMail to the "+str(i+1)+". candidate: "+candidate[0].metadata["name"]+" "+candidate[0].metadata["candidateId"]+" \n\n"
675
+ else:
676
+ mail_txt_string += "Mail to the "+str(i+1)+". candidate: "+candidate[0].metadata["name"]+" "+candidate[0].metadata["candidateId"]+" \n\n"
677
+ mail_txt_string += generate_candidate_mail(candidate,candidate_links[i])
678
+ # Summary in eine TXT Datei schreiben
679
+ mail_txt_path = "mailattachment.txt"
680
+ with open(mail_txt_path, 'wb') as summary_file:
681
+ summary_file.write(mail_txt_string.encode('utf-8'))
682
+ # Resume als Anhang hinzufügen
683
+ with open(mail_txt_path, 'rb') as summary_file:
684
+ encode_file_summary = base64.b64encode(summary_file.read()).decode()
685
+ summary_attachment = Attachment()
686
+ summary_attachment.file_content = FileContent(encode_file_summary)
687
+ summary_attachment.file_name = FileName('candidate_mails.txt')
688
+ summary_attachment.file_type = FileType('text/plain')
689
+ summary_attachment.disposition = Disposition('attachment')
690
+ message.attachment = summary_attachment
691
+ try:
692
+ response = sg.send(message)
693
+ print("E-Mail wurde erfolgreich gesendet. Statuscode:", response.status_code)
694
+ os.remove("mailattachment.txt")
695
+ except Exception as e:
696
+ print("Fehler beim Senden der E-Mail:", str(e))
697
+ st.error("Unfortunately the mail dispatch did not work. Please reload the page and try again or contact the administrator. ", icon="🚨")
698
+ try:
699
+ bullets = generate_job_bullets(st.session_state["job_string"])
700
+ client = Client(os.getenv("TWILIO_SID"), os.getenv("TWILIO_API"))
701
+ message_body = f"Dear candidate,\n\nare you interested in the following position: \n\n"+st.session_state["job_title"]+"\n"+bullets+"\n\nThen please answer with 'yes'\n\nSincerely,\n"+"WorkGenius"
702
+ message = client.messages.create(
703
+ to=st.session_state["recruiter_phone"],
704
+ from_="+1 857 214 8753",
705
+ body=message_body
706
+ )
707
+
708
+ print(f"Message sent with SID: {message.sid}")
709
+ st.success('The dispatch and the upload of the data was successful')
710
+ except Exception as e:
711
+ st.error("Unfortunately the SMS dispatch did not work. Please reload the page and try again or contact the administrator. ", icon="🚨")
712
+ print("Fehler beim Senden der SMS:", str(e))