diff --git a/model.py b/model.py new file mode 100644 index 0000000..42de820 --- /dev/null +++ b/model.py @@ -0,0 +1,33 @@ +import pickle +import numpy as np +import tensorflow as tf +from keras.src.legacy.preprocessing.text import Tokenizer +from keras.src.utils import pad_sequences + +# Загрузка модели +model = tf.keras.models.load_model('best_model_lstm_negative.keras') + +# Загрузка токенизатора +with open('tokenizer_lstm_lstm_negative.pickle', 'rb') as handle: + tokenizer = pickle.load(handle) + +def preprocess_text(text: str): + # Токенизация текста + sequences = tokenizer.texts_to_sequences([text]) + # Преобразование последовательностей в фиксированной длины + padded_sequences = pad_sequences(sequences, maxlen=90) # 90 - длина последовательности, используемая при обучении + return padded_sequences + +def predict_answer(question: str) -> str: + # Предобработка вопроса + print("Вопрос:", question) + input_data = preprocess_text(question) + print("Предобработанные данные:", input_data) + # Предсказание + prediction = model.predict(input_data) + print("Предсказание:", prediction) + # Преобразование предсказания в строку для сохранения + prediction_str = np.array2string(prediction[0], separator=',') + print("Строковое представление предсказания:", prediction_str) + return prediction_str # Возвращаем строковое представление предсказания + diff --git a/repository.py b/repository.py index 447e4b2..dd8281b 100644 --- a/repository.py +++ b/repository.py @@ -2,12 +2,19 @@ from sqlalchemy import select from database import new_session, QuestionOrm from schemas import SQuestionAdd, SQuestion +from model import predict_answer + class QuestionRepository: @classmethod async def add_one(cls, data: SQuestionAdd) -> int: async with new_session() as session: question_dict = data.model_dump() + + # Предсказание ответа с помощью модели + answer = predict_answer(question_dict["question"]) + + question_dict["answer"] = answer question = QuestionOrm(**question_dict) session.add(question) await session.flush() diff --git a/requirements.txt b/requirements.txt index 712cf65..3b537bb 100644 Binary files a/requirements.txt and b/requirements.txt differ