Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,68 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
from sentence_transformers import SentenceTransformer
|
3 |
-
import faiss
|
4 |
import numpy as np
|
|
|
|
|
5 |
from transformers import pipeline
|
6 |
import gradio as gr
|
7 |
-
import os
|
8 |
|
9 |
-
#
|
10 |
-
def
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
data = pd.read_excel(file_path)
|
13 |
-
data = data[['Product_name', 'price']].
|
14 |
-
data['combined'] = data.apply(
|
15 |
lambda row: f"Product: {row['Product_name']} | Price: {row['price']}", axis=1
|
16 |
)
|
17 |
return data
|
18 |
|
19 |
-
#
|
20 |
-
def
|
21 |
-
|
22 |
-
embeddings = model.encode(data['combined'].tolist())
|
23 |
-
return embeddings, model
|
24 |
-
|
25 |
-
# 3. Créer l'index FAISS
|
26 |
-
def create_faiss_index(embeddings):
|
27 |
-
dimension = embeddings.shape[1]
|
28 |
-
index = faiss.IndexFlatL2(dimension)
|
29 |
-
index.add(embeddings)
|
30 |
-
return index
|
31 |
-
|
32 |
-
# 4. Rechercher dans FAISS
|
33 |
-
def query_faiss_index(query, model, index, data, top_k=5):
|
34 |
-
query_embedding = model.encode([query])
|
35 |
distances, indices = index.search(query_embedding, top_k)
|
36 |
results = [data['combined'].iloc[idx] for idx in indices[0]]
|
37 |
return results
|
38 |
|
39 |
-
#
|
40 |
-
def
|
41 |
-
prompt = f"Given the following context, answer the question:\n\nContext
|
42 |
-
response =
|
43 |
return response[0]['generated_text']
|
44 |
|
45 |
-
#
|
46 |
-
def main(
|
47 |
-
# Charger et
|
48 |
-
|
49 |
-
|
50 |
-
# Générer les embeddings et créer l'index FAISS
|
51 |
-
embeddings, embedding_model = generate_embeddings(data)
|
52 |
-
index = create_faiss_index(embeddings)
|
53 |
|
54 |
-
# Charger le
|
55 |
-
|
56 |
|
57 |
-
# Interface Gradio
|
58 |
-
def
|
59 |
-
#
|
60 |
-
context = query_faiss_index(query,
|
61 |
context_text = "\n".join(context)
|
62 |
-
# Générer une réponse à
|
63 |
-
answer =
|
64 |
return answer
|
65 |
|
66 |
-
# Lancer l'
|
67 |
-
interface = gr.Interface(fn=
|
68 |
interface.launch()
|
69 |
|
70 |
-
# Exemple d'
|
71 |
if __name__ == "__main__":
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
+
import faiss
|
3 |
+
import pandas as pd
|
4 |
from transformers import pipeline
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
+
# Charger les embeddings et l'index FAISS
|
8 |
+
def load_embeddings_and_index(embeddings_file, index_file):
|
9 |
+
embeddings = np.load(embeddings_file)
|
10 |
+
index = faiss.read_index(index_file)
|
11 |
+
return embeddings, index
|
12 |
+
|
13 |
+
# Charger les données sources
|
14 |
+
def load_data(file_path):
|
15 |
data = pd.read_excel(file_path)
|
16 |
+
data['combined'] = data[['Product_name', 'price']].apply(
|
|
|
17 |
lambda row: f"Product: {row['Product_name']} | Price: {row['price']}", axis=1
|
18 |
)
|
19 |
return data
|
20 |
|
21 |
+
# Effectuer une recherche dans l'index FAISS
|
22 |
+
def query_faiss_index(query, embeddings, index, data, embedding_model, top_k=5):
|
23 |
+
query_embedding = embedding_model.encode([query])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
distances, indices = index.search(query_embedding, top_k)
|
25 |
results = [data['combined'].iloc[idx] for idx in indices[0]]
|
26 |
return results
|
27 |
|
28 |
+
# Générer une réponse avec le modèle LLM
|
29 |
+
def generate_answer(query, context, llm_pipeline):
|
30 |
+
prompt = f"Given the following context, answer the question:\n\nContext:\n{context}\n\nQuestion: {query}\nAnswer:"
|
31 |
+
response = llm_pipeline(prompt, max_length=500, num_return_sequences=1)
|
32 |
return response[0]['generated_text']
|
33 |
|
34 |
+
# Interface principale
|
35 |
+
def main(embeddings_file, index_file, data_file):
|
36 |
+
# Charger les données et les fichiers nécessaires
|
37 |
+
embeddings, index = load_embeddings_and_index(embeddings_file, index_file)
|
38 |
+
data = load_data(data_file)
|
|
|
|
|
|
|
39 |
|
40 |
+
# Charger le modèle LLM
|
41 |
+
llm_pipeline = pipeline("text-generation", model="HuggingFaceH4/zypher-llm")
|
42 |
|
43 |
+
# Interface utilisateur avec Gradio
|
44 |
+
def gradio_interface(query):
|
45 |
+
# Recherche dans FAISS
|
46 |
+
context = query_faiss_index(query, embeddings, index, data, embedding_model, top_k=5)
|
47 |
context_text = "\n".join(context)
|
48 |
+
# Générer une réponse à l'aide du LLM
|
49 |
+
answer = generate_answer(query, context_text, llm_pipeline)
|
50 |
return answer
|
51 |
|
52 |
+
# Lancer l'application Gradio
|
53 |
+
interface = gr.Interface(fn=gradio_interface, inputs="text", outputs="text", title="RAG Chatbot")
|
54 |
interface.launch()
|
55 |
|
56 |
+
# Exemple d'exécution
|
57 |
if __name__ == "__main__":
|
58 |
+
# Fichiers nécessaires
|
59 |
+
embeddings_file = "embeddings.npy"
|
60 |
+
index_file = "faiss_index.bin"
|
61 |
+
data_file = "avito.xls"
|
62 |
+
|
63 |
+
# Charger le modèle d'embedding
|
64 |
+
from sentence_transformers import SentenceTransformer
|
65 |
+
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
66 |
+
|
67 |
+
# Lancer le programme
|
68 |
+
main(embeddings_file, index_file, data_file)
|