File size: 15,479 Bytes
2914676
 
 
3487d09
2914676
 
 
 
 
 
 
 
 
 
3487d09
2914676
 
3487d09
2914676
 
3487d09
2914676
 
 
 
 
3487d09
2914676
 
 
3487d09
2914676
 
 
 
 
0bd2428
 
3487d09
 
 
2914676
0bd2428
2914676
 
0e16884
ab812c5
 
 
3487d09
ab812c5
2914676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3487d09
 
2914676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3487d09
2914676
 
 
 
 
 
 
 
 
 
 
 
 
 
3487d09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2914676
3487d09
2914676
 
3487d09
2914676
 
3487d09
2914676
 
 
 
 
 
 
ab812c5
 
 
 
3487d09
 
 
 
 
 
 
 
 
 
 
 
 
ab812c5
2914676
 
 
 
 
 
0bd2428
2914676
 
 
 
0bd2428
 
 
 
 
2914676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3487d09
 
 
 
 
 
2914676
 
 
 
 
 
 
 
 
3487d09
2914676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3487d09
2914676
3487d09
 
 
 
 
2914676
 
 
 
 
 
3487d09
2914676
 
87dce9f
2914676
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import os
import sys
import uvicorn
from fastapi import FastAPI, Query, HTTPException, BackgroundTasks
from fastapi.responses import HTMLResponse
from starlette.middleware.cors import CORSMiddleware
from datasets import load_dataset, list_datasets
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from loguru import logger
import concurrent.futures
import psutil
import asyncio
import torch
from tenacity import retry, stop_after_attempt, wait_fixed
from huggingface_hub import HfApi, RepositoryNotFoundError
from dotenv import load_dotenv

# Cargar variables de entorno
load_dotenv()

# Obtener el token de Hugging Face
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
if not HUGGINGFACE_TOKEN:
    logger.error("Hugging Face token not found. Please set the HUGGINGFACE_TOKEN environment variable.")
    sys.exit(1)

# Inicializar diccionarios para datasets y ejemplos
datasets_dict = {}
example_usage_list = []

# Configuraci贸n de cach茅
CACHE_DIR = os.path.expanduser("~/.cache/huggingface")
os.makedirs(CACHE_DIR, exist_ok=True)
os.environ["HF_HOME"] = CACHE_DIR
os.environ["HF_TOKEN"] = HUGGINGFACE_TOKEN

pipeline_instance = None  # Solo un pipeline

# Flag para indicar si la inicializaci贸n est谩 completa
initialization_complete = False

def initialize_model():
    global pipeline_instance
    try:
        logger.info("Initializing the base model and tokenizer.")
        base_model_repo = "meta-llama/Llama-3.2-1B"
        model = AutoModelForCausalLM.from_pretrained(
            base_model_repo,
            cache_dir=CACHE_DIR,
            ignore_mismatched_sizes=True  # Ignorar discrepancias de tama帽o
        )
        tokenizer = AutoTokenizer.from_pretrained(base_model_repo, cache_dir=CACHE_DIR)
        if tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token
        pipeline_instance = pipeline(
            "text-generation",
            model=model,
            tokenizer=tokenizer,
            device=0 if torch.cuda.is_available() else -1
        )
        logger.info("Model and tokenizer initialized successfully.")
    except Exception as e:
        logger.error(f"Error initializing model and tokenizer: {e}", exc_info=True)
        sys.exit(1)

@retry(stop=stop_after_attempt(3), wait=wait_fixed(5))
def download_dataset(dataset_name):
    try:
        logger.info(f"Starting download for dataset: {dataset_name}")
        # Eliminado 'trust_remote_code=True' para evitar el error con ParquetConfig
        datasets_dict[dataset_name] = load_dataset(dataset_name, cache_dir=CACHE_DIR)
        create_example_usage(dataset_name)
    except Exception as e:
        logger.error(f"Error loading dataset {dataset_name}: {e}", exc_info=True)
        raise

def upload_model_to_hub():
    try:
        api = HfApi()
        model_repo = "Yhhxhfh/test"
        try:
            api.repo_info(repo_id=model_repo)
            logger.info(f"Model repository {model_repo} already exists.")
        except RepositoryNotFoundError:
            api.create_repo(repo_id=model_repo, private=False, token=HUGGINGFACE_TOKEN)
            logger.info(f"Created model repository {model_repo}.")
        logger.info(f"Pushing the model and tokenizer to {model_repo}.")
        pipeline_instance.model.push_to_hub(model_repo, use_auth_token=HUGGINGFACE_TOKEN)
        pipeline_instance.tokenizer.push_to_hub(model_repo, use_auth_token=HUGGINGFACE_TOKEN)
        logger.info(f"Successfully pushed the model and tokenizer to {model_repo}.")
    except Exception as e:
        logger.error(f"Error uploading model to Hugging Face Hub: {e}", exc_info=True)

