diff --git a/apiApp/config.py b/apiApp/config.py index 9b249ab..2767c1b 100644 --- a/apiApp/config.py +++ b/apiApp/config.py @@ -33,4 +33,9 @@ CALLS_WEB_CLIENT_URL = os.getenv( "http://calls_web_client:8000" ) WEBHOOK_ENDPOINT = f"{CALLS_WEB_CLIENT_URL}/api/transcription/webhook" -WEBHOOK_API_KEY = os.getenv("WEBHOOK_API_KEY", "webhook_secret_key") \ No newline at end of file +WEBHOOK_API_KEY = os.getenv("WEBHOOK_API_KEY", "webhook_secret_key") + +# Auto-restore recognition on startup +ENABLE_AUTO_RESTORE = os.getenv("ENABLE_AUTO_RESTORE", "true").lower() == "true" +AUTO_RESTORE_LIMIT = int(os.getenv("AUTO_RESTORE_LIMIT", "100")) # Максимум файлов для восстановления +AUTO_RESTORE_DELAY = int(os.getenv("AUTO_RESTORE_DELAY", "5")) # Задержка перед запуском (секунды) \ No newline at end of file diff --git a/apiApp/routers/audio_management_router.py b/apiApp/routers/audio_management_router.py index 92627b4..5528757 100644 --- a/apiApp/routers/audio_management_router.py +++ b/apiApp/routers/audio_management_router.py @@ -330,3 +330,64 @@ async def get_audio_stats(db: Session = Depends(get_db)): status_code=500, detail=str(e) ) + + +def auto_restore_on_startup(db: Session, limit: int = 100): + """ + Автоматическое восстановление распознавания при старте FileAudioAPI + + Проверяет, есть ли файлы без AiConclusion, и запускает их распознавание + + Args: + db: Сессия БД + limit: Максимум файлов для восстановления + """ + try: + from sqlalchemy import or_ + + # Проверяем, есть ли файлы без AiConclusion + pending_audio = db.query(Audio).filter( + or_( + Audio.AiConclusion == None, + Audio.AiConclusion == '' + ) + ).limit(limit).all() + + if not pending_audio: + logger.info("ℹ️ Auto-restore: нет файлов для распознавания") + return + + logger.info(f"🔄 Auto-restore: найдено {len(pending_audio)} файлов без AiConclusion") + + # Запускаем распознавание + started_count = 0 + for audio in pending_audio: + file_path = os.path.join(AUDIOFILES_PATH, audio.filename) + + if not os.path.exists(file_path): + logger.warning(f"⚠️ Файл не найден: {audio.filename}") + continue + + # Отправляем в GigaAM API + from apiApp.config import GIGAAM_API_URL + api_url = f"{GIGAAM_API_URL}/api/call/process" + + payload = {"filename": audio.filename} + + try: + import requests + response = requests.post(api_url, json=payload, timeout=5) + + if response.status_code in [200, 202]: + logger.info(f"✅ Запущено распознавание: {audio.filename}") + started_count += 1 + else: + logger.warning(f"⚠️ Ошибка запуска {audio.filename}: {response.status_code}") + + except Exception as e: + logger.error(f"❌ Ошибка при запуске {audio.filename}: {e}") + + logger.info(f"🎉 Auto-restore завершено: запущено {started_count} файлов") + + except Exception as e: + logger.error(f"❌ Ошибка при auto-restore: {e}") diff --git a/main.py b/main.py index 84424f9..dcac96c 100644 --- a/main.py +++ b/main.py @@ -53,7 +53,7 @@ async def startup_event(): """Создание таблиц при запуске приложения""" try: Base.metadata.create_all(bind=engine) - logger.info("Database tables created successfully") + logger.info("✅ Database tables created successfully") except Exception as e: logger.error(f"Error creating database tables: {e}")