Upload 5 files
Browse files- Readme.md +20 -0
- app.py +112 -0
- ingest.py +39 -0
- requirements.txt +15 -0
- trusted_sources.json +10 -0
Readme.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CyberWise Assistant
|
| 2 |
+
|
| 3 |
+
Un assistant IA open-source pour la **sensibilisation à la cybersécurité**, la **vie numérique** et l’**analyse de logs**.
|
| 4 |
+
|
| 5 |
+
✅ Réponses **sourcées**
|
| 6 |
+
✅ Sources **officielles uniquement** (ANSSI, CNIL, OWASP, CISA…)
|
| 7 |
+
✅ **Pas de hallucination**
|
| 8 |
+
✅ Interface conversationnelle (type ChatGPT)
|
| 9 |
+
|
| 10 |
+
Déployé sur Hugging Face Spaces : [](https://huggingface.co/spaces/votre-username/cyberwise-assistant)
|
| 11 |
+
|
| 12 |
+
## Comment lancer localement
|
| 13 |
+
1. `pip install -r requirements.txt`
|
| 14 |
+
2. `python ingest.py` (une seule fois)
|
| 15 |
+
3. `python app.py`
|
| 16 |
+
|
| 17 |
+
## Comment déployer sur Hugging Face
|
| 18 |
+
1. Créez un nouveau Space (Type : **GPU**, SDK : **Gradio**)
|
| 19 |
+
2. Poussez ce repo
|
| 20 |
+
3. Ajoutez une variable d’environnement (optionnel) : `HF_TOKEN` si besoin
|
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (version auto-indexante - sans besoin de chroma_db pré-uploadé)
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Auto-indexation au premier lancement
|
| 8 |
+
# ----------------------------
|
| 9 |
+
DB_PATH = "./chromo_db"
|
| 10 |
+
if not os.path.exists(DB_PATH):
|
| 11 |
+
print("🔍 Base de connaissances absente. Indexation en cours...")
|
| 12 |
+
from ingest import run_ingest
|
| 13 |
+
run_ingest(DB_PATH)
|
| 14 |
+
print("✅ Indexation terminée.")
|
| 15 |
+
|
| 16 |
+
# ----------------------------
|
| 17 |
+
# Charger la base vectorielle
|
| 18 |
+
# ----------------------------
|
| 19 |
+
from langchain_community.vectorstores import Chroma
|
| 20 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 21 |
+
|
| 22 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 23 |
+
vectorstore = Chroma(persist_directory=DB_PATH, embedding_function=embedding_model)
|
| 24 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 25 |
+
|
| 26 |
+
# ----------------------------
|
| 27 |
+
# Charger le LLM
|
| 28 |
+
# ----------------------------
|
| 29 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 30 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
|
| 31 |
+
import torch
|
| 32 |
+
|
| 33 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 34 |
+
|
| 35 |
+
bnb_config = BitsAndBytesConfig(
|
| 36 |
+
load_in_4bit=True,
|
| 37 |
+
bnb_4bit_quant_type="nf4",
|
| 38 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 42 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
+
model_name,
|
| 44 |
+
quantization_config=bnb_config,
|
| 45 |
+
device_map="auto",
|
| 46 |
+
trust_remote_code=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
pipe = pipeline(
|
| 50 |
+
"text-generation",
|
| 51 |
+
model=model,
|
| 52 |
+
tokenizer=tokenizer,
|
| 53 |
+
max_new_tokens=512,
|
| 54 |
+
temperature=0.3,
|
| 55 |
+
top_p=0.95,
|
| 56 |
+
repetition_penalty=1.2
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
| 60 |
+
|
| 61 |
+
# ----------------------------
|
| 62 |
+
# Chaîne RAG
|
| 63 |
+
# ----------------------------
|
| 64 |
+
from langchain_core.prompts import PromptTemplate
|
| 65 |
+
|
| 66 |
+
def format_docs(docs):
|
| 67 |
+
sources = list(set(doc.metadata["source"] for doc in docs))
|
| 68 |
+
context = "\n\n".join(doc.page_content for doc in docs)
|
| 69 |
+
return context, sources
|
| 70 |
+
|
| 71 |
+
prompt_template = """
|
| 72 |
+
Tu es CyberWise, un assistant expert en cybersécurité, éducation numérique et analyse de logs.
|
| 73 |
+
Utilise UNIQUEMENT les informations du contexte ci-dessous pour répondre.
|
| 74 |
+
Si le contexte ne contient pas la réponse, dis strictement :
|
| 75 |
+
"Je ne dispose pas d'informations fiables sur ce sujet dans mes sources officielles."
|
| 76 |
+
|
| 77 |
+
Contexte :
|
| 78 |
+
{context}
|
| 79 |
+
|
| 80 |
+
Question : {question}
|
| 81 |
+
|
| 82 |
+
Réponse (claire, pédagogique, en français si la question est en français) :
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
prompt = PromptTemplate.from_template(prompt_template)
|
| 86 |
+
|
| 87 |
+
def rag_chain(question):
|
| 88 |
+
docs = retriever.invoke(question)
|
| 89 |
+
context, sources = format_docs(docs)
|
| 90 |
+
|
| 91 |
+
if not context.strip():
|
| 92 |
+
return "Je ne dispose pas d'informations fiables sur ce sujet dans mes sources officielles.", []
|
| 93 |
+
|
| 94 |
+
formatted_prompt = prompt.format(context=context, question=question)
|
| 95 |
+
response = llm.invoke(formatted_prompt)
|
| 96 |
+
clean_response = response[0]["generated_text"].replace(formatted_prompt, "").strip()
|
| 97 |
+
return clean_response, sources
|
| 98 |
+
|
| 99 |
+
# ----------------------------
|
| 100 |
+
# Interface Gradio
|
| 101 |
+
# ----------------------------
|
| 102 |
+
def predict(message, history):
|
| 103 |
+
response, sources = rag_chain(message)
|
| 104 |
+
if sources:
|
| 105 |
+
source_links = "\n\n**Sources officielles :**\n" + "\n".join([f"- [{s}]({s})" for s in sources])
|
| 106 |
+
response += source_links
|
| 107 |
+
return response
|
| 108 |
+
|
| 109 |
+
# ----------------------------
|
| 110 |
+
# Modifier ingest.py pour qu'il soit appelable
|
| 111 |
+
# ----------------------------
|
| 112 |
+
# ➡️ On déplace la logique d'ingest dans une fonction
|
ingest.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ingest.py (version appelable en fonction)
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 6 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain_community.vectorstores import Chroma
|
| 8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
|
| 10 |
+
def run_ingest(persist_directory: str = "./chroma_db"):
|
| 11 |
+
# Charger les sources
|
| 12 |
+
with open("trusted_sources.json", "r", encoding="utf-8") as f:
|
| 13 |
+
sources = json.load(f)
|
| 14 |
+
|
| 15 |
+
docs = []
|
| 16 |
+
for src in sources:
|
| 17 |
+
try:
|
| 18 |
+
print(f"📄 Chargement : {src['url']}")
|
| 19 |
+
loader = WebBaseLoader(src['url'])
|
| 20 |
+
data = loader.load()
|
| 21 |
+
for d in data:
|
| 22 |
+
d.metadata["source"] = src['url']
|
| 23 |
+
docs.extend(data)
|
| 24 |
+
time.sleep(1)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"❌ Erreur sur {src['url']}: {e}")
|
| 27 |
+
|
| 28 |
+
if not docs:
|
| 29 |
+
raise RuntimeError("Aucun document chargé. Vérifiez les URLs.")
|
| 30 |
+
|
| 31 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 32 |
+
splits = text_splitter.split_documents(docs)
|
| 33 |
+
|
| 34 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 35 |
+
Chroma.from_documents(
|
| 36 |
+
documents=splits,
|
| 37 |
+
embedding=embedding_model,
|
| 38 |
+
persist_directory=persist_directory
|
| 39 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.35.0
|
| 2 |
+
langchain==0.2.11
|
| 3 |
+
langchain-community==0.2.10
|
| 4 |
+
langchain-core==0.2.23
|
| 5 |
+
chromadb==0.4.24
|
| 6 |
+
sentence-transformers==3.0.1
|
| 7 |
+
torch==2.4.0
|
| 8 |
+
transformers==4.43.0
|
| 9 |
+
accelerate==0.33.0
|
| 10 |
+
bitsandbytes==0.43.3
|
| 11 |
+
beautifulsoup4==4.12.3
|
| 12 |
+
trafilatura==1.10.1
|
| 13 |
+
unstructured==0.15.2
|
| 14 |
+
unstructured[local-inference]==0.15.2
|
| 15 |
+
huggingface-hub==0.23.5
|
trusted_sources.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{"name": "ANSSI - Guide pratique", "url": "https://www.ssi.gouv.fr/guide/"},
|
| 3 |
+
{"name": "ANSSI - Recommandations", "url": "https://www.ssi.gouv.fr/recommandation/"},
|
| 4 |
+
{"name": "CNIL - Fiches pratiques", "url": "https://www.cnil.fr/fr/fiches-pratiques"},
|
| 5 |
+
{"name": "CNIL - Données personnelles", "url": "https://www.cnil.fr/fr/donnees-personnelles"},
|
| 6 |
+
{"name": "OWASP Top 10", "url": "https://owasp.org/www-project-top-ten/"},
|
| 7 |
+
{"name": "CISA - Secure Our World", "url": "https://www.cisa.gov/secure-our-world"},
|
| 8 |
+
{"name": "Elastic - Logging Guide", "url": "https://www.elastic.co/guide/en/elastic-stack-guide/current/logging.html"},
|
| 9 |
+
{"name": "Splunk - Log Analysis", "url": "https://docs.spluent.com/Documentation/Splunk/latest/Data/About-logs"}
|
| 10 |
+
]
|