Spaces:
Running
Running
update with pre generating questions
Browse files
app.py
CHANGED
|
@@ -2,17 +2,21 @@ import streamlit as st
|
|
| 2 |
from streamlit_js_eval import streamlit_js_eval
|
| 3 |
from azure.storage.blob import BlobServiceClient
|
| 4 |
from azure.cosmos import CosmosClient, exceptions
|
|
|
|
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
import time
|
| 9 |
import calendar
|
|
|
|
| 10 |
|
| 11 |
connection_string = os.getenv("CONNECTION")
|
| 12 |
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
|
| 13 |
|
| 14 |
|
| 15 |
-
def upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs):
|
| 16 |
try:
|
| 17 |
container_name = "jobdescriptions"
|
| 18 |
# json_blob_name = f"{pdf_name}_jsondata.json"
|
|
@@ -26,7 +30,7 @@ def upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs):
|
|
| 26 |
pdf_blob_client = container_client.get_blob_client(pdf_blob_name_jobdescription)
|
| 27 |
pdf_blob_client.upload_blob(pdf_data_jobdescription, overwrite=True)
|
| 28 |
|
| 29 |
-
upload_job_db_item(pdf_name,len(pdf_data_cvs),json.loads(json_data))
|
| 30 |
|
| 31 |
links = []
|
| 32 |
names = []
|
|
@@ -53,7 +57,7 @@ def upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs):
|
|
| 53 |
print(f"Fehler beim Hochladen der Daten: {str(e)}")
|
| 54 |
return False
|
| 55 |
|
| 56 |
-
def upload_job_db_item(id, number_of_applicants, data):
|
| 57 |
endpoint = "https://wg-candidate-data.documents.azure.com:443/"
|
| 58 |
key = os.getenv("CONNECTION_DB")
|
| 59 |
client = CosmosClient(endpoint, key)
|
|
@@ -69,6 +73,8 @@ def upload_job_db_item(id, number_of_applicants, data):
|
|
| 69 |
"question_one": data["question_one"],
|
| 70 |
"question_two": data["question_two"],
|
| 71 |
"question_three": data["question_three"],
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
try:
|
| 74 |
# Fügen Sie das Element in den Container ein
|
|
@@ -109,13 +115,6 @@ def upload_db_item(name, data, job_description_id, cv_id):
|
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Allgemeiner Fehler: {str(e)}")
|
| 111 |
|
| 112 |
-
# def clear_states():
|
| 113 |
-
# if len(st.session_state.title) > 0 and len(st.session_state.mail) > 0 and st.session_state.job and len(st.session_state.cvs)>0:
|
| 114 |
-
# st.session_state.title = ""
|
| 115 |
-
# st.session_state.mail = ""
|
| 116 |
-
# # st.session_state.job = None
|
| 117 |
-
# st.session_state.cvs = []
|
| 118 |
-
|
| 119 |
st.markdown(
|
| 120 |
"""
|
| 121 |
<style>
|
|
@@ -131,26 +130,122 @@ st.markdown(
|
|
| 131 |
)
|
| 132 |
col1, col2 = st.columns([2, 1])
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
col1.title("Job description upload")
|
| 136 |
col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg")
|
| 137 |
|
| 138 |
st.write("Please upload the job description and resume(s) as PDF and enter the job title for the position. To receive the evaluation of the potential candidate(s), please provide your email address.")
|
| 139 |
upload_success = True
|
|
|
|
|
|
|
| 140 |
with st.container():
|
|
|
|
|
|
|
| 141 |
uploaded_file_jobdescription = st.file_uploader("Upload the job description:", type=["pdf"], key="job")
|
| 142 |
job_title = st.text_input("Enter the job title:", key="title")
|
| 143 |
email = st.text_input("Enter the email:" , key="mail")
|
| 144 |
uploaded_file_cvs = st.file_uploader("Upload the resume(s):", type=["pdf"],accept_multiple_files=True, key="cvs")
|
| 145 |
for i,cv in enumerate(st.session_state["cvs"]):
|
| 146 |
st.text_input(label="Enter the name of the "+str(i+1)+". CV (File: "+cv.name+")", value=cv.name,key="cv-"+str(i+1))
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
col_submit_btn, col_empty, col_clear_btn = st.columns([1,4, 1])
|
| 152 |
if col_clear_btn.button("Clear " ,use_container_width=True):
|
| 153 |
streamlit_js_eval(js_expressions="parent.window.location.reload()")
|
|
|
|
|
|
|
| 154 |
if col_submit_btn.button("Submit", use_container_width=True):
|
| 155 |
if len(job_title) > 0 and len(email) > 0 and uploaded_file_jobdescription and len(uploaded_file_cvs)>0:
|
| 156 |
data = {
|
|
@@ -160,12 +255,13 @@ with st.container():
|
|
| 160 |
"question_two": "",
|
| 161 |
"question_three": "",
|
| 162 |
}
|
| 163 |
-
if
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
json_data = json.dumps(data, ensure_ascii=False)
|
| 171 |
|
|
@@ -177,14 +273,11 @@ with st.container():
|
|
| 177 |
|
| 178 |
pdf_name = uuid_string
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
for i,cv in enumerate(st.session_state["cvs"]):
|
| 183 |
-
print(cv.name)
|
| 184 |
-
pdf_data_cvs.append(cv.read())
|
| 185 |
# pdf_data_cv = uploaded_file_cv.read()
|
| 186 |
|
| 187 |
-
upload_success = upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs)
|
| 188 |
else:
|
| 189 |
st.write("Please fill out both fields and upload a PDF file.")
|
| 190 |
|
|
|
|
| 2 |
from streamlit_js_eval import streamlit_js_eval
|
| 3 |
from azure.storage.blob import BlobServiceClient
|
| 4 |
from azure.cosmos import CosmosClient, exceptions
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
+
import io
|
| 7 |
+
import openai
|
| 8 |
import json
|
| 9 |
import os
|
| 10 |
import uuid
|
| 11 |
import time
|
| 12 |
import calendar
|
| 13 |
+
import re
|
| 14 |
|
| 15 |
connection_string = os.getenv("CONNECTION")
|
| 16 |
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
|
| 17 |
|
| 18 |
|
| 19 |
+
def upload_blob(pdf_name, json_data, pdf_data_jobdescription,pdf_data_cvs, pre_generated_bool, custom_questions):
|
| 20 |
try:
|
| 21 |
container_name = "jobdescriptions"
|
| 22 |
# json_blob_name = f"{pdf_name}_jsondata.json"
|
|
|
|
| 30 |
pdf_blob_client = container_client.get_blob_client(pdf_blob_name_jobdescription)
|
| 31 |
pdf_blob_client.upload_blob(pdf_data_jobdescription, overwrite=True)
|
| 32 |
|
| 33 |
+
upload_job_db_item(pdf_name,len(pdf_data_cvs),json.loads(json_data),pre_generated_bool, custom_questions)
|
| 34 |
|
| 35 |
links = []
|
| 36 |
names = []
|
|
|
|
| 57 |
print(f"Fehler beim Hochladen der Daten: {str(e)}")
|
| 58 |
return False
|
| 59 |
|
| 60 |
+
def upload_job_db_item(id, number_of_applicants, data, pre_generated_bool, custom_questions):
|
| 61 |
endpoint = "https://wg-candidate-data.documents.azure.com:443/"
|
| 62 |
key = os.getenv("CONNECTION_DB")
|
| 63 |
client = CosmosClient(endpoint, key)
|
|
|
|
| 73 |
"question_one": data["question_one"],
|
| 74 |
"question_two": data["question_two"],
|
| 75 |
"question_three": data["question_three"],
|
| 76 |
+
"pre_generated": pre_generated_bool,
|
| 77 |
+
"custom_questions": custom_questions
|
| 78 |
}
|
| 79 |
try:
|
| 80 |
# Fügen Sie das Element in den Container ein
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
print(f"Allgemeiner Fehler: {str(e)}")
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
st.markdown(
|
| 119 |
"""
|
| 120 |
<style>
|
|
|
|
| 130 |
)
|
| 131 |
col1, col2 = st.columns([2, 1])
|
| 132 |
|
| 133 |
+
if "ai_questions" not in st.session_state:
|
| 134 |
+
st.session_state["ai_questions"] = None
|
| 135 |
+
if "pdf_data_cvs" not in st.session_state:
|
| 136 |
+
st.session_state["pdf_data_cvs"] = None
|
| 137 |
+
if "pdf_data_cvs_string" not in st.session_state:
|
| 138 |
+
st.session_state["pdf_data_cvs_string"] = None
|
| 139 |
+
if "pdf_data_jobdescription" not in st.session_state:
|
| 140 |
+
st.session_state["pdf_data_jobdescription"] = None
|
| 141 |
+
if "pdf_data_jobdescription_string" not in st.session_state:
|
| 142 |
+
st.session_state["pdf_data_jobdescription_string"] = None
|
| 143 |
+
if "final_question_string" not in st.session_state:
|
| 144 |
+
st.session_state["final_question_string"] = []
|
| 145 |
+
|
| 146 |
+
with open("sys_prompt_frontend.txt") as f:
|
| 147 |
+
sys_prompt = f.read()
|
| 148 |
|
| 149 |
col1.title("Job description upload")
|
| 150 |
col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg")
|
| 151 |
|
| 152 |
st.write("Please upload the job description and resume(s) as PDF and enter the job title for the position. To receive the evaluation of the potential candidate(s), please provide your email address.")
|
| 153 |
upload_success = True
|
| 154 |
+
|
| 155 |
+
#This container represents the form
|
| 156 |
with st.container():
|
| 157 |
+
|
| 158 |
+
#Form section for the files, names, title and mail
|
| 159 |
uploaded_file_jobdescription = st.file_uploader("Upload the job description:", type=["pdf"], key="job")
|
| 160 |
job_title = st.text_input("Enter the job title:", key="title")
|
| 161 |
email = st.text_input("Enter the email:" , key="mail")
|
| 162 |
uploaded_file_cvs = st.file_uploader("Upload the resume(s):", type=["pdf"],accept_multiple_files=True, key="cvs")
|
| 163 |
for i,cv in enumerate(st.session_state["cvs"]):
|
| 164 |
st.text_input(label="Enter the name of the "+str(i+1)+". CV (File: "+cv.name+")", value=cv.name,key="cv-"+str(i+1))
|
| 165 |
+
|
| 166 |
+
#Form section for the interview mode (pre generated or not) and additional questions
|
| 167 |
+
if len(job_title) > 0 and len(email) > 0 and uploaded_file_jobdescription and len(uploaded_file_cvs)>0:
|
| 168 |
+
st.write("Activate the toggle to generate and select the questions in advance. Otherwise the questions will be generated automatically during the interview.")
|
| 169 |
+
if not st.session_state["pdf_data_cvs"] and not st.session_state["pdf_data_cvs_string"] and not st.session_state["pdf_data_jobdescription"] and not st.session_state["pdf_data_jobdescription_string"]:
|
| 170 |
+
pdf_data_jobdescription = uploaded_file_jobdescription.read()
|
| 171 |
+
pdf_data_jobdescription_string = ""
|
| 172 |
+
pdf_reader_job = PdfReader(io.BytesIO(pdf_data_jobdescription))
|
| 173 |
+
for page_num in range(len(pdf_reader_job.pages)):
|
| 174 |
+
page = pdf_reader_job.pages[page_num]
|
| 175 |
+
pdf_data_jobdescription_string += page.extract_text()
|
| 176 |
+
pdf_data_cvs = []
|
| 177 |
+
pdf_data_cvs_string = ""
|
| 178 |
+
for i,cv in enumerate(st.session_state["cvs"]):
|
| 179 |
+
print(cv.name)
|
| 180 |
+
# print(cv.name)
|
| 181 |
+
# print(cv.size)
|
| 182 |
+
cv_data_bytes = cv.read()
|
| 183 |
+
# print(len(cv_data_bytes))
|
| 184 |
+
pdf_data_cvs.append(cv_data_bytes)
|
| 185 |
+
pdf_reader_cvs = PdfReader(io.BytesIO(cv_data_bytes))
|
| 186 |
+
pdf_data_cvs_string += "CV "+str(i+1)+": "
|
| 187 |
+
for page_num in range(len(pdf_reader_cvs.pages)):
|
| 188 |
+
page = pdf_reader_cvs.pages[page_num]
|
| 189 |
+
pdf_data_cvs_string += page.extract_text()
|
| 190 |
+
pdf_data_cvs_string += "\n"
|
| 191 |
+
st.session_state["pdf_data_cvs"] = pdf_data_cvs
|
| 192 |
+
st.session_state["pdf_data_cvs_string"] = pdf_data_cvs_string
|
| 193 |
+
st.session_state["pdf_data_jobdescription"] = pdf_data_jobdescription
|
| 194 |
+
st.session_state["pdf_data_jobdescription_string"] = pdf_data_jobdescription_string
|
| 195 |
+
pre_generate = st.toggle("Activate to pre generate questions", key="pre_toggle")
|
| 196 |
+
if pre_generate:
|
| 197 |
+
system = sys_prompt.format(job=st.session_state["pdf_data_jobdescription_string"], resume=st.session_state["pdf_data_cvs_string"], n=15)
|
| 198 |
+
if not st.session_state["ai_questions"]:
|
| 199 |
+
try:
|
| 200 |
+
st.write("The questions are generated. This may take a short moment...")
|
| 201 |
+
res = openai.ChatCompletion.create(
|
| 202 |
+
model="gpt-4",
|
| 203 |
+
temperature=0.2,
|
| 204 |
+
messages=[
|
| 205 |
+
{
|
| 206 |
+
"role": "system",
|
| 207 |
+
"content": system,
|
| 208 |
+
},
|
| 209 |
+
],
|
| 210 |
+
)
|
| 211 |
+
st.session_state["ai_questions"] = res.choices[0]["message"]["content"].split("\n")
|
| 212 |
+
for i,q in enumerate(res.choices[0]["message"]["content"].split("\n")):
|
| 213 |
+
st.session_state["disable_row_"+str(i)] = False
|
| 214 |
+
st.rerun()
|
| 215 |
+
except Exception as e:
|
| 216 |
+
print(f"Fehler beim generieren der Fragen: {str(e)}")
|
| 217 |
+
st.error("An error has occurred. Please reload the page or contact the admin.", icon="🚨")
|
| 218 |
+
else:
|
| 219 |
+
for i,question in enumerate(st.session_state["ai_questions"]):
|
| 220 |
+
cols = st.columns([5,1])
|
| 221 |
+
with cols[1]:
|
| 222 |
+
if st.button("Accept",use_container_width=True,key="btn_accept_row_"+str(i)):
|
| 223 |
+
print("accept")
|
| 224 |
+
pattern = re.compile(r"^[1-9][0-9]?\.")
|
| 225 |
+
questions_length = len(st.session_state["final_question_string"])
|
| 226 |
+
question_from_text_area = st.session_state["text_area_"+str(i)]
|
| 227 |
+
question_to_append = str(questions_length+1)+"."+re.sub(pattern, "", question_from_text_area)
|
| 228 |
+
st.session_state["final_question_string"].append(question_to_append)
|
| 229 |
+
st.session_state["disable_row_"+str(i)] = True
|
| 230 |
+
st.rerun()
|
| 231 |
+
if st.button("Delete",use_container_width=True,key="btn_del_row_"+str(i)):
|
| 232 |
+
print("delete")
|
| 233 |
+
st.session_state["ai_questions"].remove(question)
|
| 234 |
+
st.rerun()
|
| 235 |
+
with cols[0]:
|
| 236 |
+
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)])
|
| 237 |
+
else:
|
| 238 |
+
with st.expander("Enter up to three predefined questions if needed. Otherwise leave it blank:"):
|
| 239 |
+
question_one = st.text_input("Enter the first question:")
|
| 240 |
+
question_two = st.text_input("Enter the second question:")
|
| 241 |
+
question_three = st.text_input("Enter the third question:")
|
| 242 |
+
|
| 243 |
+
#Form section for Submit and Clear
|
| 244 |
col_submit_btn, col_empty, col_clear_btn = st.columns([1,4, 1])
|
| 245 |
if col_clear_btn.button("Clear " ,use_container_width=True):
|
| 246 |
streamlit_js_eval(js_expressions="parent.window.location.reload()")
|
| 247 |
+
|
| 248 |
+
#Code to handle the input
|
| 249 |
if col_submit_btn.button("Submit", use_container_width=True):
|
| 250 |
if len(job_title) > 0 and len(email) > 0 and uploaded_file_jobdescription and len(uploaded_file_cvs)>0:
|
| 251 |
data = {
|
|
|
|
| 255 |
"question_two": "",
|
| 256 |
"question_three": "",
|
| 257 |
}
|
| 258 |
+
if not st.session_state["pre_toggle"]:
|
| 259 |
+
if question_one:
|
| 260 |
+
data["question_one"] = question_one
|
| 261 |
+
if question_two:
|
| 262 |
+
data["question_two"] = question_two
|
| 263 |
+
if question_three:
|
| 264 |
+
data["question_three"] = question_three
|
| 265 |
|
| 266 |
json_data = json.dumps(data, ensure_ascii=False)
|
| 267 |
|
|
|
|
| 273 |
|
| 274 |
pdf_name = uuid_string
|
| 275 |
|
| 276 |
+
print(st.session_state["final_question_string"])
|
| 277 |
+
|
|
|
|
|
|
|
|
|
|
| 278 |
# pdf_data_cv = uploaded_file_cv.read()
|
| 279 |
|
| 280 |
+
upload_success = upload_blob(pdf_name, json_data, st.session_state["pdf_data_jobdescription"],st.session_state["pdf_data_cvs"],st.session_state["pre_toggle"],st.session_state["final_question_string"])
|
| 281 |
else:
|
| 282 |
st.write("Please fill out both fields and upload a PDF file.")
|
| 283 |
|