index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <!DOCTYPE html>
  2. <html lang="fr">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Smart File Chatbot</title>
  6. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet" />
  7. <style>
  8. body {
  9. font-family: 'Inter', sans-serif;
  10. background-color: #f4f6f8;
  11. margin: 0;
  12. padding: 2rem;
  13. display: flex;
  14. justify-content: center;
  15. flex-direction: column;
  16. align-items: center;
  17. min-height: 100vh;
  18. }
  19. .container {
  20. background: white;
  21. padding: 2rem;
  22. border-radius: 12px;
  23. box-shadow: 0 0 10px rgba(0,0,0,0.05);
  24. max-width: 600px;
  25. width: 100%;
  26. /* display none until login */
  27. display: none;
  28. flex-direction: column;
  29. }
  30. h2 {
  31. margin-top: 0;
  32. color: #333;
  33. }
  34. /* Login popup overlay */
  35. #loginPopup {
  36. position: fixed;
  37. top: 0; left: 0; right: 0; bottom: 0;
  38. background: rgba(0,0,0,0.5);
  39. display: flex;
  40. justify-content: center;
  41. align-items: center;
  42. z-index: 9999;
  43. }
  44. #loginForm {
  45. background: white;
  46. padding: 2rem;
  47. border-radius: 12px;
  48. width: 320px;
  49. box-shadow: 0 0 15px rgba(0,0,0,0.2);
  50. }
  51. #loginForm h3 {
  52. margin-top: 0;
  53. margin-bottom: 1rem;
  54. text-align: center;
  55. }
  56. #loginForm input {
  57. width: 100%;
  58. padding: 0.75rem;
  59. margin-bottom: 1rem;
  60. border: 1px solid #ccc;
  61. border-radius: 6px;
  62. font-size: 1rem;
  63. }
  64. #loginForm button {
  65. width: 100%;
  66. padding: 0.75rem;
  67. background: #0066cc;
  68. color: white;
  69. border: none;
  70. border-radius: 6px;
  71. font-size: 1rem;
  72. font-weight: 600;
  73. cursor: pointer;
  74. }
  75. #loginForm button:hover {
  76. background: #0051a8;
  77. }
  78. #loginError {
  79. color: red;
  80. margin-bottom: 1rem;
  81. text-align: center;
  82. }
  83. /* Upload area styles */
  84. .upload-area {
  85. display: flex;
  86. align-items: center;
  87. gap: 10px;
  88. border: 2px dashed #aaa;
  89. padding: 1rem;
  90. border-radius: 8px;
  91. background-color: #fafafa;
  92. cursor: pointer;
  93. user-select: none;
  94. }
  95. .file-info {
  96. display: flex;
  97. justify-content: space-between;
  98. align-items: center;
  99. margin-top: 1rem;
  100. background: #eef3f7;
  101. padding: 0.5rem 1rem;
  102. border-radius: 6px;
  103. }
  104. .remove-btn {
  105. background: none;
  106. border: 1px solid rgb(191, 221, 220);
  107. color: rgb(168, 224, 224);
  108. width: 24px;
  109. height: 24px;
  110. font-size: 1rem;
  111. line-height: 1rem;
  112. padding: 0;
  113. border-radius: 4px;
  114. cursor: pointer;
  115. display: flex;
  116. align-items: center;
  117. justify-content: center;
  118. transition: background 0.2s;
  119. }
  120. .remove-btn:hover {
  121. background: #cceeff;
  122. color: #0066cc;
  123. border-color: #0066cc;
  124. }
  125. input[type="file"] {
  126. display: none;
  127. }
  128. textarea, input[type="text"] {
  129. width: 100%;
  130. padding: 0.75rem;
  131. border: 1px solid #ccc;
  132. border-radius: 6px;
  133. font-size: 1rem;
  134. margin-top: 1rem;
  135. box-sizing: border-box;
  136. }
  137. button {
  138. margin-top: 1rem;
  139. width: 100%;
  140. padding: 0.75rem;
  141. background: #0066cc;
  142. color: white;
  143. border: none;
  144. border-radius: 6px;
  145. font-size: 1rem;
  146. font-weight: 600;
  147. cursor: pointer;
  148. }
  149. button:hover {
  150. background: #0051a8;
  151. }
  152. .answer-box {
  153. margin-top: 1.5rem;
  154. background: #f0f7ff;
  155. padding: 1rem;
  156. border-radius: 6px;
  157. color: #333;
  158. white-space: pre-wrap;
  159. min-height: 50px;
  160. }
  161. .source-box {
  162. font-size: 0.9rem;
  163. color: #555;
  164. margin-top: 0.5rem;
  165. min-height: 20px;
  166. }
  167. #chat-history {
  168. white-space: pre-wrap;
  169. border: 1px solid #ccc;
  170. padding: 10px;
  171. max-height: 300px;
  172. overflow-y: auto;
  173. margin-bottom: 1rem;
  174. width: 100%;
  175. max-width: 600px;
  176. background: white;
  177. border-radius: 12px;
  178. box-shadow: 0 0 10px rgba(0,0,0,0.05);
  179. color: #333;
  180. }
  181. /* Make questions in chat history appear in blue */
  182. #chat-history span.question {
  183. color: blue;
  184. font-weight: 600;
  185. display: block;
  186. margin-bottom: 4px;
  187. }
  188. #chat-history span.answer {
  189. color: #333;
  190. display: block;
  191. margin-bottom: 12px;
  192. white-space: pre-wrap;
  193. }
  194. </style>
  195. </head>
  196. <body>
  197. <!-- Login popup -->
  198. <div id="loginPopup">
  199. <form id="loginForm">
  200. <h3>Connexion Wiki Intersel</h3>
  201. <div id="loginError"></div>
  202. <input type="text" id="username" placeholder="Nom d'utilisateur" required />
  203. <input type="password" id="password" placeholder="Mot de passe" required />
  204. <button type="submit">Se connecter</button>
  205. </form>
  206. </div>
  207. <!-- Chatbot container (hidden until login) -->
  208. <div class="container" id="chatbotContainer">
  209. <h2>📎 Chatbot Documentaire</h2>
  210. <!-- Historique de chat -->
  211. <div id="chat-history"></div>
  212. <form id="questionForm">
  213. <label class="upload-area" for="file">
  214. 📁 Cliquez ici pour ajouter un fichier
  215. </label>
  216. <input type="file" id="file" accept=".pdf,.txt,image/*" />
  217. <div class="file-info" id="filePreview" style="display:none;">
  218. <span id="fileName"></span>
  219. <button type="button" class="remove-btn" id="clearFileBtn" title="Supprimer le fichier">❌</button>
  220. </div>
  221. <input type="text" id="question" placeholder="Posez votre question…" autocomplete="off" />
  222. <button type="submit">Envoyer</button>
  223. </form>
  224. <div class="answer-box" id="answer" style="display:none;"></div>
  225. <div class="source-box" id="source"></div>
  226. </div>
  227. <script>
  228. // Elements for login
  229. const loginPopup = document.getElementById("loginPopup");
  230. const loginForm = document.getElementById("loginForm");
  231. const loginError = document.getElementById("loginError");
  232. const chatbotContainer = document.getElementById("chatbotContainer");
  233. // Show login popup on page load, hide chatbot until login success
  234. window.onload = () => {
  235. loginPopup.style.display = "flex";
  236. chatbotContainer.style.display = "none";
  237. };
  238. loginForm.addEventListener("submit", async (e) => {
  239. e.preventDefault();
  240. loginError.textContent = ""; // Clear previous errors
  241. const username = document.getElementById("username").value.trim();
  242. const password = document.getElementById("password").value.trim();
  243. if (!username || !password) {
  244. loginError.textContent = "Veuillez remplir tous les champs.";
  245. return;
  246. }
  247. try {
  248. const response = await fetch("/login", {
  249. method: "POST",
  250. headers: { "Content-Type": "application/json" },
  251. body: JSON.stringify({ username, password }),
  252. });
  253. if (response.ok) {
  254. // Login success
  255. loginPopup.style.display = "none";
  256. chatbotContainer.style.display = "flex";
  257. loadChatHistory();
  258. } else {
  259. const errorData = await response.json();
  260. loginError.textContent = errorData.error || "Erreur d'authentification";
  261. }
  262. } catch (err) {
  263. loginError.textContent = "Erreur réseau ou serveur.";
  264. }
  265. });
  266. // File upload preview and clear
  267. const fileInput = document.getElementById("file");
  268. const filePreview = document.getElementById("filePreview");
  269. const fileNameDisplay = document.getElementById("fileName");
  270. fileInput.addEventListener("change", () => {
  271. if (fileInput.files.length > 0) {
  272. fileNameDisplay.textContent = fileInput.files[0].name;
  273. filePreview.style.display = "flex";
  274. }
  275. });
  276. document.getElementById("clearFileBtn").onclick = () => {
  277. fileInput.value = "";
  278. filePreview.style.display = "none";
  279. };
  280. // Handle question submission
  281. document.getElementById("questionForm").onsubmit = async (e) => {
  282. e.preventDefault();
  283. const questionInput = document.getElementById("question");
  284. const question = questionInput.value.trim();
  285. const answerBox = document.getElementById("answer");
  286. const sourceBox = document.getElementById("source");
  287. if (!question) {
  288. answerBox.style.display = "block";
  289. answerBox.textContent = "❗ Veuillez entrer une question.";
  290. sourceBox.textContent = "";
  291. return;
  292. }
  293. answerBox.style.display = "block";
  294. answerBox.textContent = "⏳ Recherche de la réponse...";
  295. sourceBox.textContent = "";
  296. try {
  297. let response;
  298. if (fileInput.files.length > 0) {
  299. const formData = new FormData();
  300. formData.append("file", fileInput.files[0]);
  301. formData.append("question", question);
  302. response = await fetch("/upload_file", {
  303. method: "POST",
  304. body: formData,
  305. });
  306. } else {
  307. response = await fetch("/qa_chain", {
  308. method: "POST",
  309. headers: {
  310. "Content-Type": "application/json",
  311. },
  312. body: JSON.stringify({ question }),
  313. });
  314. }
  315. if (response.ok) {
  316. const data = await response.json();
  317. answerBox.textContent = data.answer || "Pas de réponse reçue.";
  318. if (data.source) {
  319. sourceBox.textContent = "Source: " + data.source;
  320. } else {
  321. sourceBox.textContent = "";
  322. }
  323. questionInput.value = "";
  324. fileInput.value = "";
  325. filePreview.style.display = "none";
  326. loadChatHistory();
  327. } else {
  328. answerBox.textContent = "❌ Erreur lors de la récupération de la réponse.";
  329. sourceBox.textContent = "";
  330. }
  331. } catch (error) {
  332. answerBox.textContent = "❌ Erreur réseau ou serveur.";
  333. sourceBox.textContent = "";
  334. }
  335. };
  336. // Chat history display
  337. const chatHistoryDiv = document.getElementById("chat-history");
  338. async function loadChatHistory() {
  339. try {
  340. const response = await fetch("/chat_history");
  341. if (response.ok) {
  342. const chatHistory = await response.json();
  343. chatHistoryDiv.innerHTML = "";
  344. chatHistory.forEach(({ question, answer, sources }) => {
  345. const questionSpan = document.createElement("span");
  346. questionSpan.className = "question";
  347. questionSpan.textContent = "Q: " + question;
  348. const sourceSpan = document.createElement("span");
  349. sourceSpan.style.fontSize = "0.9rem";
  350. sourceSpan.style.color = "#777";
  351. sourceSpan.textContent = sources && sources.length
  352. ? "🔗 Source: " + sources.join(", ")
  353. : "🔗 Source: inconnue";
  354. const answerSpan = document.createElement("span");
  355. answerSpan.className = "answer";
  356. answerSpan.textContent = "A: " + answer;
  357. chatHistoryDiv.appendChild(questionSpan);
  358. chatHistoryDiv.appendChild(sourceSpan);
  359. chatHistoryDiv.appendChild(answerSpan);
  360. });
  361. chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight;
  362. }
  363. } catch (e) {
  364. // silent fail
  365. }
  366. }
  367. </script>
  368. </body>
  369. </html>