File size: 9,560 Bytes
040db21
37619a6
040db21
 
0d2d16d
 
37619a6
 
040db21
37619a6
 
7ddeafc
 
 
 
 
 
37619a6
7ddeafc
 
 
37619a6
 
0d2d16d
7ddeafc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d2d16d
7ddeafc
 
 
 
 
 
 
0d2d16d
7ddeafc
37619a6
0d2d16d
7ddeafc
 
 
 
 
 
 
 
 
 
 
0d2d16d
 
 
040db21
0d2d16d
 
37619a6
 
 
 
 
7ddeafc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37619a6
 
 
 
 
 
 
 
 
 
 
 
7ddeafc
 
 
 
 
 
 
 
37619a6
0d2d16d
37619a6
 
 
0d2d16d
7ddeafc
 
 
 
37619a6
 
0d2d16d
37619a6
 
 
 
 
 
 
 
7ddeafc
 
37619a6
 
0d2d16d
37619a6
 
 
 
 
0d2d16d
37619a6
 
 
 
0d2d16d
37619a6
 
 
 
 
0d2d16d
37619a6
 
 
0d2d16d
37619a6
 
 
 
 
 
 
 
0d2d16d
 
37619a6
 
 
 
 
0d2d16d
37619a6
 
 
0d2d16d
37619a6
 
 
 
7ddeafc
 
 
 
 
 
 
37619a6
 
 
 
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
<!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 with improved scrolling -->
                <div id="chatMessages" class="flex-1 overflow-y-auto mb-4 space-y-4 bg-gray-50 p-4 rounded-lg">
                    {% for message in messages %}
                    <div class="flex {% if message.role == 'user' %}justify-end{% endif %}">
                        <div class="max-w-3/4 p-3 rounded-lg {% if message.role == 'user' %}bg-blue-500 text-white{% else %}bg-gray-200{% endif %}">
                            {{ message.content }}
                        </div>
                    </div>
                    {% endfor %}
                </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>
        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;

        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);
            });
        }

        function loadConversation(conversationId) {
            currentConversationId = conversationId;
            const conversation = conversations.find(c => c.id === conversationId);
            updateConversationsList();
            
            if (conversation) {
                clearChatMessages();
                conversation.messages.forEach(msg => {
                    addMessage(msg.content, msg.isUser);
                });
            }
        }

        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;

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

        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}`);
            }
        }

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

        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}`);
            }
        });

        async function clearChat() {
            try {
                await fetch('/clear_chat', { method: 'POST' });
                if (currentConversationId) {
                    const conversation = conversations.find(c => c.id === currentConversationId);
                    if (conversation) {
                        conversation.messages = [];
                    }
                }
                clearChatMessages();
            } catch (error) {
                alert(`Erreur: ${error.message}`);
            }
        }

        // Start with a new conversation
        startNewConversation();
    </script>
</body>
</html>