Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .envrc +1 -0
- .gitignore +124 -0
- README.md +2 -8
- app.py +78 -0
- crawler.py +310 -0
- data/chunks.pkl +3 -0
- data/embeddings.pkl +3 -0
- rag.py +149 -0
- requirements.txt +5 -0
.envrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
source .envrc.private
|
.gitignore
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
*.egg-info/
|
| 24 |
+
.installed.cfg
|
| 25 |
+
*.egg
|
| 26 |
+
|
| 27 |
+
# PyInstaller
|
| 28 |
+
# Usually these files are written by a python script from a template
|
| 29 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 30 |
+
*.manifest
|
| 31 |
+
*.spec
|
| 32 |
+
|
| 33 |
+
# Installer logs
|
| 34 |
+
pip-log.txt
|
| 35 |
+
pip-delete-this-directory.txt
|
| 36 |
+
|
| 37 |
+
# Unit test / coverage reports
|
| 38 |
+
htmlcov/
|
| 39 |
+
.tox/
|
| 40 |
+
.nox/
|
| 41 |
+
.coverage
|
| 42 |
+
.coverage.*
|
| 43 |
+
.cache
|
| 44 |
+
nosetests.xml
|
| 45 |
+
coverage.xml
|
| 46 |
+
*.cover
|
| 47 |
+
*.py,cover
|
| 48 |
+
.hypothesis/
|
| 49 |
+
|
| 50 |
+
# Translations
|
| 51 |
+
*.mo
|
| 52 |
+
*.pot
|
| 53 |
+
|
| 54 |
+
# Django stuff:
|
| 55 |
+
*.log
|
| 56 |
+
local_settings.py
|
| 57 |
+
db.sqlite3
|
| 58 |
+
db.sqlite3-journal
|
| 59 |
+
|
| 60 |
+
# Flask stuff:
|
| 61 |
+
instance/
|
| 62 |
+
.webassets-cache
|
| 63 |
+
|
| 64 |
+
# Scrapy stuff:
|
| 65 |
+
.scrapy
|
| 66 |
+
|
| 67 |
+
# Sphinx documentation
|
| 68 |
+
docs/_build/
|
| 69 |
+
|
| 70 |
+
# PyBuilder
|
| 71 |
+
target/
|
| 72 |
+
|
| 73 |
+
# Jupyter Notebook
|
| 74 |
+
.ipynb_checkpoints
|
| 75 |
+
|
| 76 |
+
# IPython
|
| 77 |
+
profile_default/
|
| 78 |
+
ipython_config.py
|
| 79 |
+
|
| 80 |
+
# pyenv
|
| 81 |
+
.python-version
|
| 82 |
+
|
| 83 |
+
# celery beat schedule file
|
| 84 |
+
celerybeat-schedule
|
| 85 |
+
|
| 86 |
+
# SageMath parsed files
|
| 87 |
+
*.sage.py
|
| 88 |
+
|
| 89 |
+
# Environments
|
| 90 |
+
.env
|
| 91 |
+
.venv
|
| 92 |
+
env/
|
| 93 |
+
venv/
|
| 94 |
+
ENV/
|
| 95 |
+
env.bak/
|
| 96 |
+
venv.bak/
|
| 97 |
+
|
| 98 |
+
# Spyder project settings
|
| 99 |
+
.spyderproject
|
| 100 |
+
.spyderworkspace
|
| 101 |
+
|
| 102 |
+
# Rope project settings
|
| 103 |
+
.ropeproject
|
| 104 |
+
|
| 105 |
+
# mkdocs documentation
|
| 106 |
+
/site
|
| 107 |
+
|
| 108 |
+
# mypy
|
| 109 |
+
.mypy_cache/
|
| 110 |
+
.dmypy.json
|
| 111 |
+
dmypy.json
|
| 112 |
+
|
| 113 |
+
# Pyre type checker
|
| 114 |
+
.pyre/
|
| 115 |
+
|
| 116 |
+
# pytype static type analyzer
|
| 117 |
+
.pytype/
|
| 118 |
+
|
| 119 |
+
# Cython debug symbols
|
| 120 |
+
cython_debug/
|
| 121 |
+
|
| 122 |
+
.envrc.private
|
| 123 |
+
|
| 124 |
+
.gradio
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: purple
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.19.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: publik_rag
|
| 3 |
+
app_file: app.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.19.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pickle
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from crawler import ContentCrawler
|
| 5 |
+
from rag import RAGEngine
|
| 6 |
+
|
| 7 |
+
# Define file paths for the pickled chunks and embeddings
|
| 8 |
+
chunks = "./data/chunks.pkl"
|
| 9 |
+
embeddings = "./data/embeddings.pkl"
|
| 10 |
+
|
| 11 |
+
# Check if the chunks file exists; if not, crawl the website and save the results
|
| 12 |
+
if os.path.exists(chunks):
|
| 13 |
+
print("Loading chunks")
|
| 14 |
+
with open(chunks, "rb") as f:
|
| 15 |
+
results = pickle.load(f)
|
| 16 |
+
else:
|
| 17 |
+
print("Chunks file not found. Crawling the website...")
|
| 18 |
+
# Define the base URL and initialize the content crawler
|
| 19 |
+
base_url = "https://doc-publik.entrouvert.com/"
|
| 20 |
+
crawler = ContentCrawler(base_url)
|
| 21 |
+
results = crawler.crawl()
|
| 22 |
+
# Save the crawled chunks to a pickle file
|
| 23 |
+
with open(chunks, "wb") as f:
|
| 24 |
+
pickle.dump(results, f)
|
| 25 |
+
|
| 26 |
+
# Initialize the RAGEngine with the loaded chunks
|
| 27 |
+
rag_engine = RAGEngine(results)
|
| 28 |
+
|
| 29 |
+
# Check if the embeddings file exists; if not, create the embeddings and save them
|
| 30 |
+
if os.path.exists(embeddings):
|
| 31 |
+
print("Loading embeddings")
|
| 32 |
+
with open(embeddings, "rb") as f:
|
| 33 |
+
rag_engine.embeddings = pickle.load(f)
|
| 34 |
+
else:
|
| 35 |
+
print("Creating embeddings")
|
| 36 |
+
rag_engine.index_documents()
|
| 37 |
+
with open(embeddings, "wb") as f:
|
| 38 |
+
pickle.dump(rag_engine.embeddings, f)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Define a function to answer questions using the RAG engine.
|
| 42 |
+
# This function also retrieves the "urls" field and formats them as clickable Markdown links.
|
| 43 |
+
def answer_question(question):
|
| 44 |
+
# Affiche immédiatement un message de chargement
|
| 45 |
+
yield "Chargement en cours..."
|
| 46 |
+
try:
|
| 47 |
+
result = rag_engine.rag(question, top_k=5)
|
| 48 |
+
# Récupération de la réponse et des URLs associées
|
| 49 |
+
prompt = result.get("prompt", "")
|
| 50 |
+
response = result.get("response", "")
|
| 51 |
+
urls = result.get("urls", [])
|
| 52 |
+
|
| 53 |
+
# Formatage de la réponse avec les liens Markdown si des URLs sont présentes
|
| 54 |
+
if urls:
|
| 55 |
+
links_md = "\n".join([f"- [{url}]({url})" for url in urls])
|
| 56 |
+
markdown_output = f"{response}\n\n**Sources:**\n{links_md}"
|
| 57 |
+
else:
|
| 58 |
+
markdown_output = response
|
| 59 |
+
|
| 60 |
+
# Envoi de la réponse finale
|
| 61 |
+
yield markdown_output
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
# En cas d'erreur, affiche le message de l'exception
|
| 65 |
+
yield f"Une erreur est survenue: {str(e)}"
|
| 66 |
+
|
| 67 |
+
# Create a Gradio interface for the Q&A with Markdown formatted output and flagging disabled
|
| 68 |
+
iface = gr.Interface(
|
| 69 |
+
fn=answer_question,
|
| 70 |
+
inputs=gr.Textbox(label="Votre question"),
|
| 71 |
+
outputs=gr.Markdown(label="Réponse"),
|
| 72 |
+
title="Publik Q&A",
|
| 73 |
+
flagging_mode="never",
|
| 74 |
+
description="Poser des questions sur Publik",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Launch the Gradio interface
|
| 78 |
+
iface.launch()
|
crawler.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
+
from typing import Dict, List, Set, Tuple
|
| 3 |
+
from urllib.parse import urljoin, urlparse, urlunparse
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
from bs4 import BeautifulSoup, NavigableString, Tag
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class ContentCrawler:
|
| 10 |
+
def __init__(
|
| 11 |
+
self, base_url: str, ignore_prefixes: List[str] = None, max_length: int = 8000
|
| 12 |
+
):
|
| 13 |
+
"""
|
| 14 |
+
Initialize the crawler with the base URL, a list of URL prefixes to ignore, and the maximum chunk size.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
base_url: The website URL to crawl.
|
| 18 |
+
ignore_prefixes: List of URL path prefixes to ignore.
|
| 19 |
+
max_length: Maximum allowed size for a chunk.
|
| 20 |
+
"""
|
| 21 |
+
self.base_url = base_url
|
| 22 |
+
self.visited = set()
|
| 23 |
+
self.results = []
|
| 24 |
+
self.max_length = max_length
|
| 25 |
+
self.ignore_prefixes = ignore_prefixes or [
|
| 26 |
+
"manage/",
|
| 27 |
+
"password/",
|
| 28 |
+
"media/",
|
| 29 |
+
"notes-de-mises-a-jour/",
|
| 30 |
+
]
|
| 31 |
+
# Pour éviter les doublons de contenu
|
| 32 |
+
self.content_hashes = set()
|
| 33 |
+
|
| 34 |
+
def crawl(self) -> List[Dict[str, str]]:
|
| 35 |
+
"""
|
| 36 |
+
Recursively crawl the website starting from the homepage.
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
A list of dictionaries with keys 'url' and 'text' (in markdown format).
|
| 40 |
+
"""
|
| 41 |
+
try:
|
| 42 |
+
homepage_response = requests.get(self.base_url)
|
| 43 |
+
homepage_response.raise_for_status()
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error fetching homepage {self.base_url}: {e}")
|
| 46 |
+
return []
|
| 47 |
+
|
| 48 |
+
homepage_soup = BeautifulSoup(homepage_response.text, "html.parser")
|
| 49 |
+
initial_links = self._get_internal_links(homepage_soup)
|
| 50 |
+
|
| 51 |
+
# Utiliser un ensemble pour éviter les doublons d'URLs dans la file
|
| 52 |
+
queue = set()
|
| 53 |
+
for link in initial_links:
|
| 54 |
+
full_url = self._normalize_url(urljoin(self.base_url, link))
|
| 55 |
+
if full_url != self.base_url:
|
| 56 |
+
queue.add(full_url)
|
| 57 |
+
self.visited.add(full_url)
|
| 58 |
+
|
| 59 |
+
# Convertir en liste pour le traitement
|
| 60 |
+
queue_list = list(queue)
|
| 61 |
+
while queue_list:
|
| 62 |
+
current_url = queue_list.pop(0)
|
| 63 |
+
print(f"Processing {current_url}")
|
| 64 |
+
result, new_links = self._parse_page(current_url)
|
| 65 |
+
|
| 66 |
+
if result:
|
| 67 |
+
self.results.extend(result)
|
| 68 |
+
|
| 69 |
+
# Ajouter seulement les liens non visités
|
| 70 |
+
for link in new_links:
|
| 71 |
+
full_url = self._normalize_url(urljoin(self.base_url, link))
|
| 72 |
+
if full_url not in self.visited and full_url != self.base_url:
|
| 73 |
+
self.visited.add(full_url)
|
| 74 |
+
queue_list.append(full_url)
|
| 75 |
+
|
| 76 |
+
return self.results
|
| 77 |
+
|
| 78 |
+
def _normalize_url(self, url: str) -> str:
|
| 79 |
+
"""Normaliser l'URL en supprimant les fragments et paramètres de requête."""
|
| 80 |
+
parsed = urlparse(url)
|
| 81 |
+
# Supprimer fragment et query params
|
| 82 |
+
normalized = parsed._replace(fragment="", query="")
|
| 83 |
+
return urlunparse(normalized)
|
| 84 |
+
|
| 85 |
+
def _get_internal_links(self, soup: BeautifulSoup) -> Set[str]:
|
| 86 |
+
"""
|
| 87 |
+
Retrieve internal links from the BeautifulSoup object,
|
| 88 |
+
ignoring those whose path starts with any of the specified prefixes.
|
| 89 |
+
"""
|
| 90 |
+
links = set()
|
| 91 |
+
for a_tag in soup.find_all("a", href=True):
|
| 92 |
+
href = a_tag["href"]
|
| 93 |
+
if href.startswith("#") or href.startswith("javascript:"):
|
| 94 |
+
continue
|
| 95 |
+
|
| 96 |
+
parsed_href = urlparse(href)
|
| 97 |
+
path = parsed_href.path.lstrip("/")
|
| 98 |
+
|
| 99 |
+
if any(path.startswith(prefix) for prefix in self.ignore_prefixes):
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
+
# S'assurer que le lien est interne
|
| 103 |
+
is_internal = (
|
| 104 |
+
not parsed_href.netloc
|
| 105 |
+
or self.base_url in href
|
| 106 |
+
or parsed_href.netloc == urlparse(self.base_url).netloc
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
if is_internal:
|
| 110 |
+
links.add(href)
|
| 111 |
+
|
| 112 |
+
return links
|
| 113 |
+
|
| 114 |
+
def _parse_page(self, url: str) -> Tuple[List[Dict[str, str]], Set[str]]:
|
| 115 |
+
"""Parse une page et extrait son contenu ainsi que ses liens."""
|
| 116 |
+
try:
|
| 117 |
+
response = requests.get(url)
|
| 118 |
+
response.raise_for_status()
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Error fetching {url}: {e}")
|
| 121 |
+
return [], set()
|
| 122 |
+
|
| 123 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 124 |
+
|
| 125 |
+
# Trouver la div principale de contenu
|
| 126 |
+
content_div = soup.find(id="content")
|
| 127 |
+
if not content_div:
|
| 128 |
+
print(f"No content div found in {url}")
|
| 129 |
+
return [], self._get_internal_links(soup)
|
| 130 |
+
|
| 131 |
+
# Nettoyer le contenu
|
| 132 |
+
for script in content_div.find_all(["script", "style"]):
|
| 133 |
+
script.decompose()
|
| 134 |
+
|
| 135 |
+
# Récupérer le titre
|
| 136 |
+
h1_tag = content_div.find("h1")
|
| 137 |
+
page_title = h1_tag.get_text(strip=True) if h1_tag else ""
|
| 138 |
+
|
| 139 |
+
# Créer le markdown complet
|
| 140 |
+
markdown_content = self._extract_structured_content(content_div, page_title)
|
| 141 |
+
|
| 142 |
+
# Vérifier si le contenu est un doublon
|
| 143 |
+
content_hash = self._hash_content(markdown_content)
|
| 144 |
+
if content_hash in self.content_hashes:
|
| 145 |
+
print(f"Duplicate content skipped for {url}")
|
| 146 |
+
return [], self._get_internal_links(soup)
|
| 147 |
+
|
| 148 |
+
self.content_hashes.add(content_hash)
|
| 149 |
+
|
| 150 |
+
# Diviser en chunks si nécessaire
|
| 151 |
+
chunks = self._split_content(markdown_content)
|
| 152 |
+
|
| 153 |
+
# Créer la liste des résultats
|
| 154 |
+
results = []
|
| 155 |
+
for i, chunk in enumerate(chunks):
|
| 156 |
+
results.append({"url": f"{url}#chunk-{i+1}", "text": chunk})
|
| 157 |
+
|
| 158 |
+
return results, self._get_internal_links(soup)
|
| 159 |
+
|
| 160 |
+
def _extract_structured_content(self, content_div: Tag, page_title: str) -> str:
|
| 161 |
+
"""Extrait le contenu de manière structurée en respectant la hiérarchie de titres."""
|
| 162 |
+
lines = []
|
| 163 |
+
|
| 164 |
+
# Ajouter le titre principal
|
| 165 |
+
if page_title:
|
| 166 |
+
lines.append(f"# {page_title}")
|
| 167 |
+
|
| 168 |
+
# Identifier tous les titres et le contenu
|
| 169 |
+
current_element = content_div.find_next()
|
| 170 |
+
|
| 171 |
+
while current_element and current_element.parent == content_div:
|
| 172 |
+
if current_element.name in ["h1", "h2", "h3", "h4", "h5", "h6"]:
|
| 173 |
+
# Convertir le niveau de titre
|
| 174 |
+
level = int(current_element.name[1])
|
| 175 |
+
text = current_element.get_text(strip=True)
|
| 176 |
+
lines.append(f"{'#' * level} {text}")
|
| 177 |
+
else:
|
| 178 |
+
markdown = self._convert_element_to_markdown(current_element)
|
| 179 |
+
if markdown:
|
| 180 |
+
lines.append(markdown)
|
| 181 |
+
|
| 182 |
+
# Passer à l'élément suivant au même niveau
|
| 183 |
+
current_element = current_element.find_next_sibling()
|
| 184 |
+
|
| 185 |
+
return "\n\n".join(line for line in lines if line)
|
| 186 |
+
|
| 187 |
+
def _convert_element_to_markdown(self, element) -> str:
|
| 188 |
+
"""Convertit un élément HTML en markdown."""
|
| 189 |
+
if isinstance(element, NavigableString):
|
| 190 |
+
text = element.strip()
|
| 191 |
+
return text if text else ""
|
| 192 |
+
|
| 193 |
+
if isinstance(element, Tag):
|
| 194 |
+
if element.name in ["script", "style", "iframe"]:
|
| 195 |
+
return ""
|
| 196 |
+
|
| 197 |
+
if element.name == "p":
|
| 198 |
+
return element.get_text(strip=True)
|
| 199 |
+
|
| 200 |
+
elif element.name == "a" and element.get("href"):
|
| 201 |
+
text = element.get_text(strip=True)
|
| 202 |
+
href = element.get("href")
|
| 203 |
+
return f"[{text}]({href})"
|
| 204 |
+
|
| 205 |
+
elif element.name in ["ul", "ol"]:
|
| 206 |
+
items = []
|
| 207 |
+
for li in element.find_all("li", recursive=False):
|
| 208 |
+
text = li.get_text(strip=True)
|
| 209 |
+
if text:
|
| 210 |
+
items.append(f"* {text}")
|
| 211 |
+
return "\n".join(items)
|
| 212 |
+
|
| 213 |
+
elif element.name == "table":
|
| 214 |
+
# Extraction basique des tableaux
|
| 215 |
+
rows = []
|
| 216 |
+
for tr in element.find_all("tr"):
|
| 217 |
+
cols = []
|
| 218 |
+
for td in tr.find_all(["td", "th"]):
|
| 219 |
+
cols.append(td.get_text(strip=True))
|
| 220 |
+
rows.append(" | ".join(cols))
|
| 221 |
+
|
| 222 |
+
if rows:
|
| 223 |
+
# Ajouter la ligne de séparation après l'en-tête
|
| 224 |
+
if len(rows) > 1:
|
| 225 |
+
rows.insert(1, "-" * len(rows[0]))
|
| 226 |
+
return "\n".join(rows)
|
| 227 |
+
return ""
|
| 228 |
+
|
| 229 |
+
elif element.name in ["div", "section", "article"]:
|
| 230 |
+
parts = []
|
| 231 |
+
for child in element.children:
|
| 232 |
+
part = self._convert_element_to_markdown(child)
|
| 233 |
+
if part:
|
| 234 |
+
parts.append(part)
|
| 235 |
+
return "\n\n".join(parts)
|
| 236 |
+
|
| 237 |
+
else:
|
| 238 |
+
text = element.get_text(strip=True)
|
| 239 |
+
return text if text else ""
|
| 240 |
+
|
| 241 |
+
return ""
|
| 242 |
+
|
| 243 |
+
def _split_content(self, content: str) -> List[str]:
|
| 244 |
+
"""Divise le contenu en chunks de taille maximale."""
|
| 245 |
+
if len(content) <= self.max_length:
|
| 246 |
+
return [content]
|
| 247 |
+
|
| 248 |
+
# Extraction du titre principal pour le préserver dans chaque chunk
|
| 249 |
+
lines = content.split("\n\n")
|
| 250 |
+
main_title = lines[0] if lines and lines[0].startswith("# ") else ""
|
| 251 |
+
|
| 252 |
+
chunks = []
|
| 253 |
+
current_chunk = main_title
|
| 254 |
+
current_length = len(main_title)
|
| 255 |
+
|
| 256 |
+
for line in lines:
|
| 257 |
+
# Ignorer le titre principal déjà traité
|
| 258 |
+
if line == main_title:
|
| 259 |
+
continue
|
| 260 |
+
|
| 261 |
+
line_length = len(line)
|
| 262 |
+
|
| 263 |
+
# Si la ligne seule dépasse la taille max, on doit la diviser
|
| 264 |
+
if line_length > self.max_length:
|
| 265 |
+
# D'abord ajouter le chunk courant s'il y a du contenu
|
| 266 |
+
if current_length > len(main_title):
|
| 267 |
+
chunks.append(current_chunk)
|
| 268 |
+
|
| 269 |
+
# Diviser cette longue ligne en sous-parties
|
| 270 |
+
start = 0
|
| 271 |
+
while start < line_length:
|
| 272 |
+
part = line[start : start + self.max_length]
|
| 273 |
+
if main_title and not part.startswith("#"):
|
| 274 |
+
chunks.append(f"{main_title}\n\n{part}")
|
| 275 |
+
else:
|
| 276 |
+
chunks.append(part)
|
| 277 |
+
start += self.max_length
|
| 278 |
+
|
| 279 |
+
# Réinitialiser le chunk courant
|
| 280 |
+
current_chunk = main_title
|
| 281 |
+
current_length = len(main_title)
|
| 282 |
+
else:
|
| 283 |
+
# Si l'ajout de cette ligne dépasse la taille max, créer un nouveau chunk
|
| 284 |
+
if current_length + line_length + 4 > self.max_length: # +4 pour \n\n
|
| 285 |
+
chunks.append(current_chunk)
|
| 286 |
+
current_chunk = main_title
|
| 287 |
+
current_length = len(main_title)
|
| 288 |
+
|
| 289 |
+
if main_title and current_chunk:
|
| 290 |
+
current_chunk += "\n\n"
|
| 291 |
+
current_length += 2
|
| 292 |
+
|
| 293 |
+
# Ajouter la ligne au chunk courant
|
| 294 |
+
if current_chunk:
|
| 295 |
+
current_chunk += "\n\n" + line
|
| 296 |
+
current_length += line_length + 2
|
| 297 |
+
else:
|
| 298 |
+
current_chunk = line
|
| 299 |
+
current_length = line_length
|
| 300 |
+
|
| 301 |
+
# Ajouter le dernier chunk s'il reste du contenu
|
| 302 |
+
if current_length > len(main_title):
|
| 303 |
+
chunks.append(current_chunk)
|
| 304 |
+
|
| 305 |
+
return chunks
|
| 306 |
+
|
| 307 |
+
def _hash_content(self, content: str) -> str:
|
| 308 |
+
"""Crée un hash du contenu pour identifier les doublons."""
|
| 309 |
+
# Utiliser seulement le contenu principal (pas les URLs) pour la détection de doublons
|
| 310 |
+
return hashlib.md5(content.encode()).hexdigest()
|
data/chunks.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c623fccae7e8dd58d582bbd8afcee7f2e2ab8db6bf72c43bfbf12a6378463146
|
| 3 |
+
size 1259802
|
data/embeddings.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:79145630ff16a45dc12fa301cb21dd4ae3735f67d12030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 |
+
import torch
|
| 6 |
+
from transformers import AutoTokenizer, AutoModel
|
| 7 |
+
from huggingface_hub import InferenceClient
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class RAGEngine:
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
documents: List[Dict[str, str]],
|
| 14 |
+
embedding_model: str = "BAAI/bge-m3",
|
| 15 |
+
llm_model: str = "meta-llama/Llama-3.1-8B-Instruct",
|
| 16 |
+
batch_size: int = 64,
|
| 17 |
+
):
|
| 18 |
+
"""
|
| 19 |
+
Initialise le moteur RAG avec les documents (contenant chacun 'url' et 'text'),
|
| 20 |
+
les paramètres de configuration et les clients nécessaires.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
documents: Liste de documents, chacun un dictionnaire avec les clés 'url' et 'text'.
|
| 24 |
+
embedding_model: Nom du modèle pour calculer les embeddings en local.
|
| 25 |
+
llm_model: Nom du modèle LLM pour les complétions.
|
| 26 |
+
batch_size: Nombre de documents à traiter par lot.
|
| 27 |
+
"""
|
| 28 |
+
self.documents = documents
|
| 29 |
+
self.embedding_model = embedding_model # Nom du modèle pour embeddings (local)
|
| 30 |
+
self.llm_model = llm_model
|
| 31 |
+
self.batch_size = batch_size
|
| 32 |
+
self.embeddings: List[List[float]] = []
|
| 33 |
+
# Filtrer les documents dont le texte est vide pour éviter les erreurs
|
| 34 |
+
self.indexed_documents = [doc for doc in self.documents if doc["text"].strip()]
|
| 35 |
+
|
| 36 |
+
# Initialiser le modèle et le tokenizer en local pour le calcul des embeddings
|
| 37 |
+
self.embedding_tokenizer = AutoTokenizer.from_pretrained(self.embedding_model)
|
| 38 |
+
self.embedding_model_local = AutoModel.from_pretrained(self.embedding_model)
|
| 39 |
+
|
| 40 |
+
# Initialiser le client pour le LLM (l'inférence reste à distance pour le LLM)
|
| 41 |
+
self._init_client_hf()
|
| 42 |
+
|
| 43 |
+
def _init_client_hf(self) -> None:
|
| 44 |
+
self.client = InferenceClient(
|
| 45 |
+
model=self.llm_model,
|
| 46 |
+
token=os.environ.get("HF_TOKEN"),
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def index_documents(self) -> None:
|
| 50 |
+
"""Calcule les embeddings par lots en local avec le modèle Hugging Face."""
|
| 51 |
+
texts = [doc["text"] for doc in self.indexed_documents]
|
| 52 |
+
|
| 53 |
+
for i in range(0, len(texts), self.batch_size):
|
| 54 |
+
batch = texts[i:i + self.batch_size]
|
| 55 |
+
if not batch:
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
# Tokenisation et préparation des tenseurs
|
| 59 |
+
inputs = self.embedding_tokenizer(batch, padding=True, truncation=True, return_tensors="pt")
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
outputs = self.embedding_model_local(**inputs)
|
| 62 |
+
# Calcul du pooling moyen sur la dernière couche
|
| 63 |
+
batch_embeddings_tensor = outputs.last_hidden_state.mean(dim=1)
|
| 64 |
+
batch_embeddings = batch_embeddings_tensor.cpu().tolist()
|
| 65 |
+
self.embeddings.extend(batch_embeddings)
|
| 66 |
+
|
| 67 |
+
print(f"Batch {i//self.batch_size + 1} traité, {len(batch_embeddings)} embeddings obtenus")
|
| 68 |
+
|
| 69 |
+
@staticmethod
|
| 70 |
+
def cosine_similarity(query_vec: np.ndarray, matrix: np.ndarray) -> np.ndarray:
|
| 71 |
+
"""
|
| 72 |
+
Calcule la similarité cosinus entre un vecteur de requête et chaque vecteur d'une matrice.
|
| 73 |
+
"""
|
| 74 |
+
query_norm = np.linalg.norm(query_vec)
|
| 75 |
+
query_normalized = query_vec / (query_norm + 1e-10)
|
| 76 |
+
matrix_norm = np.linalg.norm(matrix, axis=1, keepdims=True)
|
| 77 |
+
matrix_normalized = matrix / (matrix_norm + 1e-10)
|
| 78 |
+
return np.dot(matrix_normalized, query_normalized)
|
| 79 |
+
|
| 80 |
+
def search(self, query_embedding: List[float], top_k: int = 5) -> List[Dict]:
|
| 81 |
+
"""
|
| 82 |
+
Recherche des documents sur la base de la similarité cosinus.
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
query_embedding: L'embedding de la requête.
|
| 86 |
+
top_k: Nombre de résultats à renvoyer.
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
Une liste de dictionnaires avec les clés "url", "text" et "score".
|
| 90 |
+
"""
|
| 91 |
+
query_vec = np.array(query_embedding)
|
| 92 |
+
emb_matrix = np.array(self.embeddings)
|
| 93 |
+
scores = self.cosine_similarity(query_vec, emb_matrix)
|
| 94 |
+
top_indices = np.argsort(scores)[::-1][:top_k]
|
| 95 |
+
results = []
|
| 96 |
+
for idx in top_indices:
|
| 97 |
+
doc = self.indexed_documents[idx]
|
| 98 |
+
results.append(
|
| 99 |
+
{"url": doc["url"], "text": doc["text"], "score": float(scores[idx])}
|
| 100 |
+
)
|
| 101 |
+
return results
|
| 102 |
+
|
| 103 |
+
def ask_llm(self, prompt: str) -> str:
|
| 104 |
+
"""
|
| 105 |
+
Appelle le LLM avec l'invite construite et renvoie la réponse générée.
|
| 106 |
+
"""
|
| 107 |
+
messages = [{"role": "user", "content": prompt}]
|
| 108 |
+
response = self.client.chat.completions.create(
|
| 109 |
+
model=self.llm_model, messages=messages
|
| 110 |
+
)
|
| 111 |
+
return response.choices[0].message.content
|
| 112 |
+
|
| 113 |
+
def rag(self, question: str, top_k: int = 4) -> Dict[str, str]:
|
| 114 |
+
"""
|
| 115 |
+
Effectue une génération augmentée par récupération (RAG) pour une question donnée.
|
| 116 |
+
|
| 117 |
+
Args:
|
| 118 |
+
question: La question posée.
|
| 119 |
+
top_k: Nombre de documents de contexte à inclure.
|
| 120 |
+
|
| 121 |
+
Returns:
|
| 122 |
+
Un dictionnaire avec les clés "response", "prompt" et "urls".
|
| 123 |
+
"""
|
| 124 |
+
# 1. Calculer l'embedding de la question en local.
|
| 125 |
+
inputs = self.embedding_tokenizer(question, return_tensors="pt")
|
| 126 |
+
with torch.no_grad():
|
| 127 |
+
outputs = self.embedding_model_local(**inputs)
|
| 128 |
+
question_embedding_tensor = outputs.last_hidden_state.mean(dim=1)[0]
|
| 129 |
+
question_embedding = question_embedding_tensor.cpu().tolist()
|
| 130 |
+
|
| 131 |
+
# 2. Récupérer les documents les plus similaires.
|
| 132 |
+
results = self.search(query_embedding=question_embedding, top_k=top_k)
|
| 133 |
+
context = "\n\n".join([f"URL: {res['url']}\n{res['text']}" for res in results])
|
| 134 |
+
|
| 135 |
+
# 3. Construire l'invite.
|
| 136 |
+
prompt = (
|
| 137 |
+
"You are a highly capable, thoughtful, and precise assistant. Your goal is to deeply understand the user's intent, ask clarifying questions when needed, think step-by-step through complex problems, provide clear and accurate answers, and proactively anticipate helpful follow-up information. "
|
| 138 |
+
"Based on the following context, answer the question precisely and concisely. "
|
| 139 |
+
"If you do not know the answer, do not make it up.\n\n"
|
| 140 |
+
f"Context:\n{context}\n\n"
|
| 141 |
+
f"Question: {question}\n\n"
|
| 142 |
+
"Answer:"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
urls = [res['url'] for res in results]
|
| 146 |
+
|
| 147 |
+
# 4. Appeler le LLM avec l'invite construite.
|
| 148 |
+
llm_response = self.ask_llm(prompt)
|
| 149 |
+
return {"response": llm_response, "prompt": prompt, "urls": urls}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests==2.32.3
|
| 2 |
+
transformers==4.49.0
|
| 3 |
+
numpy==2.2.3
|
| 4 |
+
huggingface-hub==0.29.1
|
| 5 |
+
torch==2.6.0
|