akra35567 commited on
Commit
d9a5c79
·
1 Parent(s): 60951f1

fix: startup - 1 worker, timeout 90s, debug logs in _setup_providers and _setup_hf_inference

Browse files
Files changed (3) hide show
  1. main.py +43 -25
  2. modules/api.py +14 -0
  3. scripts/init_pg.sh +1 -2
main.py CHANGED
@@ -199,10 +199,12 @@ async def get_rotation_log(request: Request):
199
  # === INTEGRAÇÃO DA API ===
200
  akira_api = None
201
  api_disponivel = False
 
202
 
203
  try:
204
  from modules.api import get_akira_api, get_router
205
  import modules.config as config
 
206
 
207
  API_AVAILABLE = getattr(config, 'API_AVAILABLE', {})
208
 
@@ -213,33 +215,56 @@ try:
213
  config.validate_config()
214
  logger.info("Config validada")
215
 
216
- akira_api = get_akira_api()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
 
218
  app.include_router(get_router(), prefix="/api")
219
- logger.success("API V21 integrada -> /api/akira")
 
220
 
221
  apis_ok = []
222
- if config.MISTRAL_API_KEY: apis_ok.append("Mistral")
223
- if config.GEMINI_API_KEY: apis_ok.append("Gemini")
224
- if config.GROQ_API_KEY: apis_ok.append("Groq")
225
- if config.COHERE_API_KEY: apis_ok.append("Cohere")
226
- if config.TOGETHER_API_KEY: apis_ok.append("Together")
227
 
228
  if apis_ok:
229
  logger.info(f"APIs: {', '.join(apis_ok)}")
230
 
231
- api_disponivel = True
232
- else:
233
- logger.warning("API nao disponivel")
 
 
 
 
 
 
 
234
 
235
- except ImportError as e:
236
- logger.critical(f"ERRO DE IMPORTACAO: {e}")
237
- import traceback
238
- logger.critical(traceback.format_exc())
239
- except Exception as e:
240
- logger.critical(f"FALHA: {e}")
241
- import traceback
242
- logger.critical(traceback.format_exc())
243
 
244
  # === BACKUP AUTOMÁTICO ===
245
  def periodic_hf_sync_checkpoint():
@@ -257,13 +282,6 @@ def periodic_hf_sync_checkpoint():
257
  threading.Thread(target=_run_checkpoint, daemon=True).start()
258
  logger.info("Checkpoint HF Sync agendado p/ cada 2 horas.")
259
 
260
- if api_disponivel:
261
- periodic_hf_sync_checkpoint()
262
-
263
- @app.on_event("startup")
264
- async def startup_event():
265
- logger.success("🚀 AKIRA V21 — Servidor FastAPI PRONTO e escutando!")
266
-
267
  if __name__ == "__main__":
268
  import uvicorn
269
  host = os.getenv("API_HOST", "0.0.0.0")
 
199
  # === INTEGRAÇÃO DA API ===
200
  akira_api = None
201
  api_disponivel = False
202
+ _config_module = None
203
 
204
  try:
205
  from modules.api import get_akira_api, get_router
206
  import modules.config as config
207
+ _config_module = config
208
 
209
  API_AVAILABLE = getattr(config, 'API_AVAILABLE', {})
210
 
 
215
  config.validate_config()
216
  logger.info("Config validada")
217
 
218
+ except ImportError as e:
219
+ logger.critical(f"ERRO DE IMPORTACAO: {e}")
220
+ import traceback
221
+ logger.critical(traceback.format_exc())
222
+ except Exception as e:
223
+ logger.critical(f"FALHA: {e}")
224
+ import traceback
225
+ logger.critical(traceback.format_exc())
226
+
227
+ import concurrent.futures
228
+
229
+ def _init_akira_safe():
230
+ global akira_api, api_disponivel
231
+ logger.info("🔧 [STARTUP] Inicializando AkiraAPI com timeout 90s...")
232
+ pool = concurrent.futures.ThreadPoolExecutor(max_workers=1)
233
+ try:
234
+ future = pool.submit(get_akira_api)
235
+ akira_api = future.result(timeout=90)
236
+ logger.success("✅ [STARTUP] AkiraAPI criada")
237
 
238
  app.include_router(get_router(), prefix="/api")
239
+ api_disponivel = True
240
+ logger.success("✅ [STARTUP] API V21 integrada -> /api/akira")
241
 
242
  apis_ok = []
243
+ if _config_module and _config_module.MISTRAL_API_KEY: apis_ok.append("Mistral")
244
+ if _config_module and _config_module.GEMINI_API_KEY: apis_ok.append("Gemini")
245
+ if _config_module and _config_module.GROQ_API_KEY: apis_ok.append("Groq")
246
+ if _config_module and _config_module.COHERE_API_KEY: apis_ok.append("Cohere")
247
+ if _config_module and _config_module.TOGETHER_API_KEY: apis_ok.append("Together")
248
 
249
  if apis_ok:
250
  logger.info(f"APIs: {', '.join(apis_ok)}")
251
 
252
+ periodic_hf_sync_checkpoint()
253
+ except concurrent.futures.TimeoutError:
254
+ logger.critical("⏰ [STARTUP] AkiraAPI init TIMEOUT (90s) — API não disponível")
255
+ akira_api = None
256
+ except Exception as e:
257
+ logger.critical(f"FALHA AO INICIALIZAR AkiraAPI: {e}")
258
+ import traceback
259
+ logger.critical(traceback.format_exc())
260
+ finally:
261
+ pool.shutdown(wait=False, cancel_futures=True)
262
 
263
+ _init_akira_safe()
264
+
265
+ @app.on_event("startup")
266
+ async def startup_event():
267
+ logger.success("🚀 AKIRA V21 — Servidor FastAPI PRONTO e escutando!")
 
 
 
268
 
269
  # === BACKUP AUTOMÁTICO ===
270
  def periodic_hf_sync_checkpoint():
 
282
  threading.Thread(target=_run_checkpoint, daemon=True).start()
283
  logger.info("Checkpoint HF Sync agendado p/ cada 2 horas.")
284
 
 
 
 
 
 
 
 
285
  if __name__ == "__main__":
286
  import uvicorn
287
  host = os.getenv("API_HOST", "0.0.0.0")
modules/api.py CHANGED
@@ -563,12 +563,19 @@ class LLMManager:
563
  self._setup_torouter()
564
  self._setup_cerebras() # 🎯 Novo: Setup Cerebras
565
  self._setup_hf_inference() # 🤗 Novo: Setup HF Inference
 
566
  self._setup_mistral()
 
567
  self._setup_gemini()
 
568
  self._setup_groq()
 
569
  self._setup_grok()
 
570
  self._setup_cohere()
 
571
  self._setup_together()
 
572
 
573
  def _setup_openrouter(self):
574
  api_key = getattr(self.config, 'OPENROUTER_API_KEY', '')
@@ -631,18 +638,25 @@ class LLMManager:
631
  if os.getenv(rotation.accounts[acc])
632
  ]
633
 
 
 
634
  if configured_accounts:
635
  # HF Inference usa InferenceClient via huggingface_hub
636
  try:
637
  from huggingface_hub import InferenceClient
 
638
  current_token = rotation.get_current_api_token()
639
  current_name = rotation.get_current_account_name()
640
 
 
 
641
  if current_token:
 
642
  self.hf_inference_client = InferenceClient(
643
  token=current_token,
644
  timeout=30.0,
645
  )
 
646
  logger.info(
647
  f"✅ HF Inference OK (rotação multi-conta ativa, atual: {current_name}, "
648
  f"{len(configured_accounts)} contas disponíveis)"
 
563
  self._setup_torouter()
564
  self._setup_cerebras() # 🎯 Novo: Setup Cerebras
565
  self._setup_hf_inference() # 🤗 Novo: Setup HF Inference
566
+ logger.info("🔧 [INIT] Providers intermediários...")
567
  self._setup_mistral()
568
+ logger.info("🔧 [INIT] Mistral OK")
569
  self._setup_gemini()
570
+ logger.info("🔧 [INIT] Gemini OK")
571
  self._setup_groq()
572
+ logger.info("🔧 [INIT] Groq OK")
573
  self._setup_grok()
574
+ logger.info("🔧 [INIT] Grok OK")
575
  self._setup_cohere()
576
+ logger.info("🔧 [INIT] Cohere OK")
577
  self._setup_together()
578
+ logger.info("🔧 [INIT] Together OK")
579
 
580
  def _setup_openrouter(self):
581
  api_key = getattr(self.config, 'OPENROUTER_API_KEY', '')
 
638
  if os.getenv(rotation.accounts[acc])
639
  ]
640
 
641
+ logger.info(f"🔧 [INIT] HF configured_accounts: {configured_accounts}")
642
+
643
  if configured_accounts:
644
  # HF Inference usa InferenceClient via huggingface_hub
645
  try:
646
  from huggingface_hub import InferenceClient
647
+ logger.info("🔧 [INIT] InferenceClient import OK")
648
  current_token = rotation.get_current_api_token()
649
  current_name = rotation.get_current_account_name()
650
 
651
+ logger.info(f"🔧 [INIT] HF token={'YES' if current_token else 'NO'}, name={current_name}")
652
+
653
  if current_token:
654
+ logger.info("🔧 [INIT] Criando InferenceClient...")
655
  self.hf_inference_client = InferenceClient(
656
  token=current_token,
657
  timeout=30.0,
658
  )
659
+ logger.info("🔧 [INIT] InferenceClient criado")
660
  logger.info(
661
  f"✅ HF Inference OK (rotação multi-conta ativa, atual: {current_name}, "
662
  f"{len(configured_accounts)} contas disponíveis)"
scripts/init_pg.sh CHANGED
@@ -27,11 +27,10 @@ if [ -f "$BACKUP_FILE" ]; then
27
  PGPASSWORD=akira psql -h localhost -U akira -d akira -f "$BACKUP_FILE" 2>/dev/null || true
28
  fi
29
 
30
- echo "🚀 [INIT] Iniciando Akira FastAPI com 2 workers..."
31
 
32
  exec uvicorn main:app \
33
  --host 0.0.0.0 \
34
  --port 7860 \
35
- --workers 2 \
36
  --timeout-keep-alive 30 \
37
  --limit-concurrency 100
 
27
  PGPASSWORD=akira psql -h localhost -U akira -d akira -f "$BACKUP_FILE" 2>/dev/null || true
28
  fi
29
 
30
+ echo "🚀 [INIT] Iniciando Akira FastAPI com 1 worker (async)..."
31
 
32
  exec uvicorn main:app \
33
  --host 0.0.0.0 \
34
  --port 7860 \
 
35
  --timeout-keep-alive 30 \
36
  --limit-concurrency 100