Spaces:
Runtime error
Runtime error
feat: Switch to French-specific models and refine prompt engineering for improved French RAG.
Browse files- app.py +22 -15
- data_cutter.py +1 -1
app.py
CHANGED
|
@@ -8,8 +8,7 @@ from data_cutter import create_db
|
|
| 8 |
|
| 9 |
# Constants
|
| 10 |
CHROMA_PATH = "chroma_db"
|
| 11 |
-
|
| 12 |
-
MODEL_ID = "moussaKam/t5-small-fr-summarization" # ~250MB, much more suitable for HF Spaces
|
| 13 |
|
| 14 |
print("🚀 Starting app...")
|
| 15 |
|
|
@@ -40,7 +39,7 @@ pipe = pipeline(
|
|
| 40 |
"text2text-generation",
|
| 41 |
model=model,
|
| 42 |
tokenizer=tokenizer,
|
| 43 |
-
max_new_tokens=
|
| 44 |
device=-1, # CPU
|
| 45 |
do_sample=True,
|
| 46 |
temperature=0.7,
|
|
@@ -54,27 +53,30 @@ def chat_function(message, history):
|
|
| 54 |
|
| 55 |
try:
|
| 56 |
# Search for relevant chunks
|
| 57 |
-
results = vectorstore.similarity_search(message, k=
|
| 58 |
context = "\n\n".join([doc.page_content for doc in results])
|
| 59 |
|
| 60 |
-
# Build prompt
|
| 61 |
-
prompt = f"""
|
| 62 |
{context}
|
| 63 |
|
| 64 |
-
Question
|
| 65 |
|
| 66 |
-
Répondez
|
| 67 |
-
Si la réponse n'est pas dans le contexte, dites "Je ne sais pas"."""
|
| 68 |
|
| 69 |
# Generate response
|
| 70 |
-
outputs = pipe(prompt, max_new_tokens=
|
| 71 |
response = outputs[0]['generated_text'].strip()
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
print(f"✅ Response generated: {response[:100]}...")
|
| 74 |
return response
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
error_msg = f"
|
| 78 |
print(f"❌ {error_msg}")
|
| 79 |
return error_msg
|
| 80 |
|
|
@@ -82,10 +84,15 @@ Si la réponse n'est pas dans le contexte, dites "Je ne sais pas"."""
|
|
| 82 |
# 4️⃣ Gradio Interface
|
| 83 |
demo = gr.ChatInterface(
|
| 84 |
fn=chat_function,
|
| 85 |
-
title="RAG Chat
|
| 86 |
-
description=f"
|
| 87 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
demo.launch()
|
|
|
|
| 8 |
|
| 9 |
# Constants
|
| 10 |
CHROMA_PATH = "chroma_db"
|
| 11 |
+
MODEL_ID = "google/flan-t5-small" # Better for French Q&A
|
|
|
|
| 12 |
|
| 13 |
print("🚀 Starting app...")
|
| 14 |
|
|
|
|
| 39 |
"text2text-generation",
|
| 40 |
model=model,
|
| 41 |
tokenizer=tokenizer,
|
| 42 |
+
max_new_tokens=300,
|
| 43 |
device=-1, # CPU
|
| 44 |
do_sample=True,
|
| 45 |
temperature=0.7,
|
|
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
# Search for relevant chunks
|
| 56 |
+
results = vectorstore.similarity_search(message, k=3)
|
| 57 |
context = "\n\n".join([doc.page_content for doc in results])
|
| 58 |
|
| 59 |
+
# Build prompt optimized for Flan-T5
|
| 60 |
+
prompt = f"""Contexte du document:
|
| 61 |
{context}
|
| 62 |
|
| 63 |
+
Question: {message}
|
| 64 |
|
| 65 |
+
Répondez en français en vous basant uniquement sur le contexte ci-dessus. Si l'information n'est pas dans le contexte, dites "Je ne trouve pas cette information dans le document"."""
|
|
|
|
| 66 |
|
| 67 |
# Generate response
|
| 68 |
+
outputs = pipe(prompt, max_new_tokens=300, num_return_sequences=1)
|
| 69 |
response = outputs[0]['generated_text'].strip()
|
| 70 |
|
| 71 |
+
# Fallback if response is too short or empty
|
| 72 |
+
if len(response) < 10:
|
| 73 |
+
response = "Je n'ai pas trouvé d'information pertinente dans le document pour répondre à votre question."
|
| 74 |
+
|
| 75 |
print(f"✅ Response generated: {response[:100]}...")
|
| 76 |
return response
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
error_msg = f"Erreur lors de la génération de la réponse: {str(e)}"
|
| 80 |
print(f"❌ {error_msg}")
|
| 81 |
return error_msg
|
| 82 |
|
|
|
|
| 84 |
# 4️⃣ Gradio Interface
|
| 85 |
demo = gr.ChatInterface(
|
| 86 |
fn=chat_function,
|
| 87 |
+
title="💬 RAG Chat - Documents en Français",
|
| 88 |
+
description=f"Posez des questions sur vos documents PDF en français. Propulsé par {MODEL_ID}.",
|
| 89 |
+
examples=[
|
| 90 |
+
"Quel est le sujet principal du document ?",
|
| 91 |
+
"Résume le contenu principal.",
|
| 92 |
+
"Quelles sont les informations importantes ?"
|
| 93 |
+
],
|
| 94 |
+
theme=gr.themes.Soft()
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
demo.launch()
|
data_cutter.py
CHANGED
|
@@ -58,7 +58,7 @@ def create_db():
|
|
| 58 |
|
| 59 |
print("\nCreating ChromaDB vector store with HuggingFace embeddings (all-MiniLM-L6-v2)...")
|
| 60 |
embeddings = HuggingFaceEmbeddings(
|
| 61 |
-
model_name="sentence-
|
| 62 |
)
|
| 63 |
|
| 64 |
vectorstore = Chroma.from_documents(
|
|
|
|
| 58 |
|
| 59 |
print("\nCreating ChromaDB vector store with HuggingFace embeddings (all-MiniLM-L6-v2)...")
|
| 60 |
embeddings = HuggingFaceEmbeddings(
|
| 61 |
+
model_name="dangvantuan/sentence-camembert-base" # French-specific embeddings
|
| 62 |
)
|
| 63 |
|
| 64 |
vectorstore = Chroma.from_documents(
|