Hello. im beginner in analyze tonality. I use this to analyze text for Ru. Is this correct to analyze Russian text and retrain model? Thanks
Загружаем конфигурацию модели для анализа тональности из DeepPavlov
sentiment_config_path = configs.classifiers.sentiment_twitter
sentiment_model = build_model(sentiment_config_path, download=True)
MODEL_FILENAME = ‘model_checkpoint’
def generate():
pass
def load_model():
logger = logging.getLogger()
logger.info(“Entering function: load_model”)
try:
logger.info("Attempting to load pretrained fastText model with DeepPavlov")
# Загружаем предобученную модель fastText с помощью DeepPavlov
model = build_model(configs.classifiers.sentiment_twitter, download=True)
logger.info("Model loaded successfully")
print("Модель загружена.")
logger.info("Exiting function: load_model with model loaded")
return model
except Exception as e:
logger.error("Error loading model: %s", str(e))
print(f"Ошибка при загрузке модели: {e}")
logger.info("Exiting function: load_model with error")
return None # Return None to indicate failure
def save_model(model):
logger = logging.getLogger()
logger.info(“Entering function: save_model”)
try:
logger.info("Attempting to save the trained model")
# Сохраняем обученную модель
model.save()
save_path = os.path.abspath(MODEL_FILENAME)
logger.info("Model saved successfully at: %s", save_path)
print(f"Модель сохранена. Каталог: {save_path}")
logger.info("Exiting function: save_model with model saved")
except Exception as e:
logger.error("Error saving model: %s", str(e))
print(f"Ошибка при сохранении модели: {e}")
logger.info("Exiting function: save_model with error")
# restart
main_loop()
Глобальные переменные для накопления сообщений и меток
accumulated_messages =
accumulated_labels =
def retrain_model(messages, labels):
global accumulated_messages, accumulated_labels
logger = logging.getLogger()
logger.info("Entering function: retrain_model with messages=%s, labels=%s", messages, labels)
logger.info("Accumulating messages and labels")
accumulated_messages.extend(messages)
accumulated_labels.extend(labels)
logger.info("Current accumulated_messages count: %d, accumulated_labels count: %d",
len(accumulated_messages), len(accumulated_labels))
if len(accumulated_messages) >= 1:
logger.info("Starting model retraining process with %d samples", len(accumulated_messages))
try:
logger.info("Creating dataset for retraining")
# Создание датасета для задачи
data = list(zip(accumulated_messages, accumulated_labels))
logger.info("Dataset created: %s", data)
# Переобучение модели с использованием датасета
logger.info("Retraining model with sentiment_twitter config")
train_model(configs.classifiers.sentiment_twitter, data, False, False)
logger.info("Model retraining completed successfully")
# Очистка накопленных данных
logger.info("Clearing accumulated messages and labels")
accumulated_messages = []
accumulated_labels = []
logger.info("Accumulated data cleared")
logger.info("Exiting function: retrain_model with successful retraining")
except ValueError as ve:
logger.error("ValueError in retraining: %s", str(ve))
logger.info("Exiting function: retrain_model with ValueError")
except Exception as e:
logger.error("Unexpected error in retraining: %s", str(e))
logger.info("Exiting function: retrain_model with error")
else:
logger.info("Not enough samples for retraining (need >= 1), current count: %d", len(accumulated_messages))
logger.info("Exiting function: retrain_model without retraining")
def predict_sentiment(texts):
logger = logging.getLogger()
logger.info(“Entering function: predict_sentiment”)
# Логируем входные данные
logger.info("Received input texts: type=%s, value=%s", type(texts), texts)
# Загрузите модель перед использованием
logger.info("Attempting to load sentiment model")
try:
model = load_model()
logger.info("Model loaded successfully")
except Exception as e:
logger.error("Failed to load model: %s", str(e))
raise # Re-raise to halt execution if model loading fails
# Проверяем, является ли 'texts' строкой, и преобразуем в список
if isinstance(texts, str):
logger.info("Input is a single string, converting to list")
texts = [texts]
else:
logger.info("Input is already a list-like object, proceeding as-is")
logger.info("Processed texts for prediction: %s", texts)
# Получаем предсказания
logger.info("Generating predictions for %d text(s)", len(texts))
try:
predictions = model(texts)
logger.info("Predictions generated successfully: %s", predictions)
except Exception as e:
logger.error("Error during prediction: %s", str(e))
raise # Re-raise to propagate the error
logger.info("Exiting function: predict_sentiment with predictions: %s", predictions)
return predictions