Spaces:
Running
Running
""" | |
File: calculate_practical_tasks.py | |
Author: Elena Ryumina and Dmitry Ryumin | |
Description: Event handler for Gradio app to calculate practical tasks. | |
License: MIT License | |
""" | |
from app.oceanai_init import b5 | |
import gradio as gr | |
import pandas as pd | |
from pathlib import Path | |
# Importing necessary components for the Gradio app | |
from app.config import config_data | |
from app.components import html_message, dataframe, files_create_ui, video_create_ui | |
def read_csv_file(file_path, drop_id=False): | |
df = pd.read_csv(file_path) | |
if drop_id: | |
df = pd.DataFrame(df.drop(["ID"], axis=1)) | |
df.index.name = "ID" | |
df.index += 1 | |
df.index = df.index.map(str) | |
return df | |
def apply_rounding_and_rename_columns(df): | |
df_rounded = df.rename( | |
columns={ | |
"Openness": "OPE", | |
"Conscientiousness": "CON", | |
"Extraversion": "EXT", | |
"Agreeableness": "AGR", | |
"Non-Neuroticism": "NNEU", | |
} | |
) | |
columns_to_round = df_rounded.columns[1:] | |
df_rounded[columns_to_round] = df_rounded[columns_to_round].apply( | |
lambda x: [round(i, 3) for i in x] | |
) | |
return df_rounded | |
def colleague_type(subtask): | |
return "minor" if "junior" in subtask.lower() else "major" | |
def event_handler_calculate_practical_task_blocks( | |
files, | |
practical_subtasks, | |
pt_scores, | |
threshold_professional_skills, | |
dropdown_professional_skills, | |
target_score_ope, | |
target_score_con, | |
target_score_ext, | |
target_score_agr, | |
target_score_nneu, | |
equal_coefficient, | |
): | |
if practical_subtasks.lower() == "professional skills": | |
df_professional_skills = read_csv_file(config_data.Links_PROFESSIONAL_SKILLS) | |
b5._priority_skill_calculation( | |
df_files=pt_scores.iloc[:, 1:], | |
correlation_coefficients=df_professional_skills, | |
threshold=threshold_professional_skills, | |
out=True, | |
) | |
# Optional | |
df = apply_rounding_and_rename_columns(b5.df_files_priority_skill_) | |
professional_skills_list = ( | |
config_data.Settings_DROPDOWN_PROFESSIONAL_SKILLS.copy() | |
) | |
professional_skills_list.remove(dropdown_professional_skills) | |
professional_skills_list = [ | |
"OPE", | |
"CON", | |
"EXT", | |
"AGR", | |
"NNEU", | |
] + professional_skills_list | |
df_hidden = df.drop(columns=professional_skills_list) | |
df_hidden.to_csv(config_data.Filenames_PT_SKILLS_SCORES) | |
df_hidden.reset_index(inplace=True) | |
df_hidden = df_hidden.sort_values( | |
by=[dropdown_professional_skills], ascending=False | |
) | |
person_id = int(df_hidden.iloc[0]["Person ID"]) - 1 | |
return ( | |
gr.Row(visible=True), | |
gr.Column(visible=True), | |
dataframe( | |
headers=df_hidden.columns.tolist(), | |
values=df_hidden.values.tolist(), | |
visible=True, | |
), | |
files_create_ui( | |
config_data.Filenames_PT_SKILLS_SCORES, | |
"single", | |
[".csv"], | |
config_data.OtherMessages_EXPORT_PS, | |
True, | |
False, | |
True, | |
"csv-container", | |
), | |
video_create_ui( | |
value=files[person_id], | |
file_name=Path(files[person_id]).name, | |
label="Best Person ID - " + str(person_id + 1), | |
visible=True, | |
), | |
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False), | |
) | |
elif ( | |
practical_subtasks.lower() == "finding a suitable junior colleague" | |
or practical_subtasks.lower() == "finding a suitable senior colleague" | |
): | |
df_correlation_coefficients = read_csv_file(config_data.Links_FINDING_COLLEAGUE) | |
b5._colleague_ranking( | |
df_files=pt_scores.iloc[:, 1:], | |
correlation_coefficients=df_correlation_coefficients, | |
target_scores=[ | |
target_score_ope, | |
target_score_con, | |
target_score_ext, | |
target_score_agr, | |
target_score_nneu, | |
], | |
colleague=colleague_type(practical_subtasks), | |
equal_coefficients=equal_coefficient, | |
out=False, | |
) | |
# Optional | |
df = df = apply_rounding_and_rename_columns(b5.df_files_colleague_) | |
professional_skills_list = [ | |
"OPE", | |
"CON", | |
"EXT", | |
"AGR", | |
"NNEU", | |
] | |
df_hidden = df.drop(columns=professional_skills_list) | |
df_hidden.to_csv( | |
colleague_type(practical_subtasks) + config_data.Filenames_COLLEAGUE_RANKING | |
) | |
df_hidden.reset_index(inplace=True) | |
person_id = int(df_hidden.iloc[0]["Person ID"]) - 1 | |
return ( | |
gr.Row(visible=True), | |
gr.Column(visible=True), | |
dataframe( | |
headers=df_hidden.columns.tolist(), | |
values=df_hidden.values.tolist(), | |
visible=True, | |
), | |
files_create_ui( | |
colleague_type(practical_subtasks) | |
+ config_data.Filenames_COLLEAGUE_RANKING, | |
"single", | |
[".csv"], | |
config_data.OtherMessages_EXPORT_WT, | |
True, | |
False, | |
True, | |
"csv-container", | |
), | |
video_create_ui( | |
value=files[person_id], | |
file_name=Path(files[person_id]).name, | |
label="Best Person ID - " + str(person_id + 1), | |
visible=True, | |
), | |
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False), | |
) | |
else: | |
gr.Info(config_data.InformationMessages_NOTI_IN_DEV) | |
return ( | |
gr.Row(visible=False), | |
gr.Column(visible=False), | |
dataframe(visible=False), | |
files_create_ui( | |
None, | |
"single", | |
[".csv"], | |
config_data.OtherMessages_EXPORT_PS, | |
True, | |
False, | |
False, | |
"csv-container", | |
), | |
video_create_ui(visible=False), | |
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, True), | |
) | |