Spaces:
Sleeping
Sleeping
Commit
·
2920e24
1
Parent(s):
94788eb
Move to faiss
Browse files- .gitattributes +1 -0
- app.py +21 -34
- requirements.txt +4 -2
.gitattributes
CHANGED
|
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
*.ann filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
*.ann filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.faiss filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import login, InferenceClient
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
from annoy import AnnoyIndex
|
| 7 |
|
| 8 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
| 9 |
|
|
@@ -24,14 +23,13 @@ Voici comment tu dois procéder :
|
|
| 24 |
* Si plusieurs articles pourraient s'appliquer, présente les différentes
|
| 25 |
interprétations possibles."""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
search_index_path.load('articles_path_embeds.ann')
|
| 35 |
|
| 36 |
system_prompt = """Tu es un assistant juridique spécialisé dans le Code de l'éducation français.
|
| 37 |
Ta mission est d'aider les utilisateurs à comprendre la législation en répondant à leurs questions.
|
|
@@ -47,25 +45,15 @@ Voici comment tu dois procéder :
|
|
| 47 |
* Si plusieurs articles pourraient s'appliquer, présente les différentes interprétations possibles."""
|
| 48 |
|
| 49 |
|
| 50 |
-
def query_rag(query, model
|
| 51 |
-
|
| 52 |
-
query_embed = co.embed(texts=[query],
|
| 53 |
-
model="embed-multilingual-v3.0",
|
| 54 |
-
input_type="search_document").embeddings
|
| 55 |
-
|
| 56 |
-
# Retrieve the nearest neighbors
|
| 57 |
-
index = search_index
|
| 58 |
-
if with_paths:
|
| 59 |
-
index = search_index_path
|
| 60 |
-
similar_item_ids = index.get_nns_by_vector(query_embed[0],10,
|
| 61 |
-
include_distances=True)
|
| 62 |
|
| 63 |
article_dict = {}
|
| 64 |
context_list = []
|
| 65 |
-
for
|
| 66 |
-
article =
|
| 67 |
-
context_list.append(article['
|
| 68 |
-
article_dict[article['article']] =
|
| 69 |
|
| 70 |
user = 'Question de l\'utilisateur : ' + query + '\nContexte législatif :\n' + '\n'.join(context_list)
|
| 71 |
|
|
@@ -76,17 +64,19 @@ def query_rag(query, model, with_paths=True):
|
|
| 76 |
messages=messages,
|
| 77 |
model=model,
|
| 78 |
max_tokens=1024)
|
|
|
|
| 79 |
return chat_completion.choices[0].message.content, article_dict
|
| 80 |
|
| 81 |
def create_context_response(response, article_dict):
|
| 82 |
response += '\n\n**Références**\n\n'
|
| 83 |
for i, article in enumerate(article_dict):
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
return response
|
| 87 |
|
| 88 |
-
def chat_interface(query, model
|
| 89 |
-
response, article_dict = query_rag(query, model
|
| 90 |
response_with_context = create_context_response(response, article_dict)
|
| 91 |
return response_with_context
|
| 92 |
|
|
@@ -116,15 +106,12 @@ with gr.Blocks(title="Assistant Juridique pour le Code de l'éducation (Beta)")
|
|
| 116 |
],
|
| 117 |
value="HuggingFaceH4/zephyr-7b-beta")
|
| 118 |
|
| 119 |
-
with_paths = gr.Checkbox(label="Utiliser les chemins d'accès aux articles dans le code pour interroger le modèle.",
|
| 120 |
-
value=True)
|
| 121 |
-
|
| 122 |
submit_button = gr.Button("Envoyer")
|
| 123 |
|
| 124 |
response_box = gr.Markdown()
|
| 125 |
|
| 126 |
submit_button.click(chat_interface,
|
| 127 |
-
inputs=[query_box, model
|
| 128 |
outputs=[response_box])
|
| 129 |
|
| 130 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import login, InferenceClient
|
| 3 |
import os
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
|
|
|
| 6 |
|
| 7 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
| 8 |
|
|
|
|
| 23 |
* Si plusieurs articles pourraient s'appliquer, présente les différentes
|
| 24 |
interprétations possibles."""
|
| 25 |
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
embeddings = HuggingFaceEmbeddings(model_name="OrdalieTech/Solon-embeddings-large-0.1")
|
| 29 |
+
|
| 30 |
+
db_code = FAISS.load_local("faiss_code_education",
|
| 31 |
+
embeddings,
|
| 32 |
+
allow_dangerous_deserialization=True)
|
|
|
|
| 33 |
|
| 34 |
system_prompt = """Tu es un assistant juridique spécialisé dans le Code de l'éducation français.
|
| 35 |
Ta mission est d'aider les utilisateurs à comprendre la législation en répondant à leurs questions.
|
|
|
|
| 45 |
* Si plusieurs articles pourraient s'appliquer, présente les différentes interprétations possibles."""
|
| 46 |
|
| 47 |
|
| 48 |
+
def query_rag(query, model):
|
| 49 |
+
docs = db_code.similarity_search(query, 10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
article_dict = {}
|
| 52 |
context_list = []
|
| 53 |
+
for doc in docs:
|
| 54 |
+
article = doc.metadata
|
| 55 |
+
context_list.append(article['chemin']+'\n'+article['texte']+'\n---\n')
|
| 56 |
+
article_dict[article['article']] = article
|
| 57 |
|
| 58 |
user = 'Question de l\'utilisateur : ' + query + '\nContexte législatif :\n' + '\n'.join(context_list)
|
| 59 |
|
|
|
|
| 64 |
messages=messages,
|
| 65 |
model=model,
|
| 66 |
max_tokens=1024)
|
| 67 |
+
|
| 68 |
return chat_completion.choices[0].message.content, article_dict
|
| 69 |
|
| 70 |
def create_context_response(response, article_dict):
|
| 71 |
response += '\n\n**Références**\n\n'
|
| 72 |
for i, article in enumerate(article_dict):
|
| 73 |
+
art = article_dict[article]
|
| 74 |
+
response += '* **' + art['chemin'] + '** : '+ art['texte'].replace('\n', '\n ')+'\n'
|
| 75 |
|
| 76 |
return response
|
| 77 |
|
| 78 |
+
def chat_interface(query, model):
|
| 79 |
+
response, article_dict = query_rag(query, model)
|
| 80 |
response_with_context = create_context_response(response, article_dict)
|
| 81 |
return response_with_context
|
| 82 |
|
|
|
|
| 106 |
],
|
| 107 |
value="HuggingFaceH4/zephyr-7b-beta")
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
submit_button = gr.Button("Envoyer")
|
| 110 |
|
| 111 |
response_box = gr.Markdown()
|
| 112 |
|
| 113 |
submit_button.click(chat_interface,
|
| 114 |
+
inputs=[query_box, model],
|
| 115 |
outputs=[response_box])
|
| 116 |
|
| 117 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
huggingface_hub==0.22.2
|
| 2 |
annoy
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
| 1 |
huggingface_hub==0.22.2
|
| 2 |
annoy
|
| 3 |
+
faiss
|
| 4 |
+
langchain_community
|
| 5 |
+
langchain
|
| 6 |
+
sentence_transformers
|