File size: 4,884 Bytes
e6dc8c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import shutil
import json
from ml import VacancyAnalyzer


class GlobalState:
    """
    Class to store global variables
    """
    result_file_path = os.path.join(os.path.dirname(__file__), 'result/archive.json')
    result_dir = os.path.join(os.path.dirname(__file__), 'result')
    bert_path = os.path.join(os.path.dirname(__file__), 'tiny.pt')
    catboost_path = os.path.join(os.path.dirname(__file__), 'best_cat.joblib')
    conv_classes = {0: 'low',
                    1: 'middle',
                    2: 'high'
                    }
    default_data = {'id': 'a0000',
                    'emp_brand': '',
                    'mandatory': '',
                    'additional': '',
                    'comp_stages': '',
                    'work_conditions': '',
                    'conversion': 0,
                    'conversion_class': 'unknown'
                    }
    data = None


def cid(txt):
    GlobalState.data['id'] = txt


def cbrand(txt):
    GlobalState.data['emp_brand'] = txt


def cmand(txt):
    GlobalState.data['mandatory'] = txt


def cadd(txt):
    GlobalState.data['additional'] = txt


def ccomp(txt):
    GlobalState.data['comp_stages'] = txt


def ccond(txt):
    GlobalState.data['work_conditions'] = txt


def submit(chk):
    # print(GlobalState.data)
    return gr.update("Run!", visible=True)


def append_to_json(_dict, path):
    with open(path, 'ab+') as f:
        f.seek(0, 2)
        if f.tell() == 0:
            f.write(json.dumps([_dict]).encode())
        else:
            f.seek(-1, 2)
            f.truncate()
            f.write(' , '.encode())
            f.write(json.dumps(_dict).encode())
            f.write(']'.encode())


def predict(btn):
    analyzer = VacancyAnalyzer(GlobalState.bert_path, GlobalState.catboost_path, GlobalState.data)
    status, result = analyzer.classify()
    gr.Info(status)
    if result != 'unknown':
        result = GlobalState.conv_classes[int(result[0])]
    out_2 = f'Predicted by vacancy description conversion - {result}'
    GlobalState.data['conversion_class'] = result
    fid = GlobalState.result_file_path
    append_to_json(GlobalState.data, fid)
    GlobalState.data = GlobalState.default_data
    link = GlobalState.result_file_path
    return gr.update(value=out_2), gr.update(link="/file=" + link, visible=True)


def save(btn):
    link = GlobalState.result_file_path
    return gr.update(link="/file=" + link)


def main():
    shutil.rmtree(os.path.join(os.path.dirname(__file__), 'result/'), ignore_errors=True)
    os.mkdir(os.path.join(os.path.dirname(__file__), 'result/'))
    GlobalState.data = GlobalState.default_data
    with gr.Blocks() as demo:
        with gr.Tab("Load"):
            with gr.Row():
                gr.Markdown(
                    """
                    # Input the text description of the position
                    # 👾👾👾 Then press **Run!** 👾👾👾
                    """)
            with gr.Row():
                with gr.Column():
                    with gr.Row():
                        brand = gr.Textbox(label='Company name', value=None)
                    with gr.Row():
                        vid = gr.Textbox(label='Position id', value=None)
                    with gr.Row():
                        req = gr.Textbox(label='Mandatory')
                with gr.Column():
                    with gr.Row():
                        add = gr.Textbox(label='Additional')
                    with gr.Row():
                        comp = gr.Textbox(label='Competition stage')
                    with gr.Row():
                        cond = gr.Textbox(label='Work conditions')

                with gr.Column():
                    with gr.Row():
                        with gr.Column():
                            ready = gr.Checkbox(label='Data Filled')
                        with gr.Column():
                            process_button = gr.Button("Run!", visible=False, interactive=True)
                    with gr.Row():
                        output_2 = gr.Textbox(label='LLM Result')
                    with gr.Row():
                        download_button = gr.Button("JSON Archive", visible=False)

        brand.change(cbrand, inputs=[brand])
        vid.change(cid, inputs=[vid])
        req.change(cmand, inputs=[req])
        add.change(cadd, inputs=[add])
        comp.change(ccomp, inputs=[comp])
        cond.change(ccond, inputs=[cond])
        ready.change(submit, inputs=[ready], outputs=[process_button])
        process_button.click(predict, inputs=[process_button], outputs=[output_2, download_button],
                             show_progress='full')
        download_button.click(save, inputs=[download_button], outputs=[download_button])

    demo.launch(share=True, allowed_paths=[GlobalState.result_dir])


if __name__ == "__main__":
    main()