Spaces:
Build error
Build error
Upload 4 files
Browse files- README.md +7 -13
- app.py +13 -19
- rag_utils.py +21 -41
- requirements.txt +4 -3
README.md
CHANGED
|
@@ -1,16 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
title: EduPilot
|
| 3 |
-
emoji: 🎓
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: yellow
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: "4.20.0"
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
| 1 |
+
# EduPilot – Chatbot IA d'Orientation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
EduPilot est un chatbot éducatif qui répond à tes questions sur les formations, les écoles et les métiers.
|
| 4 |
|
| 5 |
+
💡 Fonctionne avec un moteur RAG (Retrieval Augmented Generation) et peut utiliser :
|
| 6 |
+
- `Mistral-7B` (si token fourni dans les secrets)
|
| 7 |
+
- Sinon, un modèle public comme `FLAN-T5`.
|
| 8 |
|
| 9 |
+
## Exemple de question :
|
| 10 |
+
> Quelle formation pour devenir psychologue ?
|
app.py
CHANGED
|
@@ -1,37 +1,31 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from rag_utils import load_faiss_index, get_embedding_model, query_index, nettoyer_context, generate_answer
|
| 4 |
|
| 5 |
-
# Chargement des données
|
| 6 |
index, documents = load_faiss_index()
|
| 7 |
embedder = get_embedding_model()
|
| 8 |
|
| 9 |
-
# Fonction pour traiter la question et générer une réponse
|
| 10 |
def respond(message, history):
|
|
|
|
| 11 |
context = query_index(message, index, documents, embedder)
|
| 12 |
cleaned_context = nettoyer_context("\n".join(context))
|
| 13 |
answer = generate_answer(message, cleaned_context)
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Interface Gradio
|
| 18 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="yellow")) as demo:
|
| 19 |
-
gr.Markdown("# 🎓 EduPilot
|
| 20 |
-
gr.Markdown("👋
|
| 21 |
|
| 22 |
chatbot = gr.Chatbot(label="Conseiller IA")
|
| 23 |
-
state = gr.State([])
|
| 24 |
-
|
| 25 |
with gr.Row():
|
| 26 |
-
msg = gr.Textbox(
|
| 27 |
-
|
| 28 |
-
show_label=False,
|
| 29 |
-
container=True,
|
| 30 |
-
scale=8
|
| 31 |
-
)
|
| 32 |
-
submit = gr.Button("Envoyer", scale=1)
|
| 33 |
|
| 34 |
-
|
| 35 |
msg.submit(respond, [msg, state], [msg, chatbot, state])
|
| 36 |
|
| 37 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rag_utils import load_faiss_index, get_embedding_model, query_index, nettoyer_context, generate_answer
|
| 3 |
|
|
|
|
| 4 |
index, documents = load_faiss_index()
|
| 5 |
embedder = get_embedding_model()
|
| 6 |
|
|
|
|
| 7 |
def respond(message, history):
|
| 8 |
+
try:
|
| 9 |
context = query_index(message, index, documents, embedder)
|
| 10 |
cleaned_context = nettoyer_context("\n".join(context))
|
| 11 |
answer = generate_answer(message, cleaned_context)
|
| 12 |
+
except Exception as e:
|
| 13 |
+
answer = f"❌ Erreur : {str(e)}"
|
| 14 |
+
history.append((message, answer))
|
| 15 |
+
return "", history
|
| 16 |
|
|
|
|
| 17 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="yellow")) as demo:
|
| 18 |
+
gr.Markdown("# 🎓 EduPilot – Chatbot d'Orientation IA")
|
| 19 |
+
gr.Markdown("👋 Bienvenue ! Je suis **EduPilot**, ton conseiller scolaire IA. Pose-moi une question sur les métiers ou les formations.")
|
| 20 |
|
| 21 |
chatbot = gr.Chatbot(label="Conseiller IA")
|
| 22 |
+
state = gr.State([])
|
| 23 |
+
|
| 24 |
with gr.Row():
|
| 25 |
+
msg = gr.Textbox(placeholder="Exemple : Comment devenir vétérinaire ?", show_label=False, scale=8)
|
| 26 |
+
btn = gr.Button("Envoyer", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
btn.click(respond, [msg, state], [msg, chatbot, state])
|
| 29 |
msg.submit(respond, [msg, state], [msg, chatbot, state])
|
| 30 |
|
| 31 |
+
demo.launch()
|
rag_utils.py
CHANGED
|
@@ -1,18 +1,25 @@
|
|
| 1 |
-
|
| 2 |
import faiss
|
| 3 |
import pickle
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
-
from transformers import AutoTokenizer
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
-
import os
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def load_faiss_index(index_path="faiss_index/faiss_index.faiss", doc_path="faiss_index/documents.pkl"):
|
| 18 |
index = faiss.read_index(index_path)
|
|
@@ -34,12 +41,7 @@ def nettoyer_context(context):
|
|
| 34 |
return context
|
| 35 |
|
| 36 |
def generate_answer(question, context):
|
| 37 |
-
|
| 38 |
-
MAX_NEW_TOKENS = 300
|
| 39 |
-
MAX_PROMPT_TOKENS = MAX_TOKENS_TOTAL - MAX_NEW_TOKENS
|
| 40 |
-
|
| 41 |
-
# Construction initiale du prompt
|
| 42 |
-
base_prompt = f"""Voici des informations sur des établissements et formations :
|
| 43 |
|
| 44 |
{context}
|
| 45 |
|
|
@@ -48,31 +50,9 @@ Formule ta réponse comme un conseiller d’orientation bienveillant, de manièr
|
|
| 48 |
Question : {question}
|
| 49 |
Réponse :"""
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
truncated_context = tokenizer.decode(context_tokens[:keep_tokens])
|
| 58 |
-
|
| 59 |
-
# Reconstruire le prompt avec contexte réduit
|
| 60 |
-
base_prompt = f"""Voici des informations sur des établissements et formations :
|
| 61 |
-
|
| 62 |
-
{truncated_context}
|
| 63 |
-
|
| 64 |
-
Formule ta réponse comme un conseiller d’orientation bienveillant, de manière fluide et naturelle.
|
| 65 |
-
|
| 66 |
-
Question : {question}
|
| 67 |
-
Réponse :"""
|
| 68 |
-
|
| 69 |
-
print("===== PROMPT ENVOYÉ =====")
|
| 70 |
-
print(base_prompt)
|
| 71 |
-
|
| 72 |
-
response = client.text_generation(prompt=base_prompt, max_new_tokens=MAX_NEW_TOKENS, timeout=30)
|
| 73 |
-
|
| 74 |
-
print("===== RÉPONSE REÇUE =====")
|
| 75 |
-
print(response)
|
| 76 |
-
|
| 77 |
-
return response # selon format du retour
|
| 78 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import faiss
|
| 3 |
import pickle
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from transformers import AutoTokenizer, pipeline
|
| 8 |
from huggingface_hub import InferenceClient
|
|
|
|
| 9 |
|
| 10 |
+
# Choix du modèle
|
| 11 |
+
HF_TOKEN = os.environ.get("edup2")
|
| 12 |
|
| 13 |
+
if HF_TOKEN:
|
| 14 |
+
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 15 |
+
client = InferenceClient(MODEL_NAME, token=HF_TOKEN)
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
|
| 17 |
+
use_client = True
|
| 18 |
+
else:
|
| 19 |
+
MODEL_NAME = "google/flan-t5-base"
|
| 20 |
+
generator = pipeline("text2text-generation", model=MODEL_NAME)
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 22 |
+
use_client = False
|
| 23 |
|
| 24 |
def load_faiss_index(index_path="faiss_index/faiss_index.faiss", doc_path="faiss_index/documents.pkl"):
|
| 25 |
index = faiss.read_index(index_path)
|
|
|
|
| 41 |
return context
|
| 42 |
|
| 43 |
def generate_answer(question, context):
|
| 44 |
+
prompt = f"""Voici des informations sur des établissements et formations :
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
{context}
|
| 47 |
|
|
|
|
| 50 |
Question : {question}
|
| 51 |
Réponse :"""
|
| 52 |
|
| 53 |
+
if use_client:
|
| 54 |
+
response = client.text_generation(prompt=prompt, max_new_tokens=300, timeout=30)
|
| 55 |
+
return response
|
| 56 |
+
else:
|
| 57 |
+
result = generator(prompt, max_new_tokens=256, do_sample=True)
|
| 58 |
+
return result[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
-
sentence-transformers
|
| 3 |
faiss-cpu
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
|
|
|
| 2 |
faiss-cpu
|
| 3 |
+
sentence-transformers
|
| 4 |
+
transformers
|
| 5 |
+
huggingface_hub
|
| 6 |
+
numpy
|