Spaces:
Running
Running
File size: 21,792 Bytes
d0f716d b0f25b3 1bf4da3 d0f716d aa2aec1 d0f716d 78f883e 1bf4da3 c0f084a 78f883e d0f716d c0f084a 0e30f40 c0f084a 0e30f40 1bf4da3 1a72a47 1bf4da3 1a72a47 1bf4da3 78f883e b0f25b3 aa2aec1 1bf4da3 aa2aec1 0e30f40 c0f084a b0f25b3 1a72a47 1bf4da3 096ab96 1bf4da3 1a72a47 1bf4da3 096ab96 1bf4da3 1a72a47 1bf4da3 1a72a47 1bf4da3 78f883e 1bf4da3 78f883e 1bf4da3 78f883e 1bf4da3 c0f084a 1bf4da3 78f883e c0f084a 78f883e c0f084a 096ab96 c0f084a 78f883e c0f084a 1bf4da3 78f883e c0f084a 1bf4da3 c0f084a 78f883e c0f084a 0e30f40 b0f25b3 aa2aec1 c0f084a b0f25b3 0e30f40 b0f25b3 aa2aec1 c0f084a aa2aec1 b0f25b3 aa2aec1 096ab96 b0f25b3 78f883e aa2aec1 b0f25b3 aa2aec1 b0f25b3 0e30f40 1bf4da3 78f883e 0e30f40 1bf4da3 0e30f40 78f883e 0e30f40 c0f084a 0e30f40 c0f084a 0e30f40 c0f084a 0e30f40 096ab96 0e30f40 78f883e 0e30f40 c0f084a 1bf4da3 78f883e c0f084a 1bf4da3 c0f084a 78f883e c0f084a 1bf4da3 c0f084a 1bf4da3 c0f084a 1bf4da3 c0f084a 096ab96 c0f084a 096ab96 c0f084a 096ab96 c0f084a 78f883e c0f084a b0f25b3 1bf4da3 78f883e aa2aec1 1bf4da3 aa2aec1 b0f25b3 78f883e b0f25b3 aa2aec1 b0f25b3 1bf4da3 78f883e aa2aec1 78f883e b0f25b3 |
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 |
"""
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 re
import gradio as gr
from pathlib import Path
# Importing necessary components for the Gradio app
from app.config import config_data
from app.video_metadata import video_metadata
from app.mbti_description import MBTI_DESCRIPTION, MBTI_DATA
from app.utils import (
read_csv_file,
apply_rounding_and_rename_columns,
preprocess_scores_df,
)
from app.components import (
html_message,
dataframe,
files_create_ui,
video_create_ui,
textbox_create_ui,
)
def colleague_type(subtask):
return "minor" if "junior" in subtask.lower() else "major"
def consumer_preferences(subtask):
return (
config_data.Filenames_CAR_CHARACTERISTICS
if "mobile device" in subtask.lower()
else config_data.Filenames_MDA_CATEGORIES
)
def remove_parentheses(s):
return re.sub(r"\s*\([^)]*\)", "", s)
def extract_text_in_parentheses(s):
result = re.search(r"\(([^)]+)\)", s)
if result:
return result.group(1)
else:
return None
def compare_strings(original, comparison, prev=False):
result = []
prev_class = None
for orig_char, comp_char in zip(original, comparison):
curr_class = "true" if orig_char == comp_char else "err"
if not prev:
result.append(f"<span class='{curr_class}'>{comp_char}</span>")
else:
if curr_class != prev_class:
result.append("</span>" if prev_class else "")
result.append(f"<span class='{curr_class}'>")
prev_class = curr_class
result.append(comp_char)
return f"<span class='wrapper_mbti'>{''.join(result + [f'</span>' if prev_class else ''])}</span>"
def create_person_metadata(person_id, files, video_metadata):
if (
Path(files[person_id]).name in video_metadata
and config_data.Settings_SHOW_VIDEO_METADATA
):
person_metadata_list = video_metadata[Path(files[person_id]).name]
return (
gr.Column(visible=True),
gr.Row(visible=True),
gr.Row(visible=True),
gr.Image(visible=True),
textbox_create_ui(
person_metadata_list[0],
"text",
"First name",
None,
None,
1,
True,
False,
True,
False,
1,
False,
),
gr.Row(visible=True),
gr.Image(visible=True),
textbox_create_ui(
person_metadata_list[1],
"text",
"Last name",
None,
None,
1,
True,
False,
True,
False,
1,
False,
),
gr.Row(visible=True),
gr.Row(visible=True),
gr.Image(visible=True),
textbox_create_ui(
person_metadata_list[2],
"email",
"Email",
None,
None,
1,
True,
False,
True,
False,
1,
False,
),
gr.Row(visible=True),
gr.Image(visible=True),
textbox_create_ui(
person_metadata_list[3],
"text",
"Phone number",
None,
None,
1,
True,
False,
True,
False,
1,
False,
),
)
else:
return (
gr.Column(visible=False),
gr.Row(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
)
def event_handler_calculate_practical_task_blocks(
files,
practical_subtasks,
pt_scores,
dropdown_mbti,
threshold_mbti,
threshold_professional_skills,
dropdown_professional_skills,
target_score_ope,
target_score_con,
target_score_ext,
target_score_agr,
target_score_nneu,
equal_coefficient,
number_priority,
number_importance_traits,
threshold_consumer_preferences,
number_openness,
number_conscientiousness,
number_extraversion,
number_agreeableness,
number_non_neuroticism,
):
if practical_subtasks.lower() == "16 personality types of mbti":
df_correlation_coefficients = read_csv_file(config_data.Links_MBTI)
pt_scores_copy = pt_scores.iloc[:, 1:].copy()
preprocess_scores_df(pt_scores_copy, config_data.Dataframes_PT_SCORES[0][0])
b5._professional_match(
df_files=pt_scores_copy,
correlation_coefficients=df_correlation_coefficients,
personality_type=remove_parentheses(dropdown_mbti),
threshold=threshold_mbti,
out=False,
)
df = apply_rounding_and_rename_columns(b5.df_files_MBTI_job_match_)
df_hidden = df.drop(
columns=config_data.Settings_SHORT_PROFESSIONAL_SKILLS
+ config_data.Settings_DROPDOWN_MBTI_DEL_COLS
)
df_hidden.rename(
columns={
"Path": "Filename",
"MBTI": "Personality Type",
"MBTI_Score": "Personality Type Score",
},
inplace=True,
)
df_hidden.to_csv(config_data.Filenames_MBTI_JOB)
df_hidden.reset_index(inplace=True)
person_id = int(df_hidden.iloc[0][config_data.Dataframes_PT_SCORES[0][0]]) - 1
short_mbti = extract_text_in_parentheses(dropdown_mbti)
mbti_values = df_hidden["Personality Type"].tolist()
df_hidden["Personality Type"] = [
compare_strings(short_mbti, mbti, False) for mbti in mbti_values
]
person_metadata = create_person_metadata(person_id, files, video_metadata)
existing_tuple = (
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_MBTI_JOB,
"single",
[".csv"],
config_data.OtherMessages_EXPORT_MBTI,
True,
False,
True,
"csv-container",
),
gr.Accordion(
label=config_data.Labels_NOTE_MBTI_LABEL,
open=False,
visible=True,
),
gr.HTML(value=MBTI_DESCRIPTION, visible=True),
dataframe(
headers=MBTI_DATA.columns.tolist(),
values=MBTI_DATA.values.tolist(),
visible=True,
elem_classes="mbti-dataframe",
),
gr.Column(visible=True),
video_create_ui(
value=files[person_id],
file_name=Path(files[person_id]).name,
label="Best Person ID - " + str(person_id + 1),
visible=True,
elem_classes="video-sorted-container",
),
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False),
)
return existing_tuple[:-1] + person_metadata + existing_tuple[-1:]
elif practical_subtasks.lower() == "professional groups":
sum_weights = sum(
[
number_openness,
number_conscientiousness,
number_extraversion,
number_agreeableness,
number_non_neuroticism,
]
)
if sum_weights != 100:
gr.Warning(config_data.InformationMessages_SUM_WEIGHTS.format(sum_weights))
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",
),
gr.Accordion(visible=False),
gr.HTML(visible=False),
dataframe(visible=False),
gr.Column(visible=False),
video_create_ui(visible=False),
gr.Column(visible=False),
gr.Row(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
html_message(
config_data.InformationMessages_SUM_WEIGHTS.format(sum_weights),
False,
True,
),
)
else:
b5._candidate_ranking(
df_files=pt_scores.iloc[:, 1:],
weigths_openness=number_openness,
weigths_conscientiousness=number_conscientiousness,
weigths_extraversion=number_extraversion,
weigths_agreeableness=number_agreeableness,
weigths_non_neuroticism=number_non_neuroticism,
out=False,
)
df = apply_rounding_and_rename_columns(b5.df_files_ranking_)
df_hidden = df.drop(columns=config_data.Settings_SHORT_PROFESSIONAL_SKILLS)
df_hidden.to_csv(config_data.Filenames_POTENTIAL_CANDIDATES)
df_hidden.reset_index(inplace=True)
person_id = (
int(df_hidden.iloc[0][config_data.Dataframes_PT_SCORES[0][0]]) - 1
)
person_metadata = create_person_metadata(person_id, files, video_metadata)
existing_tuple = (
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_POTENTIAL_CANDIDATES,
"single",
[".csv"],
config_data.OtherMessages_EXPORT_PG,
True,
False,
True,
"csv-container",
),
gr.Accordion(visible=False),
gr.HTML(visible=False),
dataframe(visible=False),
gr.Column(visible=True),
video_create_ui(
value=files[person_id],
file_name=Path(files[person_id]).name,
label="Best Person ID - " + str(person_id + 1),
visible=True,
elem_classes="video-sorted-container",
),
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False),
)
return existing_tuple[:-1] + person_metadata + existing_tuple[-1:]
elif 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=False,
)
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)
df_hidden = df.drop(
columns=config_data.Settings_SHORT_PROFESSIONAL_SKILLS
+ 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][config_data.Dataframes_PT_SCORES[0][0]]) - 1
person_metadata = create_person_metadata(person_id, files, video_metadata)
existing_tuple = (
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",
),
gr.Accordion(visible=False),
gr.HTML(visible=False),
dataframe(visible=False),
gr.Column(visible=True),
video_create_ui(
value=files[person_id],
file_name=Path(files[person_id]).name,
label="Best Person ID - " + str(person_id + 1),
visible=True,
elem_classes="video-sorted-container",
),
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False),
)
return existing_tuple[:-1] + person_metadata + existing_tuple[-1:]
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, ["ID"]
)
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,
)
df = apply_rounding_and_rename_columns(b5.df_files_colleague_)
df_hidden = df.drop(columns=config_data.Settings_SHORT_PROFESSIONAL_SKILLS)
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][config_data.Dataframes_PT_SCORES[0][0]]) - 1
person_metadata = create_person_metadata(person_id, files, video_metadata)
existing_tuple = (
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",
),
gr.Accordion(visible=False),
gr.HTML(visible=False),
dataframe(visible=False),
gr.Column(visible=True),
video_create_ui(
value=files[person_id],
file_name=Path(files[person_id]).name,
label="Best Person ID - " + str(person_id + 1),
visible=True,
elem_classes="video-sorted-container",
),
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False),
)
return existing_tuple[:-1] + person_metadata + existing_tuple[-1:]
elif (
practical_subtasks.lower() == "car characteristics"
or practical_subtasks.lower() == "mobile device application categories"
or practical_subtasks.lower() == "clothing style correlation"
):
if practical_subtasks.lower() == "car characteristics":
df_correlation_coefficients = read_csv_file(
config_data.Links_CAR_CHARACTERISTICS,
["Style and performance", "Safety and practicality"],
)
elif practical_subtasks.lower() == "mobile device application categories":
df_correlation_coefficients = read_csv_file(
config_data.Links_MDA_CATEGORIES
)
elif practical_subtasks.lower() == "clothing style correlation":
df_correlation_coefficients = read_csv_file(config_data.Links_CLOTHING_SC)
pt_scores_copy = pt_scores.iloc[:, 1:].copy()
preprocess_scores_df(pt_scores_copy, config_data.Dataframes_PT_SCORES[0][0])
b5._priority_calculation(
df_files=pt_scores_copy,
correlation_coefficients=df_correlation_coefficients,
col_name_ocean="Trait",
threshold=threshold_consumer_preferences,
number_priority=number_priority,
number_importance_traits=number_importance_traits,
out=False,
)
df_files_priority = b5.df_files_priority_.copy()
df_files_priority.reset_index(inplace=True)
df = apply_rounding_and_rename_columns(df_files_priority.iloc[:, 1:])
preprocess_scores_df(df, config_data.Dataframes_PT_SCORES[0][0])
df_hidden = df.drop(columns=config_data.Settings_SHORT_PROFESSIONAL_SKILLS)
df_hidden.to_csv(consumer_preferences(practical_subtasks))
df_hidden.reset_index(inplace=True)
person_id = int(df_hidden.iloc[0][config_data.Dataframes_PT_SCORES[0][0]]) - 1
person_metadata = create_person_metadata(person_id, files, video_metadata)
existing_tuple = (
gr.Row(visible=True),
gr.Column(visible=True),
dataframe(
headers=df_hidden.columns.tolist(),
values=df_hidden.values.tolist(),
visible=True,
),
files_create_ui(
consumer_preferences(practical_subtasks),
"single",
[".csv"],
config_data.OtherMessages_EXPORT_CP,
True,
False,
True,
"csv-container",
),
gr.Accordion(visible=False),
gr.HTML(visible=False),
dataframe(visible=False),
gr.Column(visible=True),
video_create_ui(
value=files[person_id],
file_name=Path(files[person_id]).name,
label="Best Person ID - " + str(person_id + 1),
visible=True,
elem_classes="video-sorted-container",
),
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, False),
)
return existing_tuple[:-1] + person_metadata + existing_tuple[-1:]
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",
),
gr.Accordion(visible=False),
gr.HTML(visible=False),
dataframe(visible=False),
gr.Column(visible=False),
video_create_ui(visible=False),
gr.Column(visible=False),
gr.Row(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
gr.Row(visible=False),
gr.Image(visible=False),
textbox_create_ui(visible=False),
html_message(config_data.InformationMessages_NOTI_IN_DEV, False, True),
)
|