ParthSadaria commited on
Commit
acd95c4
·
verified ·
1 Parent(s): 13bad0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -4
app.py CHANGED
@@ -30,6 +30,7 @@ def get_admin_user(credentials: HTTPBasicCredentials = Depends(security)):
30
  # In-memory data stores
31
  chat_history: Dict[str, List[Dict[str, str]]] = {}
32
  rules: List[str] = [
 
33
  "Be respectful and helpful",
34
  "Do not generate harmful content",
35
  "Provide accurate information"
@@ -135,9 +136,106 @@ async def admin_panel(username: str = Depends(get_admin_user)):
135
  html = f"""
136
  <!DOCTYPE html>
137
  <html>
138
- <head><title>Admin Panel</title></head>
139
- <link rel="stylesheet" href="/static/style.css">
140
- <script src="/static/script.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  <body>
142
  <h1>Admin</h1>
143
 
@@ -145,7 +243,7 @@ async def admin_panel(username: str = Depends(get_admin_user)):
145
  <label>Model:</label>
146
  <select id="model-selector">{opts}</select>
147
  <button onclick="updateModel()">Update</button>
148
- <p>Current model: <strong>{current_model}</strong> (Applies to all chats)</p>
149
  </div>
150
  <div>
151
  <h2>Rules</h2>
 
30
  # In-memory data stores
31
  chat_history: Dict[str, List[Dict[str, str]]] = {}
32
  rules: List[str] = [
33
+ "You are Vetra, an office bot that answers based on given rules",
34
  "Be respectful and helpful",
35
  "Do not generate harmful content",
36
  "Provide accurate information"
 
136
  html = f"""
137
  <!DOCTYPE html>
138
  <html>
139
+ <head>
140
+ <title>Admin Panel</title>
141
+ <link rel="stylesheet" href="/static/style.css">
142
+ <script>
143
+ // Inline JavaScript for admin panel
144
+ function updateModel() {{
145
+ const model = document.getElementById('model-selector').value;
146
+ fetch('/api/model', {{
147
+ method: 'POST',
148
+ headers: {{'Content-Type': 'application/json'}},
149
+ body: JSON.stringify({{ model: model }})
150
+ }})
151
+ .then(response => response.json())
152
+ .then(data => {{
153
+ document.getElementById('current-model-display').textContent = data.model;
154
+ alert('Model updated successfully!');
155
+ }})
156
+ .catch(error => {{
157
+ console.error('Error updating model:', error);
158
+ alert('Error updating model: ' + error);
159
+ }});
160
+ }}
161
+
162
+ function addRule() {{
163
+ const ruleInput = document.getElementById('new-rule');
164
+ const rule = ruleInput.value.trim();
165
+ if (!rule) return;
166
+
167
+ fetch('/api/rules', {{
168
+ method: 'POST',
169
+ headers: {{'Content-Type': 'application/json'}},
170
+ body: JSON.stringify({{ rule: rule }})
171
+ }})
172
+ .then(response => response.json())
173
+ .then(data => {{
174
+ ruleInput.value = '';
175
+ refreshRules(data.rules);
176
+ }})
177
+ .catch(error => {{
178
+ console.error('Error adding rule:', error);
179
+ alert('Error adding rule: ' + error);
180
+ }});
181
+ }}
182
+
183
+ function deleteRule(index) {{
184
+ fetch(`/api/rules/${{index}}`, {{
185
+ method: 'DELETE'
186
+ }})
187
+ .then(response => response.json())
188
+ .then(data => {{
189
+ refreshRules(data.rules);
190
+ }})
191
+ .catch(error => {{
192
+ console.error('Error deleting rule:', error);
193
+ alert('Error deleting rule: ' + error);
194
+ }});
195
+ }}
196
+
197
+ function refreshRules(rules) {{
198
+ const rulesList = document.getElementById('rules-list');
199
+ rulesList.innerHTML = '';
200
+ rules.forEach((rule, index) => {{
201
+ const li = document.createElement('li');
202
+ li.textContent = rule + ' ';
203
+
204
+ const deleteBtn = document.createElement('button');
205
+ deleteBtn.textContent = 'Delete';
206
+ deleteBtn.onclick = function() {{ deleteRule(index); }};
207
+
208
+ li.appendChild(deleteBtn);
209
+ rulesList.appendChild(li);
210
+ }});
211
+ }}
212
+
213
+ function viewChat(chatId) {{
214
+ fetch(`/api/chats/${{chatId}}`)
215
+ .then(response => response.json())
216
+ .then(data => {{
217
+ const chatViewer = document.getElementById('chat-viewer');
218
+ chatViewer.innerHTML = `<h3>Chat: ${{chatId}}</h3>`;
219
+
220
+ const chatList = document.createElement('ul');
221
+ chatList.className = 'chat-messages';
222
+
223
+ data.history.forEach(msg => {{
224
+ const li = document.createElement('li');
225
+ li.className = `message ${{msg.role}}`;
226
+ li.innerHTML = `<strong>${{msg.role}}:</strong> ${{msg.content}}`;
227
+ chatList.appendChild(li);
228
+ }});
229
+
230
+ chatViewer.appendChild(chatList);
231
+ }})
232
+ .catch(error => {{
233
+ console.error('Error fetching chat:', error);
234
+ alert('Error fetching chat: ' + error);
235
+ }});
236
+ }}
237
+ </script>
238
+ </head>
239
  <body>
240
  <h1>Admin</h1>
241
 
 
243
  <label>Model:</label>
244
  <select id="model-selector">{opts}</select>
245
  <button onclick="updateModel()">Update</button>
246
+ <p>Current model: <strong id="current-model-display">{current_model}</strong> (Applies to all chats)</p>
247
  </div>
248
  <div>
249
  <h2>Rules</h2>