|
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() |