Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- .gitattributes +1 -0
- app.py +16 -0
- faiss_index/documents.pkl +3 -0
- faiss_index/faiss_index.faiss +3 -0
- rag_utils_unsloth.py +42 -0
- requirements.txt +5 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip 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
|
|
|
|
|
|
| 33 |
*.zip 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 |
+
faiss_index/faiss_index.faiss filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from rag_utils_unsloth import load_faiss_index, get_embedding_model, query_index, generate_answer
|
| 3 |
+
|
| 4 |
+
st.title("🎓 EduPilot (Unsloth + Mistral 4bit CPU)")
|
| 5 |
+
|
| 6 |
+
index, documents = load_faiss_index()
|
| 7 |
+
model_embed = get_embedding_model()
|
| 8 |
+
|
| 9 |
+
user_input = st.text_input("Pose ta question ici :")
|
| 10 |
+
|
| 11 |
+
if user_input:
|
| 12 |
+
top_docs = query_index(user_input, index, documents, model_embed)
|
| 13 |
+
context = "\n".join(top_docs)
|
| 14 |
+
response = generate_answer(user_input, context)
|
| 15 |
+
st.markdown("### ✨ Réponse du chatbot :")
|
| 16 |
+
st.write(response)
|
faiss_index/documents.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4cc6100b51468166d2e0b5e0ca119f239e648cb5d539dd256dc886ef39f45f46
|
| 3 |
+
size 36366182
|
faiss_index/faiss_index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:43efadcf8c063cedf2414c75df1dd801404c0ba263b0e35de8f21c25436d0694
|
| 3 |
+
size 165167661
|
rag_utils_unsloth.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import faiss
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from unsloth import FastLanguageModel
|
| 9 |
+
|
| 10 |
+
def load_faiss_index(index_path="faiss_index/faiss_index.faiss", doc_path="faiss_index/documents.pkl"):
|
| 11 |
+
index = faiss.read_index(index_path)
|
| 12 |
+
with open(doc_path, "rb") as f:
|
| 13 |
+
documents = pickle.load(f)
|
| 14 |
+
return index, documents
|
| 15 |
+
|
| 16 |
+
def get_embedding_model():
|
| 17 |
+
return SentenceTransformer("sentence-transformers/multi-qa-MiniLM-L6-cos-v1")
|
| 18 |
+
|
| 19 |
+
def query_index(question, index, documents, model, k=3):
|
| 20 |
+
question_embedding = model.encode([question])
|
| 21 |
+
_, indices = index.search(np.array(question_embedding).astype("float32"), k)
|
| 22 |
+
results = [documents[i] for i in indices[0]]
|
| 23 |
+
return results
|
| 24 |
+
|
| 25 |
+
def generate_answer(question, context):
|
| 26 |
+
model_id = "unsloth/mistral-7b-instruct-v0.1-bnb-4bit"
|
| 27 |
+
|
| 28 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 29 |
+
model_name=model_id,
|
| 30 |
+
max_seq_length=4096,
|
| 31 |
+
dtype="float32", # pour CPU uniquement
|
| 32 |
+
load_in_4bit=True,
|
| 33 |
+
device_map="auto"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 37 |
+
|
| 38 |
+
prompt = f"Voici un contexte :\n{context}\n\nQuestion : {question}\nRéponse :"
|
| 39 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(model.device)
|
| 40 |
+
|
| 41 |
+
outputs = model.generate(**inputs, max_new_tokens=256, pad_token_id=tokenizer.eos_token_id)
|
| 42 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
faiss-cpu
|
| 3 |
+
sentence-transformers
|
| 4 |
+
unsloth
|
| 5 |
+
torch
|