import os import logging import asyncio import uvicorn import torch from transformers import AutoModelForCausalLM, AutoTokenizer from fastapi import FastAPI, Query, HTTPException from fastapi.responses import HTMLResponse # Configuración de logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) # Inicializar la aplicación FastAPI app = FastAPI() # Diccionario para almacenar los modelos data_and_models_dict = {} # Lista para almacenar el historial de mensajes message_history = [] # Función para cargar modelos async def load_models(): gpt_models = ["gpt2-medium", "gpt2-large", "gpt2"] for model_name in gpt_models: try: model = AutoModelForCausalLM.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) logger.info(f"Successfully loaded {model_name} model") return model, tokenizer except Exception as e: logger.error(f"Failed to load GPT-2 model: {e}") raise HTTPException(status_code=500, detail="Failed to load any models") # Función para descargar modelos async def download_models(): model, tokenizer = await load_models() data_and_models_dict['model'] = (model, tokenizer) @app.get('/') async def main(): html_code = """