QnA / src /view /view.py
YvesP's picture
added file management
7fea1f4
raw
history blame
2.64 kB
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