First commit
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
# import base64
|
3 |
+
import gradio as gr
|
4 |
+
from src.rag import Rag
|
5 |
+
from src.amodel import ModelType
|
6 |
+
|
7 |
+
STORE_DIR = "./files/rag_app" # Le répertoire de la base
|
8 |
+
MAX_DOCS = 4 # Le nombre max de documents dans la base
|
9 |
+
|
10 |
+
def main():
|
11 |
+
# Création du rag
|
12 |
+
rag:Rag = Rag(ModelType.MTHUGGINGFACE, store_dir=STORE_DIR)
|
13 |
+
# Reset de la base à chaque démarrage du serveur
|
14 |
+
rag.reset_store()
|
15 |
+
|
16 |
+
# UI
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
|
19 |
+
def upload_file(file_path):
|
20 |
+
# Récupérer la liste des collections du store
|
21 |
+
names = list(rag.emb_store.get_collection_names())
|
22 |
+
# Supprimer la première s'il y en a déjà 4 (le max)
|
23 |
+
if len(names) == MAX_DOCS:
|
24 |
+
rag.delete_collection(names[0])
|
25 |
+
# Ajouter le pdf
|
26 |
+
name:str = Path(file_path).name
|
27 |
+
rag.add_pdf_to_store(file_name=file_path, collection_name=name)
|
28 |
+
# Mettre à jour la liste des noms de collections
|
29 |
+
names = list(rag.emb_store.get_collection_names())
|
30 |
+
combo.choices = names
|
31 |
+
return gr.update(choices=names, value=name, interactive=True), gr.update(value="")
|
32 |
+
|
33 |
+
def ask_rag(question:str, choice:str):
|
34 |
+
col_name:str = choice
|
35 |
+
if col_name == None:
|
36 |
+
return "Aucun pdf actif, veuillez en uploader un !"
|
37 |
+
prompt, resp, sources, ids = rag.ask_rag(question, col_name)
|
38 |
+
# print("choice:" + col_name, "RAG:" + resp)
|
39 |
+
return resp
|
40 |
+
|
41 |
+
def on_temperature_change(temp):
|
42 |
+
rag.set_temperature(temp)
|
43 |
+
|
44 |
+
|
45 |
+
with gr.Tab("RAG"):
|
46 |
+
# with gr.Row():
|
47 |
+
gr.Image("./files/drane.jpg", height=100, show_download_button=False, show_fullscreen_button=False, show_label=False)
|
48 |
+
# Le combo qui permet le choix du pdf
|
49 |
+
names:list[str] = rag.emb_store.get_collection_names()
|
50 |
+
combo = gr.Dropdown(names, label="PDFs", multiselect=False)
|
51 |
+
# Le button qui permet d'uploader un pdf
|
52 |
+
upload_button = gr.UploadButton("Clique pour uploader un pdf", file_types=[".pdf"], file_count="single")
|
53 |
+
|
54 |
+
# La zone où on pose une question au RAG
|
55 |
+
ask_input = gr.Text(label="Pose une question à ton pdf")
|
56 |
+
# La réponse du RAG (Markdown pour afficher les formules .tex)
|
57 |
+
rag_output = gr.Markdown(label="Réponse")
|
58 |
+
|
59 |
+
with gr.Tab("Réglages"):
|
60 |
+
gr.Markdown("## Modèles:")
|
61 |
+
gr.Markdown("- " + rag.get_llm_name())
|
62 |
+
gr.Markdown("- " + rag.get_feature_name())
|
63 |
+
temperature_slider = gr.Slider(minimum=0,
|
64 |
+
maximum=1.0,
|
65 |
+
value=0.5,
|
66 |
+
step=0.1,
|
67 |
+
label="Température")
|
68 |
+
|
69 |
+
# Réponses aux évènements
|
70 |
+
upload_button.upload(fn=upload_file, inputs=upload_button, outputs=[combo, rag_output], show_progress="full")
|
71 |
+
ask_input.submit(fn=ask_rag, inputs=[ask_input, combo], outputs=rag_output)
|
72 |
+
temperature_slider.change(fn=on_temperature_change, inputs=temperature_slider)
|
73 |
+
|
74 |
+
# demo.launch(allowed_paths=["./file/"], share=True)
|
75 |
+
demo.launch(allowed_paths=["./file/"])
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
main()
|