/
NukeFurD
/
Setelment
Обзор
Документация
Войти
/
NukeFurD
/
Setelment
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/services/python_executor.rb
53 строки
2 KB
SoBAKA
Add Python ML service for clustering and sentiment analysis
22 апр 2026, 18:11
22 апр 2026, 18:11
dcb1241
Код
Авторство
О чём код?
class PythonExecutor SCRIPT_DIR = Rails.root.join('ml_service') class << self def run_clustering(texts, n_clusters = 5) script_path = SCRIPT_DIR.join('cluster.py') input_data = { texts: texts, n_clusters: n_clusters }.to_json result = run_script(script_path, input_data) return nil unless result JSON.parse(result) rescue JSON::ParserError => e Rails.logger.error "Failed to parse Python output: #{e.message}" nil end def run_sentiment_analysis(texts, use_transformers = false) script_path = SCRIPT_DIR.join('sentiment.py') input_data = { texts: texts, use_transformers: use_transformers }.to_json result = run_script(script_path, input_data) return nil unless result JSON.parse(result) rescue JSON::ParserError => e Rails.logger.error "Failed to parse Python output: #{e.message}" nil end private def run_script(script_path, input_data) # Для Windows используем полный путь к python из venv python_path = SCRIPT_DIR.join('venv', 'Scripts', 'python.exe') cmd = if File.exist?(python_path) [ python_path.to_s, script_path.to_s ] else [ 'python', script_path.to_s ] end stdout, stderr, status = Open3.capture3(*cmd, stdin_data: input_data) unless status.success? Rails.logger.error "Python script error: #{stderr}" return nil end stdout end end end