File size: 2,643 Bytes
7fea1f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from src.control.control import Controller


def run(ctrl: Controller, examples: {}):
    with gr.Blocks() as qna:
        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=list(examples.keys()),
                )
                output_text_comp = gr.Textbox(
                    label="La réponse automatique",
                    lines=12,
                    max_lines=12,
                    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_fr=input_text_)
            source_labels = [s.distance_str + ' ' + s.index + ' ' + 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_):
            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])

    return qna