def create_example_usage(dataset_name):
    try:
        logger.info(f"Creating example usage for dataset {dataset_name}")
        example_prompts = [
            "Translate the following catering menu from English to French:",
            "Generate a catering menu for a wedding with vegetarian options:",
            "Convert the following catering menu to a gluten-free version:",
            "Provide a detailed catering menu for a corporate event including desserts:",
            "Generate a children's birthday party catering menu with allergen-free items:"
        ]
        examples = []
        for prompt in example_prompts:
            generated_text = pipeline_instance(prompt, max_length=50, num_return_sequences=1)[0]['generated_text']
            examples.append({"prompt": prompt, "response": generated_text})
        example_usage_list.append({"dataset_name": dataset_name, "examples": examples})
        logger.info(f"Example usage created for dataset {dataset_name}")
    except Exception as e:
        logger.error(f"Error creating example usage for dataset {dataset_name}: {e}", exc_info=True)

def unify_datasets():
    try:
        logger.info("Starting to unify datasets")
        unified_dataset = None
        for dataset in datasets_dict.values():
            if unified_dataset is None:
                unified_dataset = dataset
            else:
                unified_dataset = unified_dataset.concatenate(dataset)
        datasets_dict['unified'] = unified_dataset
        logger.info("Datasets successfully unified.")
    except Exception as e:
        logger.error(f"Error unifying datasets: {e}", exc_info=True)

# Configuraci贸n de concurrencia
cpu_count = psutil.cpu_count(logical=False) or 1
memory_available_mb = psutil.virtual_memory().available / (1024 * 1024)
memory_per_download_mb = 100
memory_available = int(memory_available_mb / memory_per_download_mb)
gpu_count = torch.cuda.device_count()
max_concurrent_downloads = min(cpu_count, memory_available, gpu_count * 2 if gpu_count else cpu_count)
max_concurrent_downloads = max(1, max_concurrent_downloads)
max_concurrent_downloads = min(10, max_concurrent_downloads)

logger.info(f"Using up to {max_concurrent_downloads} concurrent workers for downloading datasets.")

executor = concurrent.futures.ThreadPoolExecutor(max_workers=max_concurrent_downloads)

async def download_and_process_datasets():
    global initialization_complete
    try:
        dataset_names = list_datasets()
        logger.info(f"Found {len(dataset_names)} datasets to download.")
        loop = asyncio.get_event_loop()
        tasks = []
        for dataset_name in dataset_names:
            task = loop.run_in_executor(executor, download_dataset, dataset_name)
            tasks.append(task)
        await asyncio.gather(*tasks)
        unify_datasets()
        upload_model_to_hub()
        initialization_complete = True
        logger.info("All initialization tasks completed successfully.")
    except Exception as e:
        logger.error(f"Error during dataset processing: {e}", exc_info=True)

# Inicializar FastAPI
app = FastAPI()

# Configuraci贸n de CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Cambia esto seg煤n tus necesidades
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

message_history = []

@app.on_event("startup")
async def startup_event():
    logger.info("Application startup initiated.")
    loop = asyncio.get_event_loop()
    # Crear una tarea en segundo plano para inicializar el modelo y descargar datasets
    loop.create_task(run_initialization(loop))
    logger.info("Background initialization tasks started.")

async def run_initialization(loop):
    global initialization_complete
    try:
        # Inicializar el modelo en un hilo separado
        await loop.run_in_executor(None, initialize_model)
        # Descargar y procesar datasets
        await download_and_process_datasets()
    except Exception as e:
        logger.error(f"Error during startup tasks: {e}", exc_info=True)

