from flask import Flask, request, jsonify, send_from_directory from apscheduler.schedulers.background import BackgroundScheduler from sentence_transformers import CrossEncoder, SentenceTransformer from langchain_ollama import OllamaLLM from processing import full_update_pipeline from agent_ai.auth import login_to_wiki import faiss import json import numpy as np import textwrap from sklearn.metrics.pairwise import cosine_similarity from tabulate import tabulate import os import threading from serpapi import GoogleSearch from werkzeug.utils import secure_filename import PyPDF2 from PIL import Image from transformers import AutoTokenizer from ocr import run_ocr, generate_html , generate_word_level_html import tiktoken from VLM import ask_blip import getpass app = Flask(__name__) llm_variants = [ { "name": "config_1", "model": "llama3", "temperature": 0.1, "top_p": 0.95, "max_tokens": 1000 }, { "name": "config_2", "model": "llama3", "temperature": 0.7, "top_p": 0.85, "max_tokens": 1000 }, { "name": "config_3", "model": "llama3", "temperature": 0.3, "top_p": 0.9, "max_tokens": 1000 } ] state = { "index": None, "all_chunks": [], "chunk_urls": [], "all_embeddings": None, "embedding_model": SentenceTransformer('all-MiniLM-L6-v2'), "cross_encoder": CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2"), "is_updating": False, "llm": llm_variants, "chat_history": [] } def run_full_update(): print("🔄 Lancement du pipeline via le scheduler...") full_update_pipeline() tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") def chunk_text(text, chunk_size=400, overlap=100): tokens = tokenizer.encode(text) chunks = [] for i in range(0, len(tokens), chunk_size - overlap): chunk = tokens[i:i + chunk_size] chunks.append(tokenizer.decode(chunk)) return chunks UPLOAD_FOLDER = "uploads" os.makedirs(UPLOAD_FOLDER, exist_ok=True) # CrĂ©e le dossier s'il n'existe pas app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg'} def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route("/upload_file", methods=["POST"]) def upload_file(): if 'file' not in request.files: return jsonify({"error": "Aucun fichier dĂ©tectĂ©."}), 400 file = request.files['file'] if file.filename == '': return jsonify({"error": "Nom de fichier vide."}), 400 if file and allowed_file(file.filename): filename = secure_filename(file.filename) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) question = request.form.get("question", "") if not question: return jsonify({"error": "Veuillez poser une question liĂ©e au fichier."}), 400 try: # ✅ 1. ExĂ©cuter l'OCR document, ocr_output = run_ocr(filepath) # ✅ 2. Sauvegarder le JSON OCR json_path = os.path.join(app.config['UPLOAD_FOLDER'], "ocr_output.json") with open(json_path, "w", encoding="utf-8") as fjson: json.dump(ocr_output, fjson, ensure_ascii=False, indent=2) # ✅ 3. GĂ©nĂ©rer le HTML avec bounding boxes par mot html_path = os.path.join(app.config['UPLOAD_FOLDER'], "ocr_output_words.html") generate_word_level_html(ocr_output, output_html_path=html_path) # ✅ 4. Extraire le texte brut pour le traitement RAG text = "" for page in ocr_output['pages']: for block in page['blocks']: for line in block['lines']: for word in line['words']: text += word['value'] + " " text += "\n" text += "\n" text += "\n" except Exception as e: return jsonify({"error": f"OCR Ă©chouĂ© : {str(e)}"}), 500 if text.strip(): chunks = chunk_text(text) embeddings = state["embedding_model"].encode(chunks) question_embedding = state["embedding_model"].encode([question])[0] similarities = cosine_similarity([question_embedding], embeddings)[0] top_indices = np.argsort(similarities)[::-1][:5] top_chunks = [chunks[i] for i in top_indices] top_context = "\n\n".join(top_chunks) prompt = ( "RĂ©ponds en français Ă  la question suivante en utilisant uniquement le contenu fourni. " "Si la rĂ©ponse est absente, dis-le clairement.\n\n" f"{top_context}\n\nQuestion : {question}" ) answer = state["llm"](prompt).strip() return jsonify({ "answer": answer, "html_file": f"uploads/ocr_output_words.html" }) else: try: answer = ask_blip(filepath, question) return jsonify({"answer": answer}) except Exception as e: return jsonify({"error": f"LLaVA Ă©chouĂ© : {str(e)}"}), 500 return jsonify({"error": "Format de fichier non supportĂ©."}), 400 @app.route("/delete_file", methods=["POST"]) def delete_file(): data = request.get_json() filename = data.get("filename") if not filename: return jsonify({"error": "Nom de fichier manquant."}), 400 filepath = os.path.join(app.config["UPLOAD_FOLDER"], secure_filename(filename)) if os.path.exists(filepath): os.remove(filepath) return jsonify({"message": "Fichier supprimĂ© avec succĂšs."}) else: return jsonify({"error": "Fichier introuvable."}), 404 @app.route('/') def index(): return send_from_directory(os.getcwd(), 'index.html') @app.route("/login", methods=["POST"]) def login(): data = request.json username = data.get("username") password = data.get("password") if not username or not password: return jsonify({"error": "username and password required"}), 400 if not login_to_wiki(username, password): return jsonify({"error": "Authentication failed"}), 401 return jsonify({"message": "Login successful"}) @app.route("/chat_history", methods=["GET"]) def chat_history(): return jsonify(state["chat_history"]) @app.route("/qa_chain", methods=["POST"]) def qa_chain_route(): if state.get("llm") is None: return jsonify({"error": "Le modĂšle LLM n'est pas initialisĂ©."}), 500 if state["index"] is None or not state["all_chunks"]: if state["is_updating"]: return jsonify({"error": "Le systĂšme est encore en train d’indexer les documents. RĂ©essayez dans quelques instants."}), 503 else: return jsonify({"error": "L’index est vide. Veuillez attendre la mise Ă  jour ou rĂ©initialiser le systĂšme."}), 500 data = request.get_json() question = data.get("question", "") if not question: return jsonify({"error": "Question vide."}), 400 try: query_embedding = state["embedding_model"].encode([question])[0] if state["all_embeddings"] is None: state["all_embeddings"] = state["embedding_model"].encode(state["all_chunks"]) similarities = cosine_similarity([query_embedding], state["all_embeddings"])[0] top_indices = np.argsort(similarities)[::-1][:20] rerank_data = [(question, state["all_chunks"][i]) for i in top_indices] rerank_scores = state["cross_encoder"].predict(rerank_data) reranked = sorted( zip([state["all_chunks"][i] for i in top_indices], top_indices, rerank_scores), key=lambda x: x[2], reverse=True ) enc = tiktoken.encoding_for_model("gpt-3.5-turbo") cache_data = [] for rank, (chunk, idx, score) in enumerate(reranked[:20], start=1): url = state["chunk_urls"][idx] if idx < len(state["chunk_urls"]) else "URL inconnue" token_count = len(enc.encode(chunk)) cache_data.append({ "rank": rank, "url": url, "chunk": chunk, "tokens": token_count }) with open("cache_context.json", "w", encoding="utf-8") as f: json.dump(cache_data, f, indent=2, ensure_ascii=False) table = [] top_chunks = [] for rank, (chunk, idx, score) in enumerate(reranked, start=1): url = state["chunk_urls"][idx] if idx < len(state["chunk_urls"]) else "URL inconnue" wrapped_chunk = "\n".join(textwrap.wrap(chunk.strip(), width=80))[:1000] + "..." wrapped_url = "\n".join(textwrap.wrap(url, width=80)) table.append([rank, round(score, 4), wrapped_chunk, wrapped_url]) top_chunks.append(chunk) print("\n================== RÉSULTATS CLASSÉS PAR PERTINENCE ==================") print(tabulate(table, headers=["Rank", "Score", "Chunk", "URL"], tablefmt="fancy_grid")) print("=======================================================================\n") max_chars = 15000 top_chunks = [] current_length = 0 for chunk, _, _ in reranked: if current_length + len(chunk) > max_chars: break top_chunks.append(chunk) current_length += len(chunk) top_context = "\n\n".join(top_chunks) llm_variants = [ { "name": "config_1", "model": "llama3", "temperature": 0.1, "top_p": 0.95, "max_tokens": 600 }, { "name": "config_2", "model": "llama3", "temperature": 0.7, "top_p": 0.85, "max_tokens": 600 }, { "name": "config_3", "model": "llama3", "temperature": 0.3, "top_p": 0.9, "max_tokens": 600 } ] all_answers = [] for config in llm_variants: llm = OllamaLLM( model=config["model"], temperature=config["temperature"], top_p=config["top_p"], max_tokens=config["max_tokens"] ) prompt = f""" Tu es un assistant intelligent. RĂ©ponds uniquement avec les informations donnĂ©es dans le contexte suivant. Ta rĂ©ponse doit ĂȘtre factuelle, concise et prĂ©cise. Si la rĂ©ponse n’est pas prĂ©sente dans le contexte, dis : "Information non disponible." Ne reformule pas la question. N’invente rien. Contexte : {top_context} Question : {question} RĂ©ponse : """ response = llm(prompt).strip() all_answers.append({ "config": config["name"], "response": response }) print("\n====================== PROMPTS & RÉPONSES ======================") for ans in all_answers: print(f"\nConfig: {ans['config']}\nRĂ©ponse : {ans['response']}") print("==============================================================\n") return jsonify({"answers": all_answers}) except Exception as e: print(f"[Erreur Wiki] : {e}") # ÉTAPE 2 : Fallback vers recherche web (SerpAPI) try: params = { "engine": "google", "q": question, "api_key": "1b0f09bd610259b457b08c5fad06ded73a9e108e6bcc932e02ac88b2f3840171", "gl": "fr", "hl": "fr" } search = GoogleSearch(params) results = search.get_dict() answer = "Je n'ai pas trouvĂ© de rĂ©ponse prĂ©cise." if "answer_box" in results and "answer" in results["answer_box"]: answer = results["answer_box"]["answer"] elif "organic_results" in results and results["organic_results"]: answer = results["organic_results"][0].get("snippet", answer) # RĂ©sumer ou filtrer la rĂ©ponse via le LLM if state.get("llm"): prompt = ( "RĂ©ponds uniquement Ă  la question de maniĂšre directe et concise.\n" "Sans reformuler la question, sans phrase d’introduction, sans politesse et sans contenu promotionnel. " "Utilise uniquement les faits du texte ci-dessous. " "Si l’information n’est pas prĂ©sente, rĂ©ponds : 'Information non disponible.'\n\n" f"Texte : {answer}\n\n" f"Question : {question}" ) try: answer = state["llm"](prompt).strip() except Exception as llm_error: print("[Erreur LLM sur reformulation] :", llm_error) # Historique state["chat_history"].append({ "question": question, "answer": answer, "sources": ["Google Search"] }) return jsonify({ "answer": answer, "sources": ["Google Search"] }) except Exception as e: return jsonify({"error": f"Erreur SerpAPI : {str(e)}"}), 500 # Scheduler scheduler = BackgroundScheduler() scheduler.add_job(run_full_update, 'interval', minutes=150) print("🔁 Scheduler activĂ© pour mises Ă  jour toutes les 30 min") def prompt_for_login(): print("🔐 Please log in to the Wiki before starting the app.") while True: username = input("Username: ") password = getpass.getpass("Password: ") if login_to_wiki(username, password): print("Login successful, starting the app...") break else: print("Login failed, please try again.\n") if __name__ == "__main__": prompt_for_login() full_update_pipeline() scheduler.start() app.run(debug=True, use_reloader=False)