File size: 2,912 Bytes
de09bee
 
 
01b4d04
de09bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr


import src.control.control as ctrl


"""
==================================
A. Component part
==================================
"""

with gr.Blocks() as hrqa:

    with gr.Row():

        with gr.Column():
            pass

        with gr.Column(scale=10):
            """
            1. input docs components
            """

            gr.Markdown("# Questions sur le vivre ensemble en entreprise")

            input_text_comp = gr.Textbox(
                label="",
                lines=1,
                max_lines=3,
                interactive=True,
                placeholder="Posez votre question ici",
            )
            input_example_comp = gr.Radio(
                label="Examples de questions",
                choices=["Remboursement de frais de voiture", "Recommandations de transport"],
            )
            output_text_comp = gr.Textbox(
                label="La réponse automatique",
                lines=2,
                max_lines=10,
                interactive=False,
                visible=False,
            )
            sources_comp = gr.CheckboxGroup(
                label="Documents sources",
                visible=False,
                interactive=False,
            )

        with gr.Column():
            pass


    def input_text_fn1():
        update_ = {
            output_text_comp: gr.update(visible=True),
        }
        return update_

    def input_text_fn2(input_text_):
        answer, sources = ctrl.get_response(query=input_text_)
        source_labels = [s['distance']+' '+s['paragraph']+' '+s['title']+' from '+s['doc'] for s in sources]
        update_ = {
            output_text_comp: gr.update(value=answer),
            sources_comp: gr.update(visible=True, choices=source_labels, value=source_labels)
        }
        return update_


    def input_example_fn(input_example_):
        examples = {
            "Remboursement de frais de voiture": "Comment sont remboursés mes frais kilométriques sur mes trajets "
                                                 "professionnels?",
            "Recommandations de transport": "Quelles sont les recommandations de l'entreprise? Vaut-il mieux voyager en "
                                            "train ou en avion?"
        }
        update_ = {
            input_text_comp: gr.update(value=examples[input_example_]),
            output_text_comp: gr.update(visible=True),
        }
        return update_

    input_text_comp\
        .submit(input_text_fn1, inputs=[], outputs=[output_text_comp])\
        .then(input_text_fn2, inputs=[input_text_comp], outputs=[output_text_comp, sources_comp])
    input_example_comp\
        .change(input_example_fn, inputs=[input_example_comp], outputs=[input_text_comp, output_text_comp])\
        .then(input_text_fn2, inputs=[input_text_comp], outputs=[output_text_comp, sources_comp])

hrqa.queue().launch()