@app.get('/')
async def index():
    html_code = """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <!-- Existing head content -->
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>ChatGPT Chatbot</title>
        <style>
            /* Existing styles */
            /* Add styles for the model selector */
            .model-selector {
                margin-bottom: 10px;
            }
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 0;
                background-color: #f4f4f4;
            }
            .container {
                max-width: 800px;
                margin: auto;
                padding: 20px;
            }
            .chat-container {
                background-color: #fff;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                overflow: hidden;
                margin-bottom: 20px;
                animation: fadeInUp 0.5s ease forwards;
                display: flex;
                flex-direction: column;
            }
            .chat-box {
                flex: 1;
                overflow-y: auto;
                padding: 10px;
            }
            .chat-input {
                width: calc(100% - 20px);
                border: none;
                border-top: 1px solid #ddd;
                padding: 10px;
                font-size: 16px;
                outline: none;
            }
            .chat-input:focus {
                border-top: 1px solid #007bff;
            }
            .user-message {
                margin-bottom: 10px;
                padding: 8px 12px;
                border-radius: 8px;
                background-color: #007bff;
                color: #fff;
                max-width: 70%;
                word-wrap: break-word;
                align-self: flex-end;
            }
            .bot-message {
                margin-bottom: 10px;
                padding: 8px 12px;
                border-radius: 8px;
                background-color: #4CAF50;
                color: #fff;
                max-width: 70%;
                word-wrap: break-word;
            }
            .toggle-history {
                text-align: center;
                cursor: pointer;
                color: #007bff;
                margin-bottom: 10px;
            }
            .history-container {
                display: none;
            }
            .history-container.show {
                display: block;
            }
            .history-container .history-content {
                max-height: 200px;
                overflow-y: auto;
            }
            @keyframes fadeInUp {
                from {
                    opacity: 0;
                    transform: translateY(20px);
                }
                to {
                    opacity: 1;
                    transform: translateY(0);
                }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1 style="text-align: center;">ChatGPT Chatbot</h1>
            <div class="chat-container" id="chat-container">
                <div class="chat-box" id="chat-box">
                </div>
                <input type="text" class="chat-input" id="user-input" placeholder="Type your message...">
                <button onclick="retryLastMessage()">Retry Last Message</button>
            </div>
            <div class="toggle-history" onclick="toggleHistory()">Toggle History</div>
            <div class="history-container" id="history-container">
                <h2>Chat History</h2>
                <div class="history-content" id="history-content"></div>
            </div>
        </div>
        <script>
            function toggleHistory() {
                const historyContainer = document.getElementById('history-container');
                historyContainer.classList.toggle('show');
            }

            function saveMessage(sender, message) {
                const historyContent = document.getElementById('history-content');
                const messageElement = document.createElement('div');
                messageElement.className = `${sender}-message`;
                messageElement.innerText = message;
                historyContent.appendChild(messageElement);
            }

            function appendMessage(sender, message) {
                const chatBox = document.getElementById('chat-box');
                const messageElement = document.createElement('div');
                messageElement.className = `${sender}-message`;
                messageElement.innerText = message;
                chatBox.appendChild(messageElement);
                chatBox.scrollTop = chatBox.scrollHeight;
            }

            const chatContainer = document.getElementById('chat-container');
            const chatBox = document.getElementById('chat-box');
            const userInput = document.getElementById('user-input');

            userInput.addEventListener('keyup', function(event) {
                if (event.keyCode === 13) {
                    event.preventDefault();
                    sendMessage();
                }
            });

            function sendMessage() {
                const userMessage = userInput.value.trim();
                if (userMessage === '') return;

                saveMessage('user', userMessage);
                appendMessage('user', userMessage);
                userInput.value = '';

                fetch(`/autocomplete?q=${encodeURIComponent(userMessage)}`)
                .then(response => {
                    if (response.status === 503) {
                        return response.json().then(data => { throw new Error(data.detail); });
                    }
                    return response.json();
                })
                .then(data => {
                    const botMessages = data.result;
                    botMessages.forEach(message => {
                        saveMessage('bot', message);
                        appendMessage('bot', message);
                    });
                })
                .catch(error => {
                    console.error('Error:', error);
                    appendMessage('bot', error.message || 'An error occurred. Please try again later.');
                });
            }

            function retryLastMessage() {
                const lastUserMessage = document.querySelector('.user-message:last-of-type');
                if (lastUserMessage) {
                    userInput.value = lastUserMessage.innerText;
                    sendMessage();
                }
            }
        </script>
    </body>
    </html>
    """
    return HTMLResponse(content=html_code, status_code=200)

@app.get('/autocomplete')
async def autocomplete(q: str = Query(..., title='query')):
    global message_history, pipeline_instance, initialization_complete
    message_history.append(('user', q))
    
    if not initialization_complete:
        logger.warning("Model is not initialized yet.")
        raise HTTPException(status_code=503, detail="Model is not initialized yet. Please try again later.")
    
    try:
        response = pipeline_instance(q, max_length=50, num_return_sequences=1)[0]['generated_text']
        logger.debug(f"Successfully autocomplete, q:{q}, res:{response}")
        return {"result": [response]}
    except Exception as e:
        logger.error(f"Ignored error in autocomplete: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail="An error occurred while processing your request.")

if __name__ == '__main__':
    port = int(os.getenv("PORT", 7860))
    uvicorn.run(app=app, host='0.0.0.0', port=port)