File size: 10,829 Bytes
040db21
37619a6
040db21
 
0d2d16d
 
37619a6
 
040db21
37619a6
 
7ddeafc
 
 
 
 
 
37619a6
7ddeafc
 
 
37619a6
 
0d2d16d
7ddeafc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d2d16d
05be601
7ddeafc
05be601
37619a6
0d2d16d
7ddeafc
 
 
 
 
 
 
 
 
 
 
0d2d16d
 
 
040db21
0d2d16d
 
05be601
37619a6
 
 
 
 
7ddeafc
 
 
 
 
05be601
7ddeafc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d2f614
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ddeafc
1d2f614
 
 
05be601
1d2f614
 
 
05be601
1d2f614
 
 
 
 
7ddeafc
1d2f614
 
 
 
7ddeafc
 
 
 
37619a6
 
 
 
 
 
 
 
 
 
 
 
7ddeafc
 
 
 
 
 
 
37619a6
0d2d16d
05be601
37619a6
 
 
0d2d16d
7ddeafc
 
 
 
37619a6
 
0d2d16d
37619a6
 
 
 
 
 
 
 
7ddeafc
 
37619a6
 
0d2d16d
37619a6
 
 
 
 
0d2d16d
37619a6
 
 
 
0d2d16d
05be601
37619a6
 
 
0d2d16d
37619a6
 
 
 
 
 
 
 
0d2d16d
 
37619a6
 
 
 
 
0d2d16d
37619a6
 
 
0d2d16d
37619a6
05be601
37619a6
 
05be601
 
 
 
 
 
 
 
 
 
7ddeafc
 
 
 
 
 
 
37619a6
 
 
 
7ddeafc
05be601
 
 
 
 
 
 
 
7ddeafc
0d2d16d
040db21
0d2d16d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mariam AI</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script>
</head>
<body class="bg-gray-100 h-screen">
    <div class="container mx-auto p-4 h-full flex flex-col">
        <!-- Sidebar for conversations -->
        <div class="grid grid-cols-4 gap-4 h-full">
            <div class="bg-white rounded-lg shadow-lg p-4">
                <div class="flex flex-col h-full">
                    <button onclick="startNewConversation()" class="w-full bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 mb-4">
                        Nouvelle conversation
                    </button>
                    <div id="conversationsList" class="flex-1 overflow-y-auto space-y-2">
                        <!-- Conversations will be listed here -->
                    </div>
                </div>
            </div>

            <!-- Main chat area -->
            <div class="col-span-3 bg-white rounded-lg shadow-lg p-6 flex flex-col">
                <!-- Header -->
                <div class="flex justify-between items-center mb-6">
                    <h1 class="text-2xl font-bold">Mariam AI</h1>
                    <div class="flex space-x-4">
                        <label class="flex items-center">
                            <input type="checkbox" id="webSearchToggle" class="mr-2">
                            Activer la recherche web
                        </label>
                        <button onclick="clearChat()" class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600">
                            Effacer le chat
                        </button>
                    </div>
                </div>

                <!-- File Upload -->
                <div class="mb-4">
                    <input type="file" id="fileUpload" class="hidden" accept=".jpg,.jpeg,.png,.pdf,.txt,.mp3,.mp4">
                    <label for="fileUpload" class="cursor-pointer bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">
                        Télécharger un fichier
                    </label>
                    <span id="fileName" class="ml-2 text-gray-600"></span>
                </div>

                <!-- Chat Messages -->
                <div id="chatMessages" class="flex-1 overflow-y-auto mb-4 space-y-4 bg-gray-50 p-4 rounded-lg">
                    <!-- Messages will be displayed here -->
                </div>

                <!-- Input Area -->
                <div class="border-t pt-4">
                    <div class="flex space-x-4">
                        <input type="text" id="messageInput" 
                               class="flex-1 border rounded-lg px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500"
                               placeholder="Écrivez votre message...">
                        <button onclick="sendMessage()" 
                                class="bg-blue-500 text-white px-6 py-2 rounded-lg hover:bg-blue-600 transition duration-200">
                            Envoyer
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Global variables
        const messageInput = document.getElementById('messageInput');
        const chatMessages = document.getElementById('chatMessages');
        const webSearchToggle = document.getElementById('webSearchToggle');
        const fileUpload = document.getElementById('fileUpload');
        const fileName = document.getElementById('fileName');
        const conversationsList = document.getElementById('conversationsList');

        let conversations = [];
        let currentConversationId = null;

        // Conversation Management
        function startNewConversation() {
            const conversationId = Date.now().toString();
            const conversation = {
                id: conversationId,
                title: `Conversation ${conversations.length + 1}`,
                messages: []
            };
            
            conversations.push(conversation);
            currentConversationId = conversationId;
            updateConversationsList();
            clearChatMessages();
        }

        function updateConversationsList() {
            conversationsList.innerHTML = '';
            conversations.forEach(conv => {
                const convDiv = document.createElement('div');
                convDiv.className = `p-2 rounded cursor-pointer ${conv.id === currentConversationId ? 'bg-blue-100' : 'hover:bg-gray-100'}`;
                convDiv.onclick = () => loadConversation(conv.id);
                convDiv.textContent = conv.title;
                conversationsList.appendChild(convDiv);
            });
        }

        // Modifiez la fonction loadConversation :
async function loadConversation(conversationId) {
    if (conversationId === currentConversationId) return;
    
    try {
        // Informer le backend du changement de conversation
        const response = await fetch('/switch_conversation', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                conversation_id: conversationId
            })
        });

        const data = await response.json();
        if (data.error) {
            alert(`Erreur: ${data.error}`);
            return;
        }
        
        currentConversationId = conversationId;
        const conversation = conversations.find(c => c.id === conversationId);
        updateConversationsList();
        
        if (conversation) {
            clearChatMessages();
            
            conversation.messages.forEach(msg => {
                const messageDiv = document.createElement('div');
                messageDiv.className = `flex ${msg.isUser ? 'justify-end' : ''}`;
                
                const innerDiv = document.createElement('div');
                innerDiv.className = `max-w-3/4 p-3 rounded-lg ${msg.isUser ? 'bg-blue-500 text-white' : 'bg-gray-200'}`;
                innerDiv.innerHTML = marked.parse(msg.content);
                
                messageDiv.appendChild(innerDiv);
                chatMessages.appendChild(messageDiv);
            });
            
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }
    } catch (error) {
        alert(`Erreur lors du changement de conversation: ${error.message}`);
    }
}

        function clearChatMessages() {
            chatMessages.innerHTML = '';
        }

        function addMessage(content, isUser = false) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `flex ${isUser ? 'justify-end' : ''}`;
            
            const innerDiv = document.createElement('div');
            innerDiv.className = `max-w-3/4 p-3 rounded-lg ${isUser ? 'bg-blue-500 text-white' : 'bg-gray-200'}`;
            innerDiv.innerHTML = marked.parse(content);
            
            messageDiv.appendChild(innerDiv);
            chatMessages.appendChild(messageDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;

            if (currentConversationId) {
                const conversation = conversations.find(c => c.id === currentConversationId);
                if (conversation) {
                    conversation.messages.push({ content, isUser });
                }
            }
        }

        // Message Handling
        async function sendMessage() {
            const message = messageInput.value.trim();
            if (!message) return;

            if (!currentConversationId) {
                startNewConversation();
            }

            addMessage(message, true);
            messageInput.value = '';

            try {
                const response = await fetch('/send_message', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        message: message,
                        web_search: webSearchToggle.checked,
                        conversation_id: currentConversationId
                    })
                });

                const data = await response.json();
                if (data.error) {
                    addMessage(`Erreur: ${data.error}`);
                } else {
                    addMessage(data.response);
                }
            } catch (error) {
                addMessage(`Erreur: ${error.message}`);
            }
        }

        // File Upload Handling
        fileUpload.addEventListener('change', async (e) => {
            const file = e.target.files[0];
            if (!file) return;

            fileName.textContent = file.name;
            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();
                if (data.error) {
                    alert(`Erreur: ${data.error}`);
                } else {
                    addMessage(`Fichier téléchargé: ${file.name}`);
                }
            } catch (error) {
                alert(`Erreur: ${error.message}`);
            }
        });

        // Chat Clear Handling
        async function clearChat() {
            try {
                await fetch('/clear_chat', { 
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        conversation_id: currentConversationId
                    })
                });
                
                if (currentConversationId) {
                    const conversation = conversations.find(c => c.id === currentConversationId);
                    if (conversation) {
                        conversation.messages = [];
                    }
                }
                clearChatMessages();
            } catch (error) {
                alert(`Erreur: ${error.message}`);
            }
        }

        // Event Listeners
        messageInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });

        // Initialize
        startNewConversation();
    </script>
</body>
</html>