File size: 41,338 Bytes
9a804ac
 
 
 
 
7a876be
 
 
 
 
 
 
9a804ac
 
 
 
7a876be
 
9a804ac
7a876be
 
 
83c1c84
7a876be
 
9a804ac
 
4705276
9a804ac
c88e120
9a804ac
7a876be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fae1d4
7a876be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a804ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fae1d4
 
9a804ac
 
 
 
7a876be
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a876be
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
19d76b7
7a876be
 
 
 
 
 
 
 
 
 
 
83c1c84
 
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83c1c84
9a804ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c44016
 
 
 
 
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d76b7
 
 
 
 
 
 
 
 
 
 
0fae1d4
 
 
19d76b7
 
 
 
 
 
 
 
 
 
c73cf52
 
 
 
 
 
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d76b7
0fae1d4
 
 
19d76b7
 
 
 
 
 
 
 
 
 
c73cf52
 
 
 
 
 
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a804ac
 
 
 
 
 
 
 
 
 
 
 
0fae1d4
 
 
 
 
 
9a804ac
11f7c51
9a804ac
 
 
7a876be
9a804ac
 
 
7a876be
9a804ac
 
 
 
 
 
 
 
 
 
e11009a
 
 
 
 
 
 
 
9a804ac
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d76b7
 
 
8c44016
7a876be
 
 
 
 
 
 
 
 
 
9a804ac
 
 
7a876be
 
 
 
 
0fae1d4
 
 
 
 
 
 
 
 
 
 
9a804ac
0fae1d4
9a804ac
 
 
 
 
 
 
 
 
 
 
8c44016
 
 
 
 
 
 
 
 
0fae1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a804ac
 
 
0fae1d4
9a804ac
 
0fae1d4
9a804ac
0fae1d4
9a804ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f94940
7a876be
83c1c84
7a876be
2f94940
7a876be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fae1d4
7a876be
 
 
 
 
 
 
 
 
0fae1d4
7a876be
0fae1d4
7a876be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83c1c84
 
 
19d76b7
83c1c84
 
 
 
 
 
 
 
 
 
8c44016
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
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))