Ура, мини победа, я могу сохранять вероятнотси. Пабеда
This commit is contained in:
parent
96f8e782cc
commit
7c3020625c
33
model.py
Normal file
33
model.py
Normal file
@ -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 # Возвращаем строковое представление предсказания
|
||||||
|
|
@ -2,12 +2,19 @@ from sqlalchemy import select
|
|||||||
|
|
||||||
from database import new_session, QuestionOrm
|
from database import new_session, QuestionOrm
|
||||||
from schemas import SQuestionAdd, SQuestion
|
from schemas import SQuestionAdd, SQuestion
|
||||||
|
from model import predict_answer
|
||||||
|
|
||||||
|
|
||||||
class QuestionRepository:
|
class QuestionRepository:
|
||||||
@classmethod
|
@classmethod
|
||||||
async def add_one(cls, data: SQuestionAdd) -> int:
|
async def add_one(cls, data: SQuestionAdd) -> int:
|
||||||
async with new_session() as session:
|
async with new_session() as session:
|
||||||
question_dict = data.model_dump()
|
question_dict = data.model_dump()
|
||||||
|
|
||||||
|
# Предсказание ответа с помощью модели
|
||||||
|
answer = predict_answer(question_dict["question"])
|
||||||
|
|
||||||
|
question_dict["answer"] = answer
|
||||||
question = QuestionOrm(**question_dict)
|
question = QuestionOrm(**question_dict)
|
||||||
session.add(question)
|
session.add(question)
|
||||||
await session.flush()
|
await session.flush()
|
||||||
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Loading…
Reference in New Issue
Block a user