File size: 2,693 Bytes
c0f084a
 
 
 
 
 
 
 
b322173
 
c0f084a
13aeb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0f084a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b322173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
File: utils.py
Author: Elena Ryumina and Dmitry Ryumin
Description: Utility functions.
License: MIT License
"""

import pandas as pd
import subprocess
from pathlib import Path

# Importing necessary components for the Gradio app
from app.config import config_data


def get_language_settings(language):
    language_mappings = {
        "english": (0, config_data.Settings_LANGUAGES_EN),
        "английский": (0, config_data.Settings_LANGUAGES_EN),
        "russian": (1, config_data.Settings_LANGUAGES_RU),
        "русский": (1, config_data.Settings_LANGUAGES_RU),
    }

    normalized_language = language.lower()

    lang_id, choices = language_mappings.get(
        normalized_language, (0, config_data.Settings_LANGUAGES_EN)
    )

    return lang_id, choices


def preprocess_scores_df(df, name):
    df.index.name = name
    df.index += 1
    df.index = df.index.map(str)

    return df


def read_csv_file(file_path, drop_columns=[]):
    df = pd.read_csv(file_path)

    if len(drop_columns) != 0:
        df = pd.DataFrame(df.drop(drop_columns, axis=1))

    return preprocess_scores_df(df, "ID")


def round_numeric_values(x):
    if isinstance(x, (int, float)):
        return round(x, 3)

    return x


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].applymap(
        round_numeric_values
    )

    return df_rounded


def extract_profession_weights(df, dropdown_candidates):
    try:
        weights_professions = df.loc[df["Profession"] == dropdown_candidates, :].values[
            0
        ][1:]
        interactive_professions = False
    except Exception:
        weights_professions = [0] * 5
        interactive_professions = True
    else:
        weights_professions = list(map(int, weights_professions))

    return weights_professions, interactive_professions


def webm2mp4(input_file):
    input_path = Path(input_file)
    output_path = input_path.with_suffix(".mp4")

    if not output_path.exists():
        subprocess.run(
            [
                "ffmpeg",
                "-i",
                str(input_path),
                "-c:v",
                "copy",
                "-c:a",
                "aac",
                "-strict",
                "experimental",
                str(output_path),
            ],
            check=True,
        )

    return str(output_path)