app.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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
  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. import getpass
  24. app = Flask(__name__)
  25. llm_variants = [
  26. {
  27. "name": "config_1",
  28. "model": "llama3",
  29. "temperature": 0.1,
  30. "top_p": 0.95,
  31. "max_tokens": 1000
  32. },
  33. {
  34. "name": "config_2",
  35. "model": "llama3",
  36. "temperature": 0.7,
  37. "top_p": 0.85,
  38. "max_tokens": 1000
  39. },
  40. {
  41. "name": "config_3",
  42. "model": "llama3",
  43. "temperature": 0.3,
  44. "top_p": 0.9,
  45. "max_tokens": 1000
  46. }
  47. ]
  48. state = {
  49. "index": None,
  50. "all_chunks": [],
  51. "chunk_urls": [],
  52. "all_embeddings": None,
  53. "embedding_model": SentenceTransformer('all-MiniLM-L6-v2'),
  54. "cross_encoder": CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2"),
  55. "is_updating": False,
  56. "llm": llm_variants,
  57. "chat_history": []
  58. }
  59. def run_full_update():
  60. print("🔄 Lancement du pipeline via le scheduler...")
  61. full_update_pipeline()
  62. tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
  63. def chunk_text(text, chunk_size=400, overlap=100):
  64. tokens = tokenizer.encode(text)
  65. chunks = []
  66. for i in range(0, len(tokens), chunk_size - overlap):
  67. chunk = tokens[i:i + chunk_size]
  68. chunks.append(tokenizer.decode(chunk))
  69. return chunks
  70. UPLOAD_FOLDER = "uploads"
  71. os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Crée le dossier s'il n'existe pas
  72. app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
  73. ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg'}
  74. def allowed_file(filename):
  75. return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  76. @app.route("/upload_file", methods=["POST"])
  77. def upload_file():
  78. if 'file' not in request.files:
  79. return jsonify({"error": "Aucun fichier détecté."}), 400
  80. file = request.files['file']
  81. if file.filename == '':
  82. return jsonify({"error": "Nom de fichier vide."}), 400
  83. if file and allowed_file(file.filename):
  84. filename = secure_filename(file.filename)
  85. filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  86. file.save(filepath)
  87. question = request.form.get("question", "")
  88. if not question:
  89. return jsonify({"error": "Veuillez poser une question liée au fichier."}), 400
  90. try:
  91. # ✅ 1. Exécuter l'OCR
  92. document, ocr_output = run_ocr(filepath)
  93. # ✅ 2. Sauvegarder le JSON OCR
  94. json_path = os.path.join(app.config['UPLOAD_FOLDER'], "ocr_output.json")
  95. with open(json_path, "w", encoding="utf-8") as fjson:
  96. json.dump(ocr_output, fjson, ensure_ascii=False, indent=2)
  97. # ✅ 3. Générer le HTML avec bounding boxes par mot
  98. html_path = os.path.join(app.config['UPLOAD_FOLDER'], "ocr_output_words.html")
  99. generate_word_level_html(ocr_output, output_html_path=html_path)
  100. # ✅ 4. Extraire le texte brut pour le traitement RAG
  101. text = ""
  102. for page in ocr_output['pages']:
  103. for block in page['blocks']:
  104. for line in block['lines']:
  105. for word in line['words']:
  106. text += word['value'] + " "
  107. text += "\n"
  108. text += "\n"
  109. text += "\n"
  110. except Exception as e:
  111. return jsonify({"error": f"OCR échoué : {str(e)}"}), 500
  112. if text.strip():
  113. chunks = chunk_text(text)
  114. embeddings = state["embedding_model"].encode(chunks)
  115. question_embedding = state["embedding_model"].encode([question])[0]
  116. similarities = cosine_similarity([question_embedding], embeddings)[0]
  117. top_indices = np.argsort(similarities)[::-1][:5]
  118. top_chunks = [chunks[i] for i in top_indices]
  119. top_context = "\n\n".join(top_chunks)
  120. prompt = (
  121. "Réponds en français à la question suivante en utilisant uniquement le contenu fourni. "
  122. "Si la réponse est absente, dis-le clairement.\n\n"
  123. f"{top_context}\n\nQuestion : {question}"
  124. )
  125. answer = state["llm"](prompt).strip()
  126. return jsonify({
  127. "answer": answer,
  128. "html_file": f"uploads/ocr_output_words.html"
  129. })
  130. else:
  131. try:
  132. answer = ask_blip(filepath, question)
  133. return jsonify({"answer": answer})
  134. except Exception as e:
  135. return jsonify({"error": f"LLaVA échoué : {str(e)}"}), 500
  136. return jsonify({"error": "Format de fichier non supporté."}), 400
  137. @app.route("/delete_file", methods=["POST"])
  138. def delete_file():
  139. data = request.get_json()
  140. filename = data.get("filename")
  141. if not filename:
  142. return jsonify({"error": "Nom de fichier manquant."}), 400
  143. filepath = os.path.join(app.config["UPLOAD_FOLDER"], secure_filename(filename))
  144. if os.path.exists(filepath):
  145. os.remove(filepath)
  146. return jsonify({"message": "Fichier supprimé avec succès."})
  147. else:
  148. return jsonify({"error": "Fichier introuvable."}), 404
  149. @app.route('/')
  150. def index():
  151. return send_from_directory(os.getcwd(), 'index.html')
  152. @app.route("/login", methods=["POST"])
  153. def login():
  154. data = request.json
  155. username = data.get("username")
  156. password = data.get("password")
  157. if not username or not password:
  158. return jsonify({"error": "username and password required"}), 400
  159. if not login_to_wiki(username, password):
  160. return jsonify({"error": "Authentication failed"}), 401
  161. return jsonify({"message": "Login successful"})
  162. @app.route("/chat_history", methods=["GET"])
  163. def chat_history():
  164. return jsonify(state["chat_history"])
  165. @app.route("/qa_chain", methods=["POST"])
  166. def qa_chain_route():
  167. if state.get("llm") is None:
  168. return jsonify({"error": "Le modèle LLM n'est pas initialisé."}), 500
  169. if state["index"] is None or not state["all_chunks"]:
  170. if state["is_updating"]:
  171. return jsonify({"error": "Le système est encore en train d’indexer les documents. Réessayez dans quelques instants."}), 503
  172. else:
  173. return jsonify({"error": "L’index est vide. Veuillez attendre la mise à jour ou réinitialiser le système."}), 500
  174. data = request.get_json()
  175. question = data.get("question", "")
  176. if not question:
  177. return jsonify({"error": "Question vide."}), 400
  178. try:
  179. query_embedding = state["embedding_model"].encode([question])[0]
  180. if state["all_embeddings"] is None:
  181. state["all_embeddings"] = state["embedding_model"].encode(state["all_chunks"])
  182. similarities = cosine_similarity([query_embedding], state["all_embeddings"])[0]
  183. top_indices = np.argsort(similarities)[::-1][:20]
  184. rerank_data = [(question, state["all_chunks"][i]) for i in top_indices]
  185. rerank_scores = state["cross_encoder"].predict(rerank_data)
  186. reranked = sorted(
  187. zip([state["all_chunks"][i] for i in top_indices], top_indices, rerank_scores),
  188. key=lambda x: x[2], reverse=True
  189. )
  190. enc = tiktoken.encoding_for_model("gpt-3.5-turbo")
  191. cache_data = []
  192. for rank, (chunk, idx, score) in enumerate(reranked[:20], 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. table = []
  204. top_chunks = []
  205. for rank, (chunk, idx, score) in enumerate(reranked, start=1):
  206. url = state["chunk_urls"][idx] if idx < len(state["chunk_urls"]) else "URL inconnue"
  207. wrapped_chunk = "\n".join(textwrap.wrap(chunk.strip(), width=80))[:1000] + "..."
  208. wrapped_url = "\n".join(textwrap.wrap(url, width=80))
  209. table.append([rank, round(score, 4), wrapped_chunk, wrapped_url])
  210. top_chunks.append(chunk)
  211. print("\n================== RÉSULTATS CLASSÉS PAR PERTINENCE ==================")
  212. print(tabulate(table, headers=["Rank", "Score", "Chunk", "URL"], tablefmt="fancy_grid"))
  213. print("=======================================================================\n")
  214. max_chars = 15000
  215. top_chunks = []
  216. current_length = 0
  217. for chunk, _, _ in reranked:
  218. if current_length + len(chunk) > max_chars:
  219. break
  220. top_chunks.append(chunk)
  221. current_length += len(chunk)
  222. top_context = "\n\n".join(top_chunks)
  223. llm_variants = [
  224. {
  225. "name": "config_1",
  226. "model": "llama3",
  227. "temperature": 0.1,
  228. "top_p": 0.95,
  229. "max_tokens": 600
  230. },
  231. {
  232. "name": "config_2",
  233. "model": "llama3",
  234. "temperature": 0.7,
  235. "top_p": 0.85,
  236. "max_tokens": 600
  237. },
  238. {
  239. "name": "config_3",
  240. "model": "llama3",
  241. "temperature": 0.3,
  242. "top_p": 0.9,
  243. "max_tokens": 600
  244. }
  245. ]
  246. all_answers = []
  247. for config in llm_variants:
  248. llm = OllamaLLM(
  249. model=config["model"],
  250. temperature=config["temperature"],
  251. top_p=config["top_p"],
  252. max_tokens=config["max_tokens"]
  253. )
  254. prompt = f"""
  255. Tu es un assistant intelligent. Réponds uniquement avec les informations données dans le contexte suivant.
  256. Ta réponse doit être factuelle, concise et précise. Si la réponse n’est pas présente dans le contexte, dis :
  257. "Information non disponible."
  258. Ne reformule pas la question. N’invente rien.
  259. Contexte :
  260. {top_context}
  261. Question : {question}
  262. Réponse :
  263. """
  264. response = llm(prompt).strip()
  265. all_answers.append({
  266. "config": config["name"],
  267. "response": response
  268. })
  269. print("\n====================== PROMPTS & RÉPONSES ======================")
  270. for ans in all_answers:
  271. print(f"\nConfig: {ans['config']}\nRéponse : {ans['response']}")
  272. print("==============================================================\n")
  273. return jsonify({"answers": all_answers})
  274. except Exception as e:
  275. print(f"[Erreur Wiki] : {e}")
  276. # ÉTAPE 2 : Fallback vers recherche web (SerpAPI)
  277. try:
  278. params = {
  279. "engine": "google",
  280. "q": question,
  281. "api_key": "1b0f09bd610259b457b08c5fad06ded73a9e108e6bcc932e02ac88b2f3840171",
  282. "gl": "fr",
  283. "hl": "fr"
  284. }
  285. search = GoogleSearch(params)
  286. results = search.get_dict()
  287. answer = "Je n'ai pas trouvé de réponse précise."
  288. if "answer_box" in results and "answer" in results["answer_box"]:
  289. answer = results["answer_box"]["answer"]
  290. elif "organic_results" in results and results["organic_results"]:
  291. answer = results["organic_results"][0].get("snippet", answer)
  292. # Résumer ou filtrer la réponse via le LLM
  293. if state.get("llm"):
  294. prompt = (
  295. "Réponds uniquement à la question de manière directe et concise.\n"
  296. "Sans reformuler la question, sans phrase d’introduction, sans politesse et sans contenu promotionnel. "
  297. "Utilise uniquement les faits du texte ci-dessous. "
  298. "Si l’information n’est pas présente, réponds : 'Information non disponible.'\n\n"
  299. f"Texte : {answer}\n\n"
  300. f"Question : {question}"
  301. )
  302. try:
  303. answer = state["llm"](prompt).strip()
  304. except Exception as llm_error:
  305. print("[Erreur LLM sur reformulation] :", llm_error)
  306. # Historique
  307. state["chat_history"].append({
  308. "question": question,
  309. "answer": answer,
  310. "sources": ["Google Search"]
  311. })
  312. return jsonify({
  313. "answer": answer,
  314. "sources": ["Google Search"]
  315. })
  316. except Exception as e:
  317. return jsonify({"error": f"Erreur SerpAPI : {str(e)}"}), 500
  318. # Scheduler
  319. scheduler = BackgroundScheduler()
  320. scheduler.add_job(run_full_update, 'interval', minutes=150)
  321. print("🔁 Scheduler activé pour mises à jour toutes les 30 min")
  322. def prompt_for_login():
  323. print("🔐 Please log in to the Wiki before starting the app.")
  324. while True:
  325. username = input("Username: ")
  326. password = getpass.getpass("Password: ")
  327. if login_to_wiki(username, password):
  328. print("Login successful, starting the app...")
  329. break
  330. else:
  331. print("Login failed, please try again.\n")
  332. if __name__ == "__main__":
  333. prompt_for_login()
  334. full_update_pipeline()
  335. scheduler.start()
  336. app.run(debug=True, use_reloader=False)