Spaces:
Sleeping
Sleeping
Delete edu_pilot_gradio_space_final/rag_utils.py
Browse files
edu_pilot_gradio_space_final/rag_utils.py
DELETED
@@ -1,46 +0,0 @@
|
|
1 |
-
import faiss
|
2 |
-
import pickle
|
3 |
-
import numpy as np
|
4 |
-
import re
|
5 |
-
from sentence_transformers import SentenceTransformer
|
6 |
-
from huggingface_hub import hf_hub_download
|
7 |
-
from llama_cpp import Llama
|
8 |
-
|
9 |
-
def load_faiss_index(index_path="faiss_index/faiss_index.faiss", doc_path="faiss_index/documents.pkl"):
|
10 |
-
index = faiss.read_index(index_path)
|
11 |
-
with open(doc_path, "rb") as f:
|
12 |
-
documents = pickle.load(f)
|
13 |
-
return index, documents
|
14 |
-
|
15 |
-
def get_embedding_model():
|
16 |
-
return SentenceTransformer("sentence-transformers/multi-qa-MiniLM-L6-cos-v1")
|
17 |
-
|
18 |
-
def query_index(question, index, documents, model, k=3):
|
19 |
-
question_embedding = model.encode([question])
|
20 |
-
_, indices = index.search(np.array(question_embedding).astype("float32"), k)
|
21 |
-
return [documents[i] for i in indices[0]]
|
22 |
-
|
23 |
-
def nettoyer_context(context):
|
24 |
-
context = re.sub(r"\[\'(.*?)\'\]", r"\1", context)
|
25 |
-
context = context.replace("None", "")
|
26 |
-
return context
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
import os
|
31 |
-
from huggingface_hub import InferenceClient
|
32 |
-
|
33 |
-
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1", token=os.environ.get("edup"))
|
34 |
-
|
35 |
-
def generate_answer(question, context):
|
36 |
-
prompt = f"""Voici des informations sur des établissements et formations :
|
37 |
-
|
38 |
-
{context}
|
39 |
-
|
40 |
-
Formule ta réponse comme un conseiller d’orientation bienveillant, de manière fluide et naturelle.
|
41 |
-
|
42 |
-
Question : {question}
|
43 |
-
Réponse :"""
|
44 |
-
|
45 |
-
response = client.text_generation(prompt, max_new_tokens=300)
|
46 |
-
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|