Fix thread safety, implement prompt, fix resource leaks, add history rotation

- Add threading.Lock around asr_pipeline calls (thread-safe inference on shared GPU)
- Implement prompt parameter: propagate through process_file → transcribe → generate_kwargs
- Fix prompt_ids device mismatch (.to(self.device) for CUDA)
- Replace mkdtemp() with create_temp_file() in sources.py (no more leaked /tmp dirs)
- Add max_history_days (default 30) with automatic cleanup of old history directories
- Add intentional comments for SSRF and /config decisions (internal service)
- Lower normalize_audio log level from INFO to DEBUG

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Serge Zaigraeff
2026-03-31 00:43:06 +03:00
co-authored by Claude Sonnet 4.6
parent 2138651474
commit 1e778cae86
7 changed files with 171 additions and 87 deletions
+11 -23
View File
@@ -38,7 +38,7 @@ class AudioProcessor:
def convert_to_wav(self, input_path: str) -> str:
"""
Конвертация входного аудиофайла в WAV формат с частотой дискретизации 16 кГц.
Конвертация входного аудиофайла в WAV формат с частотой дискретизации 16 кГц (моно).
Args:
input_path: Путь к исходному аудиофайлу.
@@ -51,18 +51,6 @@ class AudioProcessor:
"""
audio_rate = self.config["audio_rate"]
# Проверка расширения файла
if input_path.lower().endswith('.wav'):
# Проверяем, нужно ли преобразовывать WAV-файл (например, если частота не 16 кГц)
try:
info = subprocess.check_output(['soxi', input_path]).decode()
if f'{audio_rate} Hz' in info:
logger.info(f"Файл {input_path} уже в формате WAV с частотой {audio_rate} Гц")
return input_path
except subprocess.CalledProcessError:
logger.warning(f"Не удалось получить информацию о WAV-файле {input_path}")
# Продолжаем конвертацию, чтобы быть уверенными в формате
# Создаем временный файл для WAV
output_path = create_temp_file(".wav")
@@ -77,14 +65,14 @@ class AudioProcessor:
output_path
]
logger.debug(f"Конвертация в WAV: {' '.join(cmd)}")
logger.debug("Конвертация в WAV: %s", " ".join(cmd))
try:
subprocess.run(cmd, check=True, capture_output=True)
logger.info(f"Файл конвертирован в WAV: {output_path}")
logger.info("Файл конвертирован в WAV: %s", output_path)
return output_path
except subprocess.CalledProcessError as e:
logger.error(f"Ошибка при конвертации в WAV: {e.stderr.decode()}")
logger.error("Ошибка при конвертации в WAV: %s", e.stderr.decode())
raise
def normalize_audio(self, input_path: str) -> str:
@@ -112,14 +100,14 @@ class AudioProcessor:
"compand"
] + self.compand_params.split()
logger.info(f"Нормализация аудио: {' '.join(cmd)}")
logger.debug("Нормализация аудио: %s", " ".join(cmd))
try:
subprocess.run(cmd, check=True, capture_output=True)
logger.info(f"Аудио нормализовано: {output_path}")
logger.info("Аудио нормализовано: %s", output_path)
return output_path
except subprocess.CalledProcessError as e:
logger.error(f"Ошибка при нормализации аудио: {e.stderr.decode()}")
logger.error("Ошибка при нормализации аудио: %s", e.stderr.decode())
raise
def add_silence(self, input_path: str) -> str:
@@ -146,14 +134,14 @@ class AudioProcessor:
"pad", "2.0", "1.0" # Добавление тишины в начале и в конце (секунды)
]
logger.info(f"Добавление тишины: {' '.join(cmd)}")
logger.info("Добавление тишины: %s", " ".join(cmd))
try:
subprocess.run(cmd, check=True, capture_output=True)
logger.info(f"Тишина добавлена: {output_path}")
logger.info("Тишина добавлена: %s", output_path)
return output_path
except subprocess.CalledProcessError as e:
logger.error(f"Ошибка при добавлении тишины: {e.stderr.decode()}")
logger.error("Ошибка при добавлении тишины: %s", e.stderr.decode())
raise
def process_audio(self, input_path: str) -> Tuple[str, list]:
@@ -188,6 +176,6 @@ class AudioProcessor:
return silence_path, temp_files
except Exception as e:
logger.error(f"Ошибка при обработке аудио {input_path}: {e}")
logger.error("Ошибка при обработке аудио %s: %s", input_path, e)
cleanup_temp_files(temp_files)
raise
+22 -16
View File
@@ -4,8 +4,6 @@
"""
import os
import uuid
import tempfile
import base64
from urllib.parse import urlparse
import magic
@@ -13,6 +11,8 @@ import requests
from typing import Tuple, Optional
import logging
from ..infrastructure.storage import create_temp_file
logger = logging.getLogger('app.audio_sources')
@@ -23,12 +23,6 @@ def _check_size(size_bytes: int, max_size_mb: int) -> Optional[str]:
return None
def _make_temp_path(suffix: str = ".wav") -> str:
"""Создаёт путь для временного файла."""
temp_dir = tempfile.mkdtemp()
return os.path.join(temp_dir, f"{uuid.uuid4()}{suffix}")
def get_uploaded_file(request_files, max_file_size_mb: int = 100) -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""
Получает аудиофайл из загруженных файлов Flask.
@@ -54,7 +48,7 @@ def get_uploaded_file(request_files, max_file_size_mb: int = 100) -> Tuple[Optio
return None, None, error
# Сохраняем во временный файл
temp_path = _make_temp_path()
temp_path = create_temp_file()
file.save(temp_path)
return temp_path, file.filename, None
@@ -72,6 +66,8 @@ def get_url_file(url: str, max_file_size_mb: int = 100) -> Tuple[Optional[str],
if parsed.scheme not in ('http', 'https'):
return None, None, f"Unsupported URL scheme: {parsed.scheme}. Only http/https allowed"
# SSRF-защита (валидация private IP) не добавлена сознательно:
# сервис работает во внутренней сети и недоступен извне
response = requests.get(url, stream=True, timeout=30)
response.raise_for_status()
@@ -92,15 +88,23 @@ def get_url_file(url: str, max_file_size_mb: int = 100) -> Tuple[Optional[str],
if url_path:
original_name = os.path.basename(url_path)
temp_path = _make_temp_path()
temp_path = create_temp_file()
max_bytes = max_file_size_mb * 1024 * 1024
total_size = 0
with open(temp_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
total_size += len(chunk)
if total_size > max_bytes:
f.close()
os.remove(temp_path)
return None, None, f"File exceeds maximum size of {max_file_size_mb}MB"
f.write(chunk)
return temp_path, original_name or os.path.basename(temp_path), None
except Exception as e:
logger.error(f"Ошибка при получении файла по URL {url}: {e}")
logger.error("Ошибка при получении файла по URL %s: %s", url, e)
return None, None, f"Error retrieving file from URL: {str(e)}"
@@ -112,12 +116,14 @@ def get_base64_file(base64_data: str, max_file_size_mb: int = 100) -> Tuple[Opti
Кортеж (путь к temp-файлу, имя файла, сообщение об ошибке).
"""
try:
audio_data = base64.b64decode(base64_data)
error = _check_size(len(audio_data), max_file_size_mb)
# Оценка размера до декодирования: base64 кодирует 3 байта в 4 символа
estimated_size = len(base64_data) * 3 // 4
error = _check_size(estimated_size, max_file_size_mb)
if error:
return None, None, error
audio_data = base64.b64decode(base64_data)
# Определяем формат по содержимому
mime_to_ext = {
"audio/mpeg": ".mp3",
@@ -131,12 +137,12 @@ def get_base64_file(base64_data: str, max_file_size_mb: int = 100) -> Tuple[Opti
detected_mime = magic.from_buffer(audio_data[:1024], mime=True)
suffix = mime_to_ext.get(detected_mime, ".wav")
temp_path = _make_temp_path(suffix)
temp_path = create_temp_file(suffix)
with open(temp_path, 'wb') as f:
f.write(audio_data)
return temp_path, os.path.basename(temp_path), None
except Exception as e:
logger.error(f"Ошибка при декодировании base64 данных: {e}")
logger.error("Ошибка при декодировании base64 данных: %s", e)
return None, None, f"Error decoding base64 data: {str(e)}"