Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import sys
|
3 |
import uvicorn
|
4 |
-
from fastapi import FastAPI, Query, HTTPException
|
5 |
from fastapi.responses import HTMLResponse
|
6 |
from starlette.middleware.cors import CORSMiddleware
|
7 |
from datasets import load_dataset, list_datasets
|
@@ -152,7 +152,7 @@ async def download_and_process_datasets():
|
|
152 |
unify_datasets()
|
153 |
upload_model_to_hub()
|
154 |
|
155 |
-
# Inicializar FastAPI
|
156 |
app = FastAPI()
|
157 |
|
158 |
# Configuraci贸n de CORS
|
@@ -194,151 +194,140 @@ async def index():
|
|
194 |
<meta charset="UTF-8">
|
195 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
196 |
<title>GPT-2 Chatbot</title>
|
|
|
|
|
197 |
<style>
|
198 |
-
/* Existing styles */
|
199 |
-
/* Add styles for the model selector */
|
200 |
-
.model-selector {
|
201 |
-
margin-bottom: 10px;
|
202 |
-
}
|
203 |
body {
|
|
|
204 |
font-family: Arial, sans-serif;
|
205 |
-
margin: 0;
|
206 |
-
padding: 0;
|
207 |
-
background-color: #f4f4f4;
|
208 |
}
|
209 |
.container {
|
210 |
max-width: 800px;
|
211 |
-
margin:
|
212 |
-
padding: 20px;
|
213 |
}
|
214 |
.chat-container {
|
215 |
-
background-color: #
|
216 |
-
border-radius:
|
217 |
-
box-shadow: 0 0
|
218 |
-
|
219 |
-
margin-bottom: 20px;
|
220 |
-
animation: fadeInUp 0.5s ease forwards;
|
221 |
display: flex;
|
222 |
flex-direction: column;
|
|
|
223 |
}
|
224 |
.chat-box {
|
225 |
flex: 1;
|
226 |
overflow-y: auto;
|
227 |
-
|
228 |
}
|
229 |
.chat-input {
|
230 |
-
width:
|
231 |
-
border: none;
|
232 |
-
border-top: 1px solid #ddd;
|
233 |
padding: 10px;
|
|
|
|
|
234 |
font-size: 16px;
|
235 |
-
outline: none;
|
236 |
}
|
237 |
.chat-input:focus {
|
238 |
-
|
|
|
|
|
239 |
}
|
240 |
.user-message {
|
|
|
241 |
margin-bottom: 10px;
|
242 |
-
|
243 |
-
|
|
|
244 |
background-color: #007bff;
|
245 |
color: #fff;
|
|
|
|
|
246 |
max-width: 70%;
|
247 |
-
word-wrap: break-word;
|
248 |
-
align-self: flex-end;
|
249 |
}
|
250 |
.bot-message {
|
|
|
251 |
margin-bottom: 10px;
|
252 |
-
|
253 |
-
|
254 |
-
|
|
|
255 |
color: #fff;
|
|
|
|
|
256 |
max-width: 70%;
|
257 |
-
word-wrap: break-word;
|
258 |
}
|
259 |
.toggle-history {
|
260 |
text-align: center;
|
261 |
cursor: pointer;
|
262 |
color: #007bff;
|
263 |
-
margin-
|
264 |
}
|
265 |
.history-container {
|
266 |
display: none;
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
max-height:
|
273 |
overflow-y: auto;
|
274 |
}
|
275 |
-
@keyframes fadeInUp {
|
276 |
-
from {
|
277 |
-
opacity: 0;
|
278 |
-
transform: translateY(20px);
|
279 |
-
}
|
280 |
-
to {
|
281 |
-
opacity: 1;
|
282 |
-
transform: translateY(0);
|
283 |
-
}
|
284 |
-
}
|
285 |
</style>
|
286 |
</head>
|
287 |
<body>
|
288 |
<div class="container">
|
289 |
-
<h1
|
290 |
-
<div class="chat-container"
|
291 |
<div class="chat-box" id="chat-box">
|
292 |
</div>
|
293 |
-
<input type="text" class="chat-input" id="user-input" placeholder="Type your message...">
|
294 |
-
<button onclick="
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
</div>
|
301 |
</div>
|
|
|
|
|
|
|
302 |
<script>
|
303 |
function toggleHistory() {
|
304 |
const historyContainer = document.getElementById('history-container');
|
305 |
-
historyContainer.classList.toggle('
|
306 |
}
|
307 |
|
308 |
function saveMessage(sender, message) {
|
309 |
const historyContent = document.getElementById('history-content');
|
310 |
const messageElement = document.createElement('div');
|
311 |
-
messageElement.className =
|
312 |
-
messageElement.
|
313 |
historyContent.appendChild(messageElement);
|
314 |
}
|
315 |
|
316 |
function appendMessage(sender, message) {
|
317 |
const chatBox = document.getElementById('chat-box');
|
318 |
const messageElement = document.createElement('div');
|
319 |
-
messageElement.className =
|
320 |
-
messageElement.
|
321 |
chatBox.appendChild(messageElement);
|
322 |
chatBox.scrollTop = chatBox.scrollHeight;
|
323 |
}
|
324 |
|
325 |
-
|
326 |
-
|
327 |
-
const userInput = document.getElementById('user-input');
|
328 |
-
|
329 |
-
userInput.addEventListener('keyup', function(event) {
|
330 |
-
if (event.keyCode === 13) {
|
331 |
event.preventDefault();
|
332 |
sendMessage();
|
333 |
}
|
334 |
-
}
|
335 |
|
336 |
function sendMessage() {
|
|
|
337 |
const userMessage = userInput.value.trim();
|
338 |
if (userMessage === '') return;
|
339 |
|
340 |
-
saveMessage('user', userMessage);
|
341 |
appendMessage('user', userMessage);
|
|
|
342 |
userInput.value = '';
|
343 |
|
344 |
fetch(`/autocomplete?q=${encodeURIComponent(userMessage)}`)
|
@@ -351,20 +340,21 @@ async def index():
|
|
351 |
.then(data => {
|
352 |
const botMessages = data.result;
|
353 |
botMessages.forEach(message => {
|
354 |
-
saveMessage('bot', message);
|
355 |
appendMessage('bot', message);
|
|
|
356 |
});
|
357 |
})
|
358 |
.catch(error => {
|
359 |
console.error('Error:', error);
|
360 |
-
saveMessage('bot', "Sorry, I'm not available right now. Please try again later.");
|
361 |
appendMessage('bot', "Sorry, I'm not available right now. Please try again later.");
|
|
|
362 |
});
|
363 |
}
|
364 |
|
365 |
function retryLastMessage() {
|
366 |
-
const lastUserMessage = document.querySelector('.user-message:last-of-type');
|
367 |
if (lastUserMessage) {
|
|
|
368 |
userInput.value = lastUserMessage.innerText;
|
369 |
sendMessage();
|
370 |
}
|
@@ -390,7 +380,7 @@ async def autocomplete(q: str = Query(..., title='query')):
|
|
390 |
return {"result": [response]}
|
391 |
except Exception as e:
|
392 |
logger.error(f"Ignored error in autocomplete: {e}", exc_info=True)
|
393 |
-
return {"result": []}
|
394 |
|
395 |
if __name__ == '__main__':
|
396 |
port = 7860 # Configurar FastAPI para que inicie en el puerto 7860
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
import uvicorn
|
4 |
+
from fastapi import FastAPI, Query, HTTPException
|
5 |
from fastapi.responses import HTMLResponse
|
6 |
from starlette.middleware.cors import CORSMiddleware
|
7 |
from datasets import load_dataset, list_datasets
|
|
|
152 |
unify_datasets()
|
153 |
upload_model_to_hub()
|
154 |
|
155 |
+
# Inicializar FastAPI con lifespan events para evitar DeprecationWarning
|
156 |
app = FastAPI()
|
157 |
|
158 |
# Configuraci贸n de CORS
|
|
|
194 |
<meta charset="UTF-8">
|
195 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
196 |
<title>GPT-2 Chatbot</title>
|
197 |
+
<!-- Bootstrap CSS for a professional interface -->
|
198 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
199 |
<style>
|
|
|
|
|
|
|
|
|
|
|
200 |
body {
|
201 |
+
background-color: #f8f9fa;
|
202 |
font-family: Arial, sans-serif;
|
|
|
|
|
|
|
203 |
}
|
204 |
.container {
|
205 |
max-width: 800px;
|
206 |
+
margin-top: 50px;
|
|
|
207 |
}
|
208 |
.chat-container {
|
209 |
+
background-color: #ffffff;
|
210 |
+
border-radius: 10px;
|
211 |
+
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
212 |
+
padding: 20px;
|
|
|
|
|
213 |
display: flex;
|
214 |
flex-direction: column;
|
215 |
+
height: 600px;
|
216 |
}
|
217 |
.chat-box {
|
218 |
flex: 1;
|
219 |
overflow-y: auto;
|
220 |
+
margin-bottom: 15px;
|
221 |
}
|
222 |
.chat-input {
|
223 |
+
width: 100%;
|
|
|
|
|
224 |
padding: 10px;
|
225 |
+
border: 1px solid #ced4da;
|
226 |
+
border-radius: 5px;
|
227 |
font-size: 16px;
|
|
|
228 |
}
|
229 |
.chat-input:focus {
|
230 |
+
outline: none;
|
231 |
+
border-color: #80bdff;
|
232 |
+
box-shadow: 0 0 5px rgba(0,123,255,0.5);
|
233 |
}
|
234 |
.user-message {
|
235 |
+
text-align: right;
|
236 |
margin-bottom: 10px;
|
237 |
+
}
|
238 |
+
.user-message .message {
|
239 |
+
display: inline-block;
|
240 |
background-color: #007bff;
|
241 |
color: #fff;
|
242 |
+
padding: 10px 15px;
|
243 |
+
border-radius: 15px;
|
244 |
max-width: 70%;
|
|
|
|
|
245 |
}
|
246 |
.bot-message {
|
247 |
+
text-align: left;
|
248 |
margin-bottom: 10px;
|
249 |
+
}
|
250 |
+
.bot-message .message {
|
251 |
+
display: inline-block;
|
252 |
+
background-color: #6c757d;
|
253 |
color: #fff;
|
254 |
+
padding: 10px 15px;
|
255 |
+
border-radius: 15px;
|
256 |
max-width: 70%;
|
|
|
257 |
}
|
258 |
.toggle-history {
|
259 |
text-align: center;
|
260 |
cursor: pointer;
|
261 |
color: #007bff;
|
262 |
+
margin-top: 10px;
|
263 |
}
|
264 |
.history-container {
|
265 |
display: none;
|
266 |
+
background-color: #ffffff;
|
267 |
+
border-radius: 10px;
|
268 |
+
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
269 |
+
padding: 20px;
|
270 |
+
margin-top: 20px;
|
271 |
+
max-height: 300px;
|
272 |
overflow-y: auto;
|
273 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
</style>
|
275 |
</head>
|
276 |
<body>
|
277 |
<div class="container">
|
278 |
+
<h1 class="text-center mb-4">GPT-2 Chatbot</h1>
|
279 |
+
<div class="chat-container">
|
280 |
<div class="chat-box" id="chat-box">
|
281 |
</div>
|
282 |
+
<input type="text" class="chat-input" id="user-input" placeholder="Type your message..." onkeypress="handleKeyPress(event)">
|
283 |
+
<button class="btn btn-primary mt-3" onclick="sendMessage()">Send</button>
|
284 |
+
<div class="toggle-history mt-3" onclick="toggleHistory()">Toggle History</div>
|
285 |
+
<div class="history-container" id="history-container">
|
286 |
+
<h3>Chat History</h3>
|
287 |
+
<div id="history-content"></div>
|
288 |
+
</div>
|
289 |
</div>
|
290 |
</div>
|
291 |
+
|
292 |
+
<!-- Bootstrap JS (optional) -->
|
293 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
|
294 |
<script>
|
295 |
function toggleHistory() {
|
296 |
const historyContainer = document.getElementById('history-container');
|
297 |
+
historyContainer.classList.toggle('d-none');
|
298 |
}
|
299 |
|
300 |
function saveMessage(sender, message) {
|
301 |
const historyContent = document.getElementById('history-content');
|
302 |
const messageElement = document.createElement('div');
|
303 |
+
messageElement.className = sender === 'user' ? 'user-message' : 'bot-message';
|
304 |
+
messageElement.innerHTML = `<div class="message">${message}</div>`;
|
305 |
historyContent.appendChild(messageElement);
|
306 |
}
|
307 |
|
308 |
function appendMessage(sender, message) {
|
309 |
const chatBox = document.getElementById('chat-box');
|
310 |
const messageElement = document.createElement('div');
|
311 |
+
messageElement.className = sender === 'user' ? 'user-message' : 'bot-message';
|
312 |
+
messageElement.innerHTML = `<div class="message">${message}</div>`;
|
313 |
chatBox.appendChild(messageElement);
|
314 |
chatBox.scrollTop = chatBox.scrollHeight;
|
315 |
}
|
316 |
|
317 |
+
function handleKeyPress(event) {
|
318 |
+
if (event.key === 'Enter') {
|
|
|
|
|
|
|
|
|
319 |
event.preventDefault();
|
320 |
sendMessage();
|
321 |
}
|
322 |
+
}
|
323 |
|
324 |
function sendMessage() {
|
325 |
+
const userInput = document.getElementById('user-input');
|
326 |
const userMessage = userInput.value.trim();
|
327 |
if (userMessage === '') return;
|
328 |
|
|
|
329 |
appendMessage('user', userMessage);
|
330 |
+
saveMessage('user', userMessage);
|
331 |
userInput.value = '';
|
332 |
|
333 |
fetch(`/autocomplete?q=${encodeURIComponent(userMessage)}`)
|
|
|
340 |
.then(data => {
|
341 |
const botMessages = data.result;
|
342 |
botMessages.forEach(message => {
|
|
|
343 |
appendMessage('bot', message);
|
344 |
+
saveMessage('bot', message);
|
345 |
});
|
346 |
})
|
347 |
.catch(error => {
|
348 |
console.error('Error:', error);
|
|
|
349 |
appendMessage('bot', "Sorry, I'm not available right now. Please try again later.");
|
350 |
+
saveMessage('bot', "Sorry, I'm not available right now. Please try again later.");
|
351 |
});
|
352 |
}
|
353 |
|
354 |
function retryLastMessage() {
|
355 |
+
const lastUserMessage = document.querySelector('.user-message:last-of-type .message');
|
356 |
if (lastUserMessage) {
|
357 |
+
const userInput = document.getElementById('user-input');
|
358 |
userInput.value = lastUserMessage.innerText;
|
359 |
sendMessage();
|
360 |
}
|
|
|
380 |
return {"result": [response]}
|
381 |
except Exception as e:
|
382 |
logger.error(f"Ignored error in autocomplete: {e}", exc_info=True)
|
383 |
+
return {"result": ["Sorry, I encountered an error processing your request."]}
|
384 |
|
385 |
if __name__ == '__main__':
|
386 |
port = 7860 # Configurar FastAPI para que inicie en el puerto 7860
|