Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,8 @@ import psutil
|
|
12 |
import asyncio
|
13 |
import torch
|
14 |
from tenacity import retry, stop_after_attempt, wait_fixed
|
15 |
-
from huggingface_hub import HfApi
|
|
|
16 |
from dotenv import load_dotenv
|
17 |
|
18 |
# Cargar variables de entorno
|
@@ -40,10 +41,10 @@ pipeline_instance = None # Solo un pipeline
|
|
40 |
initialization_complete = False
|
41 |
|
42 |
def initialize_model():
|
43 |
-
global pipeline_instance
|
44 |
try:
|
45 |
-
logger.info("Initializing the
|
46 |
-
base_model_repo = "
|
47 |
model = AutoModelForCausalLM.from_pretrained(
|
48 |
base_model_repo,
|
49 |
cache_dir=CACHE_DIR,
|
@@ -58,7 +59,8 @@ def initialize_model():
|
|
58 |
tokenizer=tokenizer,
|
59 |
device=0 if torch.cuda.is_available() else -1
|
60 |
)
|
61 |
-
logger.info("
|
|
|
62 |
except Exception as e:
|
63 |
logger.error(f"Error initializing model and tokenizer: {e}", exc_info=True)
|
64 |
sys.exit(1)
|
@@ -77,7 +79,7 @@ def download_dataset(dataset_name):
|
|
77 |
def upload_model_to_hub():
|
78 |
try:
|
79 |
api = HfApi()
|
80 |
-
model_repo = "Yhhxhfh/
|
81 |
try:
|
82 |
api.repo_info(repo_id=model_repo)
|
83 |
logger.info(f"Model repository {model_repo} already exists.")
|
@@ -95,11 +97,11 @@ def create_example_usage(dataset_name):
|
|
95 |
try:
|
96 |
logger.info(f"Creating example usage for dataset {dataset_name}")
|
97 |
example_prompts = [
|
98 |
-
"
|
99 |
-
"
|
100 |
-
"
|
101 |
-
"
|
102 |
-
"
|
103 |
]
|
104 |
examples = []
|
105 |
for prompt in example_prompts:
|
@@ -139,22 +141,16 @@ logger.info(f"Using up to {max_concurrent_downloads} concurrent workers for down
|
|
139 |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=max_concurrent_downloads)
|
140 |
|
141 |
async def download_and_process_datasets():
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
unify_datasets()
|
153 |
-
upload_model_to_hub()
|
154 |
-
initialization_complete = True
|
155 |
-
logger.info("All initialization tasks completed successfully.")
|
156 |
-
except Exception as e:
|
157 |
-
logger.error(f"Error during dataset processing: {e}", exc_info=True)
|
158 |
|
159 |
# Inicializar FastAPI
|
160 |
app = FastAPI()
|
@@ -162,7 +158,7 @@ app = FastAPI()
|
|
162 |
# Configuraci贸n de CORS
|
163 |
app.add_middleware(
|
164 |
CORSMiddleware,
|
165 |
-
allow_origins=["*"], #
|
166 |
allow_credentials=True,
|
167 |
allow_methods=["*"],
|
168 |
allow_headers=["*"]
|
@@ -174,17 +170,17 @@ message_history = []
|
|
174 |
async def startup_event():
|
175 |
logger.info("Application startup initiated.")
|
176 |
loop = asyncio.get_event_loop()
|
177 |
-
#
|
178 |
-
|
179 |
-
logger.info("
|
180 |
|
181 |
async def run_initialization(loop):
|
182 |
-
global initialization_complete
|
183 |
try:
|
184 |
# Inicializar el modelo en un hilo separado
|
185 |
await loop.run_in_executor(None, initialize_model)
|
186 |
# Descargar y procesar datasets
|
187 |
await download_and_process_datasets()
|
|
|
188 |
except Exception as e:
|
189 |
logger.error(f"Error during startup tasks: {e}", exc_info=True)
|
190 |
|
@@ -197,7 +193,7 @@ async def index():
|
|
197 |
<!-- Existing head content -->
|
198 |
<meta charset="UTF-8">
|
199 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
200 |
-
<title>
|
201 |
<style>
|
202 |
/* Existing styles */
|
203 |
/* Add styles for the model selector */
|
@@ -290,7 +286,7 @@ async def index():
|
|
290 |
</head>
|
291 |
<body>
|
292 |
<div class="container">
|
293 |
-
<h1 style="text-align: center;">
|
294 |
<div class="chat-container" id="chat-container">
|
295 |
<div class="chat-box" id="chat-box">
|
296 |
</div>
|
@@ -361,7 +357,8 @@ async def index():
|
|
361 |
})
|
362 |
.catch(error => {
|
363 |
console.error('Error:', error);
|
364 |
-
|
|
|
365 |
});
|
366 |
}
|
367 |
|
@@ -393,8 +390,8 @@ async def autocomplete(q: str = Query(..., title='query')):
|
|
393 |
return {"result": [response]}
|
394 |
except Exception as e:
|
395 |
logger.error(f"Ignored error in autocomplete: {e}", exc_info=True)
|
396 |
-
|
397 |
|
398 |
if __name__ == '__main__':
|
399 |
-
port =
|
400 |
uvicorn.run(app=app, host='0.0.0.0', port=port)
|
|
|
12 |
import asyncio
|
13 |
import torch
|
14 |
from tenacity import retry, stop_after_attempt, wait_fixed
|
15 |
+
from huggingface_hub import HfApi
|
16 |
+
from huggingface_hub.utils import RepositoryNotFoundError
|
17 |
from dotenv import load_dotenv
|
18 |
|
19 |
# Cargar variables de entorno
|
|
|
41 |
initialization_complete = False
|
42 |
|
43 |
def initialize_model():
|
44 |
+
global pipeline_instance, initialization_complete
|
45 |
try:
|
46 |
+
logger.info("Initializing the GPT-2 model and tokenizer.")
|
47 |
+
base_model_repo = "gpt2" # Puedes elegir variantes como "gpt2-medium", "gpt2-large", etc.
|
48 |
model = AutoModelForCausalLM.from_pretrained(
|
49 |
base_model_repo,
|
50 |
cache_dir=CACHE_DIR,
|
|
|
59 |
tokenizer=tokenizer,
|
60 |
device=0 if torch.cuda.is_available() else -1
|
61 |
)
|
62 |
+
logger.info("GPT-2 model and tokenizer initialized successfully.")
|
63 |
+
initialization_complete = True
|
64 |
except Exception as e:
|
65 |
logger.error(f"Error initializing model and tokenizer: {e}", exc_info=True)
|
66 |
sys.exit(1)
|
|
|
79 |
def upload_model_to_hub():
|
80 |
try:
|
81 |
api = HfApi()
|
82 |
+
model_repo = "Yhhxhfh/Hhggg" # Reemplaza con tu repositorio en Hugging Face Hub
|
83 |
try:
|
84 |
api.repo_info(repo_id=model_repo)
|
85 |
logger.info(f"Model repository {model_repo} already exists.")
|
|
|
97 |
try:
|
98 |
logger.info(f"Creating example usage for dataset {dataset_name}")
|
99 |
example_prompts = [
|
100 |
+
"Once upon a time,",
|
101 |
+
"In a world where AI rules,",
|
102 |
+
"The future of technology is",
|
103 |
+
"Explain the concept of",
|
104 |
+
"Describe a scenario where"
|
105 |
]
|
106 |
examples = []
|
107 |
for prompt in example_prompts:
|
|
|
141 |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=max_concurrent_downloads)
|
142 |
|
143 |
async def download_and_process_datasets():
|
144 |
+
dataset_names = list_datasets()
|
145 |
+
logger.info(f"Found {len(dataset_names)} datasets to download.")
|
146 |
+
loop = asyncio.get_event_loop()
|
147 |
+
tasks = []
|
148 |
+
for dataset_name in dataset_names:
|
149 |
+
task = loop.run_in_executor(executor, download_dataset, dataset_name)
|
150 |
+
tasks.append(task)
|
151 |
+
await asyncio.gather(*tasks)
|
152 |
+
unify_datasets()
|
153 |
+
upload_model_to_hub()
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
# Inicializar FastAPI
|
156 |
app = FastAPI()
|
|
|
158 |
# Configuraci贸n de CORS
|
159 |
app.add_middleware(
|
160 |
CORSMiddleware,
|
161 |
+
allow_origins=["*"], # Para mayor seguridad, especifica los or铆genes permitidos
|
162 |
allow_credentials=True,
|
163 |
allow_methods=["*"],
|
164 |
allow_headers=["*"]
|
|
|
170 |
async def startup_event():
|
171 |
logger.info("Application startup initiated.")
|
172 |
loop = asyncio.get_event_loop()
|
173 |
+
# Iniciar tareas en segundo plano sin bloquear el servidor
|
174 |
+
asyncio.create_task(run_initialization(loop))
|
175 |
+
logger.info("Startup tasks initiated.")
|
176 |
|
177 |
async def run_initialization(loop):
|
|
|
178 |
try:
|
179 |
# Inicializar el modelo en un hilo separado
|
180 |
await loop.run_in_executor(None, initialize_model)
|
181 |
# Descargar y procesar datasets
|
182 |
await download_and_process_datasets()
|
183 |
+
logger.info("All startup tasks completed successfully.")
|
184 |
except Exception as e:
|
185 |
logger.error(f"Error during startup tasks: {e}", exc_info=True)
|
186 |
|
|
|
193 |
<!-- Existing head content -->
|
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 */
|
|
|
286 |
</head>
|
287 |
<body>
|
288 |
<div class="container">
|
289 |
+
<h1 style="text-align: center;">GPT-2 Chatbot</h1>
|
290 |
<div class="chat-container" id="chat-container">
|
291 |
<div class="chat-box" id="chat-box">
|
292 |
</div>
|
|
|
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 |
|
|
|
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
|
397 |
uvicorn.run(app=app, host='0.0.0.0', port=port)
|