Spaces:
Sleeping
Sleeping
import requests | |
import json | |
import os | |
import uuid | |
import streamlit as st | |
import math | |
from datetime import datetime, timedelta | |
from dotenv import load_dotenv | |
from typing import List | |
load_dotenv() | |
def get_question_topics(): | |
url = os.environ["BASE_URL"]+"/question-topics" | |
headers = { | |
"x-api-key": os.environ["API_KEY"] | |
} | |
try: | |
response = requests.get(url,headers=headers) | |
response.raise_for_status() | |
return response.json()["output"] | |
except Exception as e: | |
print(f"Fehler beim Fetchen der Topics: {e}") | |
st.error("Someting went wrong. Please try again later.", icon="🚨") | |
def get_coding_task_topics(): | |
url = os.environ["BASE_URL"]+"/coding-task-topics" | |
headers = { | |
"x-api-key": os.environ["API_KEY"] | |
} | |
try: | |
response = requests.get(url,headers=headers) | |
response.raise_for_status() | |
return response.json()["output"] | |
except Exception as e: | |
print(f"Fehler beim Fetchen der Topics: {e}") | |
st.error("Someting went wrong. Please try again later.", icon="🚨") | |
def get_questions(topic): | |
url = os.environ["BASE_URL"]+"/questions"+f"?dimension={topic}" | |
headers = { | |
"x-api-key": os.environ["API_KEY"] | |
} | |
try: | |
response = requests.get(url, data=topic, headers=headers) | |
response.raise_for_status() | |
# print(response.json()) | |
return response.json()["output"] | |
except Exception as e: | |
print(f"Fehler beim Fetchen der Topics: {e}") | |
st.error("Someting went wrong. Please try again later.", icon="🚨") | |
def get_coding_tasks(topic): | |
url = os.environ["BASE_URL"]+"/coding-tasks"+f"?topic={topic}" | |
headers = { | |
"x-api-key": os.environ["API_KEY"] | |
} | |
try: | |
response = requests.get(url, data=topic, headers=headers) | |
response.raise_for_status() | |
print(response.json()) | |
return response.json()["output"] | |
except Exception as e: | |
print(f"Fehler beim Fetchen der Topics: {e}") | |
st.error("Someting went wrong. Please try again later.", icon="🚨") | |
def load_topics_into_question_list(): | |
with st.spinner("Loading questions..."): | |
for topic in st.session_state["loaded_questions"]: | |
if topic["topic"] not in st.session_state["selected_topics"]: | |
st.session_state["loaded_questions"].remove(topic) | |
for topic in st.session_state["selected_topics"]: | |
if topic not in [topic["topic"] for topic in st.session_state["loaded_questions"]]: | |
question_element = {"topic": topic, "questions": get_questions(topic)} | |
st.session_state["loaded_questions"].append(question_element) | |
def load_topics_into_task_list(): | |
with st.spinner("Loading coding tasks..."): | |
for topic in st.session_state["loaded_tasks"]: | |
if topic["topic"] not in st.session_state["selected_task_topics"]: | |
st.session_state["loaded_tasks"].remove(topic) | |
for topic in st.session_state["selected_task_topics"]: | |
if topic not in [topic["topic"] for topic in st.session_state["loaded_tasks"]]: | |
question_element = {"topic": topic, "tasks": get_coding_tasks(topic)} | |
st.session_state["loaded_tasks"].append(question_element) | |
def add_question_to_interview(question): | |
if question not in st.session_state["questions_for_interview"]: | |
st.session_state["questions_for_interview"].append(question) | |
else: | |
print("Question already in list") | |
def increment_page(): | |
if st.session_state["current_page"] < math.ceil(len(st.session_state["questions_to_display"])/10): | |
st.session_state["current_page"] += 1 | |
def decrement_page(): | |
if st.session_state["current_page"] > 1: | |
st.session_state["current_page"] -= 1 | |
def change_question_type(): | |
if st.session_state["question_type"] == "question": | |
st.session_state["question_type"] = "coding_tasks" | |
else: | |
st.session_state["question_type"] = "question" | |
if "topics" not in st.session_state: | |
topics = list(set([topic["dimension"] for topic in get_question_topics() if topic["dimension"] != None])) | |
st.session_state["topics"] = topics | |
if "task_topics" not in st.session_state: | |
topics = list(set([topic for topic in get_coding_task_topics() if topic != None])) | |
st.session_state["task_topics"] = topics | |
if "loaded_questions" not in st.session_state: | |
st.session_state["loaded_questions"] = [] | |
if "loaded_tasks" not in st.session_state: | |
st.session_state["loaded_tasks"] = [] | |
if "selected_topics" not in st.session_state: | |
st.session_state["selected_topics"] = [] | |
if "questions_to_display" not in st.session_state: | |
st.session_state["questions_to_display"] = [] | |
if "tasks_to_display" not in st.session_state: | |
st.session_state["tasks_to_display"] = [] | |
if "questions_for_interview" not in st.session_state: | |
st.session_state["questions_for_interview"] = [] | |
if "current_page" not in st.session_state: | |
st.session_state["current_page"] = 1 | |
if "question_type" not in st.session_state: | |
st.session_state["question_type"] = "question" | |
if "tasks_for_interview" not in st.session_state: | |
st.session_state["tasks_for_interview"] = [] | |
col1, col2 = st.columns([2, 1]) | |
col1.title("Interview Preparation") | |
col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg") | |
type_col1, type_col2 = st.columns([1, 1]) | |
with type_col1: | |
st.button("Questions", key="question_button", on_click=change_question_type, disabled=True if st.session_state["question_type"] == "question" else False, use_container_width=True) | |
with type_col2: | |
st.button("Coding tasks", key="coding_tasks", on_click=change_question_type, disabled=True if st.session_state["question_type"] == "coding_tasks" else False, use_container_width=True) | |
if st.session_state["question_type"] == "question": | |
st.multiselect("Select the topics for the questions", st.session_state["topics"], key="selected_topics", default=None, on_change=load_topics_into_question_list) | |
st.divider() | |
if len(st.session_state["selected_topics"]) > 0: | |
st.subheader("Questions to choose from:") | |
st.write("Filter according to the difficulty of the questions:") | |
st.checkbox("Easy", key="checkbox_filter_easy", value=True) | |
st.checkbox("Medium", key="checkbox_filter_medium", value=True) | |
st.checkbox("Hard", key="checkbox_filter_hard", value=True) | |
st.write("Filter by the topic of the questions:") | |
for topic in st.session_state["loaded_questions"]: | |
st.checkbox(topic["topic"], key="checkbox_filter_"+topic["topic"], value=True) | |
result_questions = [] | |
for i, topic in enumerate(st.session_state["loaded_questions"]): | |
if st.session_state["checkbox_filter_"+topic["topic"]]: | |
for j, question in enumerate(topic["questions"]): | |
if st.session_state["checkbox_filter_easy"] and question["difficulty"] == "Easy": | |
result_questions.append(question) | |
if st.session_state["checkbox_filter_medium"] and question["difficulty"] == "Medium": | |
result_questions.append(question) | |
if st.session_state["checkbox_filter_hard"] and question["difficulty"] == "Hard": | |
result_questions.append(question) | |
st.session_state["questions_to_display"] = result_questions | |
if len(st.session_state["questions_to_display"]) > 0: | |
nav_col1, nav_col2, nav_col3 = st.columns([1, 4, 1]) | |
nav_col1.button("◀️", key="nav1", use_container_width=True, on_click=decrement_page, disabled=True if st.session_state["current_page"] == 1 else False) | |
nav_col2.markdown(f"<div style='text-align: center;'>Page {str(st.session_state['current_page'])} / {str(math.ceil(len(st.session_state['questions_to_display'])/10))}</div>", unsafe_allow_html=True) | |
nav_col3.button("▶️", key="nav2", use_container_width=True, on_click=increment_page, disabled=True if st.session_state["current_page"] == math.ceil(len(st.session_state["questions_to_display"])/10) else False) | |
for i in range((st.session_state["current_page"]-1)*10, st.session_state["current_page"]*10 if st.session_state["current_page"]*10 < len(st.session_state["questions_to_display"]) else len(st.session_state["questions_to_display"])): | |
col_1, col_2 = st.columns([7, 1]) | |
with col_1: | |
st.write(st.session_state["questions_to_display"][i]["question"]) | |
with col_2: | |
st.button("Select", disabled=True if st.session_state["questions_to_display"][i] in st.session_state["questions_for_interview"] else False ,key="select_button_"+str(i), on_click=add_question_to_interview, args=(st.session_state["questions_to_display"][i],)) | |
else: | |
st.multiselect("Select the topics for the coding tasks", options=st.session_state["task_topics"], key="selected_task_topics", default=None, on_change=load_topics_into_task_list) | |
st.divider() | |
if len(st.session_state["selected_task_topics"]) > 0: | |
st.subheader("Coding tasks to choose from:") | |
st.write("Filter by the topic of the coding task:") | |
for topic in st.session_state["loaded_tasks"]: | |
st.checkbox(topic["topic"], key="checkbox_filter_tasks_"+topic["topic"], value=True) | |
result_tasks = [] | |
for i, topic in enumerate(st.session_state["loaded_tasks"]): | |
if st.session_state["checkbox_filter_tasks_"+topic["topic"]]: | |
for j, task in enumerate(topic["tasks"]): | |
result_tasks.append(task) | |
st.session_state["tasks_to_display"] = result_tasks | |
if len(st.session_state["tasks_to_display"]) > 0: | |
nav_col1, nav_col2, nav_col3 = st.columns([1, 4, 1]) | |
nav_col1.button("◀️", key="nav1", use_container_width=True, on_click=decrement_page, disabled=True if st.session_state["current_page"] == 1 else False) | |
nav_col2.markdown(f"<div style='text-align: center;'>Page {str(st.session_state['current_page'])} / {str(math.ceil(len(st.session_state['tasks_to_display'])/10))}</div>", unsafe_allow_html=True) | |
nav_col3.button("▶️", key="nav2", use_container_width=True, on_click=increment_page, disabled=True if st.session_state["current_page"] == math.ceil(len(st.session_state["tasks_to_display"])/10) else False) | |
for i in range((st.session_state["current_page"]-1)*10, st.session_state["current_page"]*10 if st.session_state["current_page"]*10 < len(st.session_state["tasks_to_display"]) else len(st.session_state["tasks_to_display"])): | |
col_1, col_2 = st.columns([7, 1]) | |
with col_1: | |
st.write(st.session_state["tasks_to_display"][i]["task"]) | |
with col_2: | |
st.button("Select", disabled=True if st.session_state["tasks_to_display"][i] in st.session_state["tasks_for_interview"] else False ,key="select_button_tasks_"+str(i), on_click=st.session_state["tasks_for_interview"].append, args=(st.session_state["tasks_to_display"][i],)) | |
st.divider() | |
st.subheader("Questions for the interview:") | |
if len(st.session_state["questions_for_interview"]) == 0: | |
st.write("No questions selected yet.") | |
for i, question in enumerate(st.session_state["questions_for_interview"]): | |
interview_col_1, interview_col_2 = st.columns([7, 1]) | |
with interview_col_1: | |
st.write(question["question"]) | |
with interview_col_2: | |
st.button("Remove", key="remove_button_"+str(i), on_click=st.session_state["questions_for_interview"].remove, args=(question,)) | |
st.subheader("Coding tasks for the interview:") | |
if len(st.session_state["tasks_for_interview"]) == 0: | |
st.write("No coding tasks selected yet.") | |
for i, task in enumerate(st.session_state["tasks_for_interview"]): | |
interview_col_1, interview_col_2 = st.columns([7, 1]) | |
with interview_col_1: | |
st.write(task["task"]) | |
with interview_col_2: | |
st.button("Remove", key="remove_button_tasks_"+str(i), on_click=st.session_state["tasks_for_interview"].remove, args=(task,)) | |
st.divider() | |