changed the structure
Browse files- app.py +91 -0
- src/control/control.py +49 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
+
import src.control as ctrl
|
5 |
+
|
6 |
+
|
7 |
+
"""
|
8 |
+
==================================
|
9 |
+
A. Component part
|
10 |
+
==================================
|
11 |
+
"""
|
12 |
+
|
13 |
+
with gr.Blocks() as hrqa:
|
14 |
+
|
15 |
+
with gr.Row():
|
16 |
+
|
17 |
+
with gr.Column():
|
18 |
+
pass
|
19 |
+
|
20 |
+
with gr.Column(scale=10):
|
21 |
+
"""
|
22 |
+
1. input docs components
|
23 |
+
"""
|
24 |
+
|
25 |
+
gr.Markdown("# Questions sur le vivre ensemble en entreprise")
|
26 |
+
|
27 |
+
input_text_comp = gr.Textbox(
|
28 |
+
label="",
|
29 |
+
lines=1,
|
30 |
+
max_lines=3,
|
31 |
+
interactive=True,
|
32 |
+
placeholder="Posez votre question ici",
|
33 |
+
)
|
34 |
+
input_example_comp = gr.Radio(
|
35 |
+
label="Examples de questions",
|
36 |
+
choices=["Remboursement de frais de voiture", "Recommandations de transport"],
|
37 |
+
)
|
38 |
+
output_text_comp = gr.Textbox(
|
39 |
+
label="La réponse automatique",
|
40 |
+
lines=2,
|
41 |
+
max_lines=10,
|
42 |
+
interactive=False,
|
43 |
+
visible=False,
|
44 |
+
)
|
45 |
+
sources_comp = gr.CheckboxGroup(
|
46 |
+
label="Documents sources",
|
47 |
+
visible=False,
|
48 |
+
interactive=False,
|
49 |
+
)
|
50 |
+
|
51 |
+
with gr.Column():
|
52 |
+
pass
|
53 |
+
|
54 |
+
|
55 |
+
def input_text_fn1():
|
56 |
+
update_ = {
|
57 |
+
output_text_comp: gr.update(visible=True),
|
58 |
+
}
|
59 |
+
return update_
|
60 |
+
|
61 |
+
def input_text_fn2(input_text_):
|
62 |
+
answer, sources = ctrl.get_response(query=input_text_)
|
63 |
+
source_labels = [s['distance']+' '+s['paragraph']+' '+s['title']+' from '+s['doc'] for s in sources]
|
64 |
+
update_ = {
|
65 |
+
output_text_comp: gr.update(value=answer),
|
66 |
+
sources_comp: gr.update(visible=True, choices=source_labels, value=source_labels)
|
67 |
+
}
|
68 |
+
return update_
|
69 |
+
|
70 |
+
|
71 |
+
def input_example_fn(input_example_):
|
72 |
+
examples = {
|
73 |
+
"Remboursement de frais de voiture": "Comment sont remboursés mes frais kilométriques sur mes trajets "
|
74 |
+
"professionnels?",
|
75 |
+
"Recommandations de transport": "Quelles sont les recommandations de l'entreprise? Vaut-il mieux voyager en "
|
76 |
+
"train ou en avion?"
|
77 |
+
}
|
78 |
+
update_ = {
|
79 |
+
input_text_comp: gr.update(value=examples[input_example_]),
|
80 |
+
output_text_comp: gr.update(visible=True),
|
81 |
+
}
|
82 |
+
return update_
|
83 |
+
|
84 |
+
input_text_comp\
|
85 |
+
.submit(input_text_fn1, inputs=[], outputs=[output_text_comp])\
|
86 |
+
.then(input_text_fn2, inputs=[input_text_comp], outputs=[output_text_comp, sources_comp])
|
87 |
+
input_example_comp\
|
88 |
+
.change(input_example_fn, inputs=[input_example_comp], outputs=[input_text_comp, output_text_comp])\
|
89 |
+
.then(input_text_fn2, inputs=[input_text_comp], outputs=[output_text_comp, sources_comp])
|
90 |
+
|
91 |
+
hrqa.queue().launch()
|
src/control/control.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import chromadb
|
2 |
+
|
3 |
+
import src.tools.retriever as rtrvr
|
4 |
+
import src.tools.llm as llm
|
5 |
+
from src.domain.doc import Doc
|
6 |
+
|
7 |
+
chroma_client = chromadb.Client()
|
8 |
+
|
9 |
+
plan_language = 'en'
|
10 |
+
content_language = 'en'
|
11 |
+
path_plan = '../data/business_trips_plan_until_9_en.docx'
|
12 |
+
path_content = '../data/business_trips_content_until_9_en.docx'
|
13 |
+
collection_name = "until_9"
|
14 |
+
|
15 |
+
doc_plan = Doc(path_plan)
|
16 |
+
doc_content = Doc(path_content)
|
17 |
+
collection_ = rtrvr.init_collections(chroma_client, doc_plan, doc_content, collection_name)
|
18 |
+
|
19 |
+
|
20 |
+
def get_response(query):
|
21 |
+
if plan_language == 'en':
|
22 |
+
query = llm.translate(query)
|
23 |
+
sources = rtrvr.similarity_search(collection=collection_, query=query)
|
24 |
+
sources = select_best_sources(sources)
|
25 |
+
sources_contents = [s['content'] for s in sources]
|
26 |
+
context = '\n'.join(sources_contents)
|
27 |
+
answer = llm.generate_paragraph(query=query, context=context, language=content_language)
|
28 |
+
if content_language == 'en':
|
29 |
+
answer = llm.translate(text=answer, language='fr')
|
30 |
+
return answer.lstrip(), sources
|
31 |
+
|
32 |
+
|
33 |
+
def select_best_sources(sources: [], delta_1_2=0.1, delta_1_n=0.25, absolute=1.1) -> []:
|
34 |
+
best_sources = []
|
35 |
+
for idx, s in enumerate(sources):
|
36 |
+
if idx == 0 \
|
37 |
+
or (s['distance_f'] - sources[idx - 1]['distance_f'] < delta_1_2
|
38 |
+
and s['distance_f'] - sources[0]['distance_f'] < delta_1_n) \
|
39 |
+
or s['distance_f'] < absolute:
|
40 |
+
best_sources.append(s)
|
41 |
+
return best_sources
|
42 |
+
|
43 |
+
|
44 |
+
q1 = "Comment sont remboursés mes frais kilométriques sur mes déplacements avec mon véhicule personnel?"
|
45 |
+
q2 = "Quels sont les moyens de transport recommandés par la société?"
|
46 |
+
q3 = "est-ce que mes billets de cinéma peuvent être remboursés?"
|
47 |
+
|
48 |
+
a2 = get_response(q3)
|
49 |
+
print(a2)
|