app.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. from flask import Flask, request, jsonify, send_from_directory
  2. from apscheduler.schedulers.background import BackgroundScheduler
  3. from sentence_transformers import CrossEncoder, SentenceTransformer
  4. from langchain_ollama import OllamaLLM
  5. from processing import full_update_pipeline, load_index_from_disk
  6. from agent_ai.auth import login_to_wiki
  7. import faiss
  8. import json
  9. import numpy as np
  10. import textwrap
  11. from sklearn.metrics.pairwise import cosine_similarity
  12. from tabulate import tabulate
  13. import os
  14. import threading
  15. from serpapi import GoogleSearch
  16. from werkzeug.utils import secure_filename
  17. import PyPDF2
  18. from PIL import Image
  19. from transformers import AutoTokenizer
  20. from ocr import run_ocr, generate_html , generate_word_level_html
  21. import tiktoken
  22. from VLM import ask_blip
  23. app = Flask(__name__)
  24. state = {
  25. "index": None,
  26. "all_chunks": [],
  27. "chunk_urls": [],
  28. "all_embeddings": None,
  29. "embedding_model": SentenceTransformer('all-MiniLM-L6-v2'),
  30. "cross_encoder": CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2"),
  31. "is_updating": False,
  32. "llm": OllamaLLM(model="llama3", temperature=0.3, top_p=0.9, max_tokens=600),
  33. "chat_history": []
  34. }
  35. tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
  36. def chunk_text(text, chunk_size=400, overlap=100):
  37. tokens = tokenizer.encode(text)
  38. chunks = []
  39. for i in range(0, len(tokens), chunk_size - overlap):
  40. chunk = tokens[i:i + chunk_size]
  41. chunks.append(tokenizer.decode(chunk))
  42. return chunks
  43. UPLOAD_FOLDER = "uploads"
  44. os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Crée le dossier s'il n'existe pas
  45. app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
  46. ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg'}
  47. def allowed_file(filename):
  48. return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  49. @app.route("/upload_file", methods=["POST"])
  50. def upload_file():
  51. if 'file' not in request.files:
  52. return jsonify({"error": "Aucun fichier détecté."}), 400
  53. file = request.files['file']
  54. if file.filename == '':
  55. return jsonify({"error": "Nom de fichier vide."}), 400
  56. if file and allowed_file(file.filename):
  57. filename = secure_filename(file.filename)
  58. filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  59. file.save(filepath)
  60. question = request.form.get("question", "")
  61. if not question:
  62. return jsonify({"error": "Veuillez poser une question liée au fichier."}), 400
  63. try:
  64. # ✅ 1. Exécuter l'OCR
  65. document, ocr_output = run_ocr(filepath)
  66. # ✅ 2. Sauvegarder le JSON OCR
  67. json_path = os.path.join(app.config['UPLOAD_FOLDER'], "ocr_output.json")
  68. with open(json_path, "w", encoding="utf-8") as fjson:
  69. json.dump(ocr_output, fjson, ensure_ascii=False, indent=2)
  70. # ✅ 3. Générer le HTML avec bounding boxes par mot
  71. html_path = os.path.join(app.config['UPLOAD_FOLDER'], "ocr_output_words.html")
  72. generate_word_level_html(ocr_output, output_html_path=html_path)
  73. # ✅ 4. Extraire le texte brut pour le traitement RAG
  74. text = ""
  75. for page in ocr_output['pages']:
  76. for block in page['blocks']:
  77. for line in block['lines']:
  78. for word in line['words']:
  79. text += word['value'] + " "
  80. text += "\n"
  81. text += "\n"
  82. text += "\n"
  83. except Exception as e:
  84. return jsonify({"error": f"OCR échoué : {str(e)}"}), 500
  85. if text.strip():
  86. chunks = chunk_text(text)
  87. embeddings = state["embedding_model"].encode(chunks)
  88. question_embedding = state["embedding_model"].encode([question])[0]
  89. similarities = cosine_similarity([question_embedding], embeddings)[0]
  90. top_indices = np.argsort(similarities)[::-1][:5]
  91. top_chunks = [chunks[i] for i in top_indices]
  92. top_context = "\n\n".join(top_chunks)
  93. prompt = (
  94. "Réponds en français à la question suivante en utilisant uniquement le contenu fourni. "
  95. "Si la réponse est absente, dis-le clairement.\n\n"
  96. f"{top_context}\n\nQuestion : {question}"
  97. )
  98. answer = state["llm"](prompt).strip()
  99. return jsonify({
  100. "answer": answer,
  101. "html_file": f"uploads/ocr_output_words.html"
  102. })
  103. else:
  104. try:
  105. answer = ask_blip(filepath, question)
  106. return jsonify({"answer": answer})
  107. except Exception as e:
  108. return jsonify({"error": f"LLaVA échoué : {str(e)}"}), 500
  109. return jsonify({"error": "Format de fichier non supporté."}), 400
  110. @app.route("/delete_file", methods=["POST"])
  111. def delete_file():
  112. data = request.get_json()
  113. filename = data.get("filename")
  114. if not filename:
  115. return jsonify({"error": "Nom de fichier manquant."}), 400
  116. filepath = os.path.join(app.config["UPLOAD_FOLDER"], secure_filename(filename))
  117. if os.path.exists(filepath):
  118. os.remove(filepath)
  119. return jsonify({"message": "Fichier supprimé avec succès."})
  120. else:
  121. return jsonify({"error": "Fichier introuvable."}), 404
  122. def load_or_update_index():
  123. print("🚀 App starting up...")
  124. index, texts, urls = load_index_from_disk()
  125. if index:
  126. state["index"] = index
  127. state["all_chunks"] = texts
  128. state["chunk_urls"] = urls
  129. print("✅ Index loaded from disk.")
  130. else:
  131. print("❌ No index found. Launching full update in background...")
  132. threading.Thread(target=run_full_update, daemon=True).start()
  133. def run_full_update():
  134. state["is_updating"] = True
  135. try:
  136. index, texts, urls = full_update_pipeline()
  137. if index:
  138. state["index"] = index
  139. state["all_chunks"] = texts
  140. state["chunk_urls"] = urls
  141. state["all_embeddings"] = state["embedding_model"].encode(texts)
  142. print("✅ Background update finished.")
  143. except Exception as e:
  144. print(f"❌ Failed to update index: {e}")
  145. finally:
  146. state["is_updating"] = False
  147. @app.route('/')
  148. def index():
  149. return send_from_directory(os.getcwd(), 'index.html')
  150. @app.route("/login", methods=["POST"])
  151. def login():
  152. data = request.json
  153. username = data.get("username")
  154. password = data.get("password")
  155. if not username or not password:
  156. return jsonify({"error": "username and password required"}), 400
  157. if not login_to_wiki(username, password):
  158. return jsonify({"error": "Authentication failed"}), 401
  159. return jsonify({"message": "Login successful"})
  160. @app.route("/chat_history", methods=["GET"])
  161. def chat_history():
  162. return jsonify(state["chat_history"])
  163. @app.route("/qa_chain", methods=["POST"])
  164. def qa_chain_route():
  165. if state.get("llm") is None:
  166. return jsonify({"error": "Le modèle LLM n'est pas initialisé."}), 500
  167. if state["index"] is None or not state["all_chunks"]:
  168. if state["is_updating"]:
  169. return jsonify({"error": "Le système est encore en train d’indexer les documents. Réessayez dans quelques instants."}), 503
  170. else:
  171. return jsonify({"error": "L’index est vide. Veuillez attendre la mise à jour ou réinitialiser le système."}), 500
  172. data = request.get_json()
  173. question = data.get("question", "")
  174. if not question:
  175. return jsonify({"error": "Question vide."}), 400
  176. # ÉTAPE 1 : Tentative de réponse avec le Wiki local
  177. try:
  178. query_embedding = state["embedding_model"].encode([question])[0]
  179. if state["all_embeddings"] is None:
  180. state["all_embeddings"] = state["embedding_model"].encode(state["all_chunks"])
  181. similarities = cosine_similarity([query_embedding], state["all_embeddings"])[0]
  182. top_indices = np.argsort(similarities)[::-1][:1500]
  183. rerank_data = [(question, state["all_chunks"][i]) for i in top_indices]
  184. rerank_scores = state["cross_encoder"].predict(rerank_data)
  185. reranked = sorted(
  186. zip([state["all_chunks"][i] for i in top_indices], top_indices, rerank_scores),
  187. key=lambda x: x[2], reverse=True
  188. )
  189. # Sauvegarde du contexte dans un fichier de cache
  190. enc = tiktoken.encoding_for_model("gpt-3.5-turbo")
  191. cache_data = []
  192. for rank, (chunk, idx, score) in enumerate(reranked[:1500], start=1):
  193. url = state["chunk_urls"][idx] if idx < len(state["chunk_urls"]) else "URL inconnue"
  194. token_count = len(enc.encode(chunk))
  195. cache_data.append({
  196. "rank": rank,
  197. "url": url,
  198. "chunk": chunk,
  199. "tokens": token_count
  200. })
  201. with open("cache_context.json", "w", encoding="utf-8") as f:
  202. json.dump(cache_data, f, indent=2, ensure_ascii=False)
  203. # Affichage en terminal
  204. table = []
  205. top_chunks = []
  206. for rank, (chunk, idx, score) in enumerate(reranked, start=1):
  207. url = state["chunk_urls"][idx] if idx < len(state["chunk_urls"]) else "URL inconnue"
  208. wrapped_chunk = "\n".join(textwrap.wrap(chunk.strip(), width=80))[:1000] + "..."
  209. wrapped_url = "\n".join(textwrap.wrap(url, width=80))
  210. table.append([rank, round(score, 4), wrapped_chunk, wrapped_url])
  211. top_chunks.append(chunk)
  212. print("\n================== RÉSULTATS CLASSÉS PAR PERTINENCE ==================")
  213. print(tabulate(table, headers=["Rank", "Score", "Chunk", "URL"], tablefmt="fancy_grid"))
  214. print("=======================================================================\n")
  215. max_chars = 15000
  216. top_chunks = []
  217. current_length = 0
  218. for chunk, _, _ in reranked:
  219. if current_length + len(chunk) > max_chars:
  220. break
  221. top_chunks.append(chunk)
  222. current_length += len(chunk)
  223. top_context = "\n\n".join(top_chunks)
  224. prompt = (
  225. "Réponds uniquement à la question en utilisant les informations contenues dans le contexte ci-dessous. "
  226. "Ta réponse doit être factuelle, concise et précise. "
  227. "N’ajoute aucune information extérieure. "
  228. "Si la réponse n’est pas présente dans le contexte, dis simplement : 'Information non disponible.'\n\n"
  229. f"Contexte :\n{top_context}\n\n"
  230. f"Question : {question}"
  231. )
  232. local_answer = state["llm"](prompt).strip()
  233. print("\n====================== PROMPT UTILISÉ ======================")
  234. print(f"CONTEXTE :\n{top_context}\nQUESTION :\n{question}\nRÉPONSE :\n{local_answer}")
  235. print("============================================================\n")
  236. # Vérifie si la réponse est valable
  237. phrases_non_reponse = [
  238. "n'est pas disponible",
  239. "je ne sais pas",
  240. "je ne trouve pas",
  241. "aucune information",
  242. "information non disponible",
  243. "pas d'information",
  244. "je ne suis pas en mesure",
  245. "le contexte ne contient pas",
  246. "https://localhost:8080"
  247. ]
  248. if local_answer and not any(phrase in local_answer.lower() for phrase in phrases_non_reponse):
  249. source_urls = [state["chunk_urls"][idx] for _, idx, _ in reranked[:5]]
  250. state["chat_history"].append({
  251. "question": question,
  252. "answer": local_answer,
  253. "sources": list(set(source_urls))
  254. })
  255. return jsonify({
  256. "answer": local_answer,
  257. "sources": list(set(source_urls))
  258. })
  259. except Exception as e:
  260. print(f"[Erreur Wiki] : {e}")
  261. # ÉTAPE 2 : Fallback vers recherche web (SerpAPI)
  262. try:
  263. params = {
  264. "engine": "google",
  265. "q": question,
  266. "api_key": "1b0f09bd610259b457b08c5fad06ded73a9e108e6bcc932e02ac88b2f3840171",
  267. "gl": "fr",
  268. "hl": "fr"
  269. }
  270. search = GoogleSearch(params)
  271. results = search.get_dict()
  272. answer = "Je n'ai pas trouvé de réponse précise."
  273. if "answer_box" in results and "answer" in results["answer_box"]:
  274. answer = results["answer_box"]["answer"]
  275. elif "organic_results" in results and results["organic_results"]:
  276. answer = results["organic_results"][0].get("snippet", answer)
  277. # Résumer ou filtrer la réponse via le LLM
  278. if state.get("llm"):
  279. prompt = (
  280. "Réponds uniquement à la question de manière directe et concise.\n"
  281. "Sans reformuler la question, sans phrase d’introduction, sans politesse et sans contenu promotionnel. "
  282. "Utilise uniquement les faits du texte ci-dessous. "
  283. "Si l’information n’est pas présente, réponds : 'Information non disponible.'\n\n"
  284. f"Texte : {answer}\n\n"
  285. f"Question : {question}"
  286. )
  287. try:
  288. answer = state["llm"](prompt).strip()
  289. except Exception as llm_error:
  290. print("[Erreur LLM sur reformulation] :", llm_error)
  291. # Historique
  292. state["chat_history"].append({
  293. "question": question,
  294. "answer": answer,
  295. "sources": ["Google Search"]
  296. })
  297. return jsonify({
  298. "answer": answer,
  299. "sources": ["Google Search"]
  300. })
  301. except Exception as e:
  302. return jsonify({"error": f"Erreur SerpAPI : {str(e)}"}), 500
  303. # Scheduler
  304. scheduler = BackgroundScheduler()
  305. scheduler.add_job(run_full_update, 'interval', minutes=30)
  306. print("🔁 Scheduler activé pour mises à jour toutes les 30 min")
  307. if __name__ == "__main__":
  308. load_or_update_index()
  309. scheduler.start()
  310. app.run(debug=True, use_reloader=False)