index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <!DOCTYPE html>
  2. <html lang="fr">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <title>Chatbot Intersel</title>
  7. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" />
  8. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  9. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
  10. <style>
  11. body {
  12. margin: 0;
  13. font-family: 'Inter', sans-serif;
  14. background-color: #f4f6f8;
  15. height: 100vh;
  16. width: 100vw;
  17. overflow: hidden;
  18. display: flex;
  19. flex-direction: column;
  20. }
  21. .chat-container {
  22. flex: 1;
  23. display: flex;
  24. flex-direction: column;
  25. height: 100vh;
  26. width: 100vw;
  27. }
  28. .chat-header {
  29. height: 64px;
  30. background: #07468f;
  31. color: white;
  32. padding: 1rem;
  33. font-size: 1.25rem;
  34. font-weight: bold;
  35. text-align: center;
  36. flex-shrink: 0;
  37. }
  38. .chat-body {
  39. display: flex;
  40. flex: 1;
  41. height: calc(100vh - 64px);
  42. overflow: hidden;
  43. }
  44. .chat-sidebar {
  45. width: 20%;
  46. background-color: #e9f5f2;
  47. border-right: 1px solid #ccc;
  48. padding: 1rem;
  49. overflow-y: auto;
  50. }
  51. .chat-title {
  52. font-weight: bold;
  53. margin-bottom: 1rem;
  54. }
  55. .chat-main {
  56. flex: 1;
  57. display: flex;
  58. flex-direction: column;
  59. background-color: #f7f9fa;
  60. }
  61. .chat-history {
  62. flex: 1;
  63. padding: 1rem;
  64. overflow-y: auto;
  65. display: flex;
  66. flex-direction: column;
  67. gap: 1rem;
  68. }
  69. .chat-input {
  70. padding: 1rem;
  71. background: white;
  72. border-top: 1px solid #ddd;
  73. flex-shrink: 0;
  74. }
  75. form {
  76. display: flex;
  77. flex-direction: column;
  78. gap: 1rem;
  79. }
  80. input[type="text"] {
  81. padding: 0.75rem;
  82. border-radius: 8px;
  83. border: 1px solid #ccc;
  84. font-size: 1rem;
  85. width: 100%;
  86. box-sizing: border-box;
  87. }
  88. button {
  89. background-color: #1e42e0;
  90. color: white;
  91. padding: 0.75rem;
  92. border: none;
  93. border-radius: 8px;
  94. cursor: pointer;
  95. font-weight: bold;
  96. transition: background-color 0.3s;
  97. }
  98. button:hover {
  99. background-color: #214874;
  100. }
  101. .upload-label {
  102. display: inline-block;
  103. background-color: #e0f7f1;
  104. color: #070e49;
  105. padding: 0.5rem 1rem;
  106. border-radius: 8px;
  107. cursor: pointer;
  108. font-weight: bold;
  109. font-size: 0.95rem;
  110. width: fit-content;
  111. }
  112. input[type="file"] {
  113. display: none;
  114. }
  115. .file-preview {
  116. background-color: #f0f0f0;
  117. padding: 0.5rem;
  118. border-radius: 6px;
  119. font-size: 0.9rem;
  120. color: #555;
  121. max-width: 100%;
  122. word-break: break-word;
  123. }
  124. .message {
  125. display: flex;
  126. max-width: 70%;
  127. line-height: 1.4;
  128. position: relative;
  129. gap: 0.5rem;
  130. }
  131. .message.user {
  132. align-self: flex-end;
  133. flex-direction: row-reverse;
  134. }
  135. .message.bot {
  136. align-self: flex-start;
  137. flex-direction: row;
  138. }
  139. .message .avatar {
  140. font-size: 1.5rem;
  141. user-select: none;
  142. flex-shrink: 0;
  143. align-self: flex-end;
  144. margin: 0 0.5rem;
  145. }
  146. .message .bubble {
  147. padding: 0.8rem 1rem;
  148. border-radius: 16px;
  149. box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  150. white-space: pre-wrap;
  151. word-wrap: break-word;
  152. font-size: 1rem;
  153. line-height: 1.4;
  154. position: relative;
  155. }
  156. .message.user .bubble {
  157. background-color: #d1e7dd;
  158. color: #00133d;
  159. border-bottom-right-radius: 0;
  160. }
  161. .message.user .bubble::after {
  162. content: "";
  163. position: absolute;
  164. bottom: 0;
  165. right: -8px;
  166. width: 0;
  167. height: 0;
  168. border-top: 12px solid #d1e7dd;
  169. border-left: 8px solid transparent;
  170. }
  171. .message.bot .bubble {
  172. background-color: #e2e3e5;
  173. color: #41464b;
  174. border-bottom-left-radius: 0;
  175. }
  176. .message.bot .bubble::after {
  177. content: "";
  178. position: absolute;
  179. bottom: 0;
  180. left: -8px;
  181. width: 0;
  182. height: 0;
  183. border-top: 12px solid #e2e3e5;
  184. border-right: 8px solid transparent;
  185. }
  186. body.dark {
  187. background-color: #1e1e1e;
  188. color: #f0f0f0;
  189. }
  190. body.dark .chat-header {
  191. background: #333;
  192. }
  193. body.dark .chat-sidebar {
  194. background-color: #2d2d2d;
  195. border-color: #444;
  196. }
  197. body.dark .chat-main {
  198. background-color: #1e1e1e;
  199. }
  200. body.dark .chat-history {
  201. background-color: #1e1e1e;
  202. }
  203. body.dark .chat-input {
  204. background-color: #2c2c2c;
  205. border-color: #444;
  206. }
  207. body.dark input[type="text"] {
  208. background-color: #3a3a3a;
  209. color: #f0f0f0;
  210. border-color: #555;
  211. }
  212. body.dark button {
  213. background-color: #96b4eb;
  214. color: white;
  215. }
  216. body.dark .upload-label {
  217. background-color: #37474f;
  218. color: #95a9e9;
  219. }
  220. body.dark .file-preview {
  221. background-color: #333;
  222. color: #ccc;
  223. }
  224. body.dark .message.user .bubble {
  225. background-color: #8d9ff0;
  226. color: #c8e6c9;
  227. }
  228. body.dark .message.user .bubble::after {
  229. border-top-color: #262f4d;
  230. }
  231. body.dark .message.bot .bubble {
  232. background-color: #444;
  233. color: #eee;
  234. }
  235. body.dark .message.bot .bubble::after {
  236. border-top-color: #444;
  237. }
  238. /* Header layout */
  239. .header {
  240. display: flex;
  241. justify-content: space-between;
  242. align-items: center;
  243. padding: 12px 24px;
  244. background-color: var(--header-bg, #f5f5f5);
  245. border-bottom: 1px solid #ccc;
  246. color: var(--text-color, #333);
  247. }
  248. .header h1 {
  249. font-size: 1.5em;
  250. margin: 0;
  251. }
  252. /* Right section layout */
  253. .header-right {
  254. display: flex;
  255. gap: 16px;
  256. }
  257. /* Shared button/icon style */
  258. .icon-button {
  259. background: none;
  260. border: none;
  261. font-size: 1.4em;
  262. color: var(--text-color, #333);
  263. cursor: pointer;
  264. transition: color 0.3s ease;
  265. text-decoration: none;
  266. }
  267. .icon-button:hover {
  268. color: #007bff;
  269. }
  270. /* Dark mode support */
  271. @media (prefers-color-scheme: dark) {
  272. .header {
  273. --header-bg: #1e1e1e;
  274. --text-color: #f0f0f0;
  275. }
  276. .icon-button {
  277. color: var(--text-color);
  278. }
  279. .icon-button:hover {
  280. color: #66b0ff;
  281. }
  282. }
  283. .header-buttons {
  284. position: absolute;
  285. right: 1rem;
  286. top: 1rem;
  287. display: flex;
  288. align-items: center;
  289. gap: 10px;
  290. z-index: 1000;
  291. }
  292. .theme-toggle,
  293. .signup-btn {
  294. background-color: rgba(255, 255, 255, 0.2);
  295. color: white;
  296. border: none;
  297. border-radius: 6px;
  298. width: 40px;
  299. height: 40px;
  300. font-size: 20px;
  301. line-height: 1;
  302. display: flex;
  303. align-items: center;
  304. justify-content: center;
  305. cursor: pointer;
  306. }
  307. .theme-toggle:hover,
  308. .signup-btn:hover {
  309. background-color: rgba(255, 255, 255, 0.35);
  310. }
  311. .chat-sidebar {
  312. width: 200px;
  313. background-color: #202020;
  314. color: white;
  315. padding: 1rem;
  316. }
  317. .chat-title {
  318. font-weight: bold;
  319. margin-bottom: 1rem;
  320. }
  321. .chat-item {
  322. padding: 0.5rem;
  323. cursor: pointer;
  324. border-radius: 4px;
  325. }
  326. .chat-item:hover {
  327. background-color: #333;
  328. }
  329. </style>
  330. </head>
  331. <body>
  332. <div class="chat-container">
  333. <div class="chat-header">📎 Chatbot Documentaire</div>
  334. <div class="header-buttons">
  335. <a href="/developpeur" class="theme-toggle" title="Monitoring développeur" style="text-decoration:none;color:inherit;font-size:0.9rem;">🔧 Dev</a>
  336. <button class="theme-toggle" id="themeToggle" title="Changer de thème">🌙</button>
  337. <button class="fas fa-user-alt" id="signupBtn" title="Créer un compte" onclick="window.location.href='/register';"></button>
  338. </div>
  339. <div class="chat-body">
  340. <div class="chat-sidebar" id="chatSidebar">
  341. <div class="chat-title">💬 Vos Chats</div>
  342. <div id="chatList"></div>
  343. <button onclick="addNewChat()">➕ Nouveau Chat</button>
  344. <div class="chat-item">🗂️ Chat du 1 août</div>
  345. <div class="chat-item">🗂️ Chat du 2 août</div>
  346. </div>
  347. <div class="chat-main">
  348. <div class="chat-history" id="chatHistory"></div>
  349. <div class="chat-input">
  350. <form id="questionForm" enctype="multipart/form-data">
  351. <label class="upload-label" for="file">📁 Ajouter un fichier (PDF, TXT, Image)</label>
  352. <input type="file" id="file" name="file" accept=".pdf,.txt,image/*" />
  353. <div class="file-preview" id="filePreview" style="display: none;"></div>
  354. <input type="text" id="question" name="question" placeholder="Posez votre question…" autocomplete="off" required />
  355. <button type="submit">Envoyer</button>
  356. </form>
  357. </div>
  358. </div>
  359. </div>
  360. <div id="status" style="color: gray; margin-top: 0.5rem;"></div>
  361. </div>
  362. <script>
  363. const chatList = document.getElementById("chatList");
  364. let chatSessions = [];
  365. let currentChatIndex = -1;
  366. const chatHistoryDiv = document.getElementById("chatHistory");
  367. const questionForm = document.getElementById("questionForm");
  368. const questionInput = document.getElementById("question");
  369. const status = document.getElementById("status");
  370. const fileInput = document.getElementById("file");
  371. const filePreview = document.getElementById("filePreview");
  372. // Affiche nom fichier sélectionné
  373. fileInput.addEventListener('change', () => {
  374. if (fileInput.files.length > 0) {
  375. filePreview.style.display = 'block';
  376. filePreview.textContent = `Fichier sélectionné : ${fileInput.files[0].name}`;
  377. } else {
  378. filePreview.style.display = 'none';
  379. filePreview.textContent = '';
  380. }
  381. });
  382. // Fonction ajout message
  383. function appendMessage(sender, text) {
  384. const message = document.createElement("div");
  385. message.className = `message ${sender}`;
  386. const avatar = document.createElement("div");
  387. avatar.className = "avatar";
  388. avatar.textContent = sender === "user" ? "👤" : "🤖";
  389. const bubble = document.createElement("div");
  390. bubble.className = "bubble";
  391. bubble.textContent = text;
  392. message.appendChild(avatar);
  393. message.appendChild(bubble);
  394. chatHistoryDiv.appendChild(message);
  395. chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight;
  396. // 🧠 Ajouter au tableau de sessions
  397. if (currentChatIndex >= 0) {
  398. chatSessions[currentChatIndex].messages.push({ sender, text });
  399. }
  400. }
  401. // pour afficher la liste de tous les chats dans la barre latérale
  402. function renderChatList() {
  403. chatList.innerHTML = "";
  404. chatSessions.forEach((session, index) => {
  405. const item = document.createElement("div");
  406. item.className = "chat-item";
  407. item.textContent = `${session.icon} ${session.label}`;
  408. item.addEventListener("click", () => {
  409. loadChat(index);
  410. });
  411. chatList.appendChild(item);
  412. });
  413. }
  414. // Fonction pour charger un ancien chat
  415. function loadChat(index) {
  416. currentChatIndex = index;
  417. chatHistoryDiv.innerHTML = "";
  418. chatSessions[index].messages.forEach(({ sender, text }) => {
  419. appendMessage(sender, text);
  420. });
  421. }
  422. // Charger historique si tu as cette route backend
  423. async function loadChatHistory() {
  424. try {
  425. const response = await fetch("/chat_history");
  426. if (response.ok) {
  427. const chatHistory = await response.json();
  428. chatHistoryDiv.innerHTML = "";
  429. chatHistory.forEach(({ question, answer }) => {
  430. appendMessage("user", question);
  431. appendMessage("bot", answer);
  432. });
  433. }
  434. } catch (error) {
  435. console.warn("Erreur chargement historique :", error);
  436. }
  437. }
  438. loadChatHistory();
  439. // Gestion submit formulaire unique
  440. questionForm.addEventListener("submit", async (e) => {
  441. e.preventDefault();
  442. const question = questionInput.value.trim();
  443. if (!question) return;
  444. appendMessage("user", question);
  445. status.textContent = "En attente de la réponse...";
  446. // Si fichier sélectionné, on envoie en multipart/form-data vers /upload_file
  447. if (fileInput.files.length > 0) {
  448. const formData = new FormData();
  449. formData.append("file", fileInput.files[0]);
  450. formData.append("question", question);
  451. try {
  452. const res = await fetch("/upload_file", {
  453. method: "POST",
  454. body: formData,
  455. });
  456. if (res.ok) {
  457. const data = await res.json();
  458. appendMessage("bot", data.answer || "Réponse reçue.");
  459. } else {
  460. appendMessage("bot", `Erreur serveur: ${res.status}`);
  461. }
  462. } catch (error) {
  463. appendMessage("bot", "Erreur réseau ou serveur.");
  464. console.error(error);
  465. }
  466. } else {
  467. // Pas de fichier, on envoie JSON vers /qa_chain
  468. try {
  469. const res = await fetch("/qa_chain", {
  470. method: "POST",
  471. headers: { "Content-Type": "application/json" },
  472. body: JSON.stringify({ query: question }),
  473. });
  474. if (res.ok) {
  475. const data = await res.json();
  476. appendMessage("bot", data.answer || "Réponse reçue.");
  477. } else {
  478. appendMessage("bot", `Erreur serveur: ${res.status}`);
  479. }
  480. } catch (error) {
  481. appendMessage("bot", "Erreur réseau ou serveur.");
  482. console.error(error);
  483. }
  484. }
  485. status.textContent = "";
  486. questionInput.value = "";
  487. fileInput.value = "";
  488. filePreview.style.display = 'none';
  489. });
  490. const themeToggle = document.getElementById("themeToggle");
  491. function setTheme(mode) {
  492. document.body.classList.toggle("dark", mode === "dark");
  493. themeToggle.textContent = mode === "dark" ? "☀️" : "🌙";
  494. localStorage.setItem("theme", mode);
  495. }
  496. themeToggle.addEventListener("click", () => {
  497. const currentMode = document.body.classList.contains("dark") ? "dark" : "light";
  498. const newMode = currentMode === "dark" ? "light" : "dark";
  499. setTheme(newMode);
  500. });
  501. // Appliquer le thème sauvegardé
  502. const savedTheme = localStorage.getItem("theme") || "light";
  503. setTheme(savedTheme);
  504. function addNewChat() {
  505. const today = new Date().toLocaleDateString('fr-FR', {
  506. day: 'numeric',
  507. month: 'long',
  508. year: 'numeric'
  509. });
  510. const newChat = {
  511. label: `Chat du ${today}`,
  512. icon: "🗂️",
  513. messages: []
  514. };
  515. chatSessions.push(newChat);
  516. currentChatIndex = chatSessions.length - 1;
  517. // Vider l'historique affiché
  518. chatHistoryDiv.innerHTML = "";
  519. // Re-render la liste
  520. renderChatList();
  521. }
  522. </script>
  523. </body>
  524. </html>