| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8" />
- <title>Smart File Chatbot</title>
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet" />
- <style>
- body {
- font-family: 'Inter', sans-serif;
- background-color: #f4f6f8;
- margin: 0;
- padding: 2rem;
- display: flex;
- justify-content: center;
- flex-direction: column;
- align-items: center;
- min-height: 100vh;
- }
- .container {
- background: white;
- padding: 2rem;
- border-radius: 12px;
- box-shadow: 0 0 10px rgba(0,0,0,0.05);
- max-width: 600px;
- width: 100%;
- /* display none until login */
- display: none;
- flex-direction: column;
- }
- h2 {
- margin-top: 0;
- color: #333;
- }
- /* Login popup overlay */
- #loginPopup {
- position: fixed;
- top: 0; left: 0; right: 0; bottom: 0;
- background: rgba(0,0,0,0.5);
- display: flex;
- justify-content: center;
- align-items: center;
- z-index: 9999;
- }
- #loginForm {
- background: white;
- padding: 2rem;
- border-radius: 12px;
- width: 320px;
- box-shadow: 0 0 15px rgba(0,0,0,0.2);
- }
- #loginForm h3 {
- margin-top: 0;
- margin-bottom: 1rem;
- text-align: center;
- }
- #loginForm input {
- width: 100%;
- padding: 0.75rem;
- margin-bottom: 1rem;
- border: 1px solid #ccc;
- border-radius: 6px;
- font-size: 1rem;
- }
- #loginForm button {
- width: 100%;
- padding: 0.75rem;
- background: #0066cc;
- color: white;
- border: none;
- border-radius: 6px;
- font-size: 1rem;
- font-weight: 600;
- cursor: pointer;
- }
- #loginForm button:hover {
- background: #0051a8;
- }
- #loginError {
- color: red;
- margin-bottom: 1rem;
- text-align: center;
- }
- /* Upload area styles */
- .upload-area {
- display: flex;
- align-items: center;
- gap: 10px;
- border: 2px dashed #aaa;
- padding: 1rem;
- border-radius: 8px;
- background-color: #fafafa;
- cursor: pointer;
- user-select: none;
- }
- .file-info {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-top: 1rem;
- background: #eef3f7;
- padding: 0.5rem 1rem;
- border-radius: 6px;
- }
- .remove-btn {
- background: none;
- border: 1px solid rgb(191, 221, 220);
- color: rgb(168, 224, 224);
- width: 24px;
- height: 24px;
- font-size: 1rem;
- line-height: 1rem;
- padding: 0;
- border-radius: 4px;
- cursor: pointer;
- display: flex;
- align-items: center;
- justify-content: center;
- transition: background 0.2s;
- }
- .remove-btn:hover {
- background: #cceeff;
- color: #0066cc;
- border-color: #0066cc;
- }
- input[type="file"] {
- display: none;
- }
- textarea, input[type="text"] {
- width: 100%;
- padding: 0.75rem;
- border: 1px solid #ccc;
- border-radius: 6px;
- font-size: 1rem;
- margin-top: 1rem;
- box-sizing: border-box;
- }
- button {
- margin-top: 1rem;
- width: 100%;
- padding: 0.75rem;
- background: #0066cc;
- color: white;
- border: none;
- border-radius: 6px;
- font-size: 1rem;
- font-weight: 600;
- cursor: pointer;
- }
- button:hover {
- background: #0051a8;
- }
- .answer-box {
- margin-top: 1.5rem;
- background: #f0f7ff;
- padding: 1rem;
- border-radius: 6px;
- color: #333;
- white-space: pre-wrap;
- min-height: 50px;
- }
- .source-box {
- font-size: 0.9rem;
- color: #555;
- margin-top: 0.5rem;
- min-height: 20px;
- }
- #chat-history {
- white-space: pre-wrap;
- border: 1px solid #ccc;
- padding: 10px;
- max-height: 300px;
- overflow-y: auto;
- margin-bottom: 1rem;
- width: 100%;
- max-width: 600px;
- background: white;
- border-radius: 12px;
- box-shadow: 0 0 10px rgba(0,0,0,0.05);
- color: #333;
- }
- /* Make questions in chat history appear in blue */
- #chat-history span.question {
- color: blue;
- font-weight: 600;
- display: block;
- margin-bottom: 4px;
- }
- #chat-history span.answer {
- color: #333;
- display: block;
- margin-bottom: 12px;
- white-space: pre-wrap;
- }
- </style>
- </head>
- <body>
- <!-- Login popup -->
- <div id="loginPopup">
- <form id="loginForm">
- <h3>Connexion Wiki Intersel</h3>
- <div id="loginError"></div>
- <input type="text" id="username" placeholder="Nom d'utilisateur" required />
- <input type="password" id="password" placeholder="Mot de passe" required />
- <button type="submit">Se connecter</button>
- </form>
- </div>
- <!-- Chatbot container (hidden until login) -->
- <div class="container" id="chatbotContainer">
- <h2>📎 Chatbot Documentaire</h2>
- <!-- Historique de chat -->
- <div id="chat-history"></div>
- <form id="questionForm">
- <label class="upload-area" for="file">
- 📁 Cliquez ici pour ajouter un fichier
- </label>
- <input type="file" id="file" accept=".pdf,.txt,image/*" />
- <div class="file-info" id="filePreview" style="display:none;">
- <span id="fileName"></span>
- <button type="button" class="remove-btn" id="clearFileBtn" title="Supprimer le fichier">❌</button>
- </div>
- <input type="text" id="question" placeholder="Posez votre question…" autocomplete="off" />
- <button type="submit">Envoyer</button>
- </form>
- <div class="answer-box" id="answer" style="display:none;"></div>
- <div class="source-box" id="source"></div>
- </div>
- <script>
- // Elements for login
- const loginPopup = document.getElementById("loginPopup");
- const loginForm = document.getElementById("loginForm");
- const loginError = document.getElementById("loginError");
- const chatbotContainer = document.getElementById("chatbotContainer");
- // Show login popup on page load, hide chatbot until login success
- window.onload = () => {
- loginPopup.style.display = "flex";
- chatbotContainer.style.display = "none";
- };
- loginForm.addEventListener("submit", async (e) => {
- e.preventDefault();
- loginError.textContent = ""; // Clear previous errors
- const username = document.getElementById("username").value.trim();
- const password = document.getElementById("password").value.trim();
- if (!username || !password) {
- loginError.textContent = "Veuillez remplir tous les champs.";
- return;
- }
- try {
- const response = await fetch("/login", {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ username, password }),
- });
- if (response.ok) {
- // Login success
- loginPopup.style.display = "none";
- chatbotContainer.style.display = "flex";
- loadChatHistory();
- } else {
- const errorData = await response.json();
- loginError.textContent = errorData.error || "Erreur d'authentification";
- }
- } catch (err) {
- loginError.textContent = "Erreur réseau ou serveur.";
- }
- });
- // File upload preview and clear
- const fileInput = document.getElementById("file");
- const filePreview = document.getElementById("filePreview");
- const fileNameDisplay = document.getElementById("fileName");
- fileInput.addEventListener("change", () => {
- if (fileInput.files.length > 0) {
- fileNameDisplay.textContent = fileInput.files[0].name;
- filePreview.style.display = "flex";
- }
- });
- document.getElementById("clearFileBtn").onclick = () => {
- fileInput.value = "";
- filePreview.style.display = "none";
- };
- // Handle question submission
- document.getElementById("questionForm").onsubmit = async (e) => {
- e.preventDefault();
- const questionInput = document.getElementById("question");
- const question = questionInput.value.trim();
- const answerBox = document.getElementById("answer");
- const sourceBox = document.getElementById("source");
- if (!question) {
- answerBox.style.display = "block";
- answerBox.textContent = "❗ Veuillez entrer une question.";
- sourceBox.textContent = "";
- return;
- }
- answerBox.style.display = "block";
- answerBox.textContent = "⏳ Recherche de la réponse...";
- sourceBox.textContent = "";
- try {
- let response;
- if (fileInput.files.length > 0) {
- const formData = new FormData();
- formData.append("file", fileInput.files[0]);
- formData.append("question", question);
- response = await fetch("/upload_file", {
- method: "POST",
- body: formData,
- });
- } else {
- response = await fetch("/qa_chain", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({ question }),
- });
- }
- if (response.ok) {
- const data = await response.json();
- answerBox.textContent = data.answer || "Pas de réponse reçue.";
- if (data.source) {
- sourceBox.textContent = "Source: " + data.source;
- } else {
- sourceBox.textContent = "";
- }
- questionInput.value = "";
- fileInput.value = "";
- filePreview.style.display = "none";
- loadChatHistory();
- } else {
- answerBox.textContent = "❌ Erreur lors de la récupération de la réponse.";
- sourceBox.textContent = "";
- }
- } catch (error) {
- answerBox.textContent = "❌ Erreur réseau ou serveur.";
- sourceBox.textContent = "";
- }
- };
- // Chat history display
- const chatHistoryDiv = document.getElementById("chat-history");
- async function loadChatHistory() {
- try {
- const response = await fetch("/chat_history");
- if (response.ok) {
- const chatHistory = await response.json();
- chatHistoryDiv.innerHTML = "";
- chatHistory.forEach(({ question, answer, sources }) => {
- const questionSpan = document.createElement("span");
- questionSpan.className = "question";
- questionSpan.textContent = "Q: " + question;
- const sourceSpan = document.createElement("span");
- sourceSpan.style.fontSize = "0.9rem";
- sourceSpan.style.color = "#777";
- sourceSpan.textContent = sources && sources.length
- ? "🔗 Source: " + sources.join(", ")
- : "🔗 Source: inconnue";
- const answerSpan = document.createElement("span");
- answerSpan.className = "answer";
- answerSpan.textContent = "A: " + answer;
- chatHistoryDiv.appendChild(questionSpan);
- chatHistoryDiv.appendChild(sourceSpan);
- chatHistoryDiv.appendChild(answerSpan);
- });
- chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight;
- }
- } catch (e) {
- // silent fail
- }
- }
- </script>
- </body>
- </html>
|