import sys

import io

sys.stdout = io.TextIOWrapper(sys.stdout, encoding='utf-8')

sys.stderr = io.TextIOWrapper(sys.stderr, encoding='utf-8')

import pandas as pd

import tensorflow as tf

from tensorflow import keras


# 엑셀 파일에서 데이터 읽어오기

data = pd.read_excel(r'C:\Users\syh\Desktop\DEJAVU v1.0\database.xlsx')


texts = data['language'].tolist()

questions = data['language'].tolist()

answers = data['s'].tolist()


# 데이터 전처리

tokenizer = keras.preprocessing.text.Tokenizer()

tokenizer.fit_on_texts(texts + questions)

vocab_size = len(tokenizer.word_index) + 1


max_text_len = 100  # text_length

max_question_length = 50  # length_length


text_sequences = tokenizer.texts_to_sequences(texts)

question_sequences = tokenizer.texts_to_sequences(questions)


x_text = keras.preprocessing.sequence.pad_sequences(text_sequences, maxlen=max_text_len)

x_query = keras.preprocessing.sequence.pad_sequences(question_sequences, maxlen=max_question_length)


answer_sequences = tokenizer.texts_to_sequences(answers)

y = keras.preprocessing.sequence.pad_sequences(answer_sequences, maxlen=max_text_len)


# 학습 데이터와 검증 데이터 분할

split_ratio = 0.8

split_index = int(len(texts) * split_ratio)


x_text_train = x_text[:split_index]

x_text_val = x_text[split_index:]

x_query_train = x_query[:split_index]

x_query_val = x_query[split_index:]

y_train = y[:split_index]

y_val = y[split_index:]


# 모델 구조 및 하이퍼파라미터

text_input = keras.layers.Input(shape=(max_text_len,))

question_input = keras.layers.Input(shape=(max_question_length,))


text_embedding = keras.layers.Embedding(vocab_size, 128)(text_input)

question_embedding = keras.layers.Embedding(vocab_size, 128)(question_input)


text_rnn = keras.layers.LSTM(128)(text_embedding)

question_rnn = keras.layers.LSTM(128)(question_embedding)


concatenated = keras.layers.concatenate([text_rnn, question_rnn])

output = keras.layers.Dense(vocab_size, activation='softmax')(concatenated)


model = keras.models.Model(inputs=[text_input, question_input], outputs=output)


model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')


# 모델 학습

model.fit([x_text_train, x_query_train], y_train, validation_data=([x_text_val, x_query_val], y_val), epochs=10, batch_size=32)


# 새로운 지문과 질문에 대한 답변 예측

new_text = ['Special Content']  # 지문 입력

new_question = ['Question Question']  # 질문 입력


new_text_sequence = tokenizer.texts_to_sequences(new_text)

new_query_sequence = tokenizer.texts_to_sequences(new_question)


x_new_text = keras.preprocessing.sequence.pad_sequences(new_text_sequence, maxlen=max_text_len)

x_new_query = keras.preprocessing.sequence.pad_sequences(new_query_sequence, maxlen=max_question_length)


predictions = model.predict([x_new_text, x_new_query])

predicted_answer_sequence = predictions[0]

predicted_answer = tokenizer.sequences_to_texts([predicted_answer_sequence])[0]


print("예측된 답변:", predicted_answer)



 File "C:\Users\syh\Desktop\DEJAVU v1.0\DEJAVU\DEJAVU.py", line 11

SyntaxError: Non-UTF-8 code starting with '\xbf' in file C:\Users\syh\Desktop\DEJAVU v1.0\DEJAVU\DEJAVU.py on line 11, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details


계속 이지랄하면서 오류 뜨는데 어디서 고쳐야 할지 감이 안온다 씨발

쌉고수 프붕이 있으면 좀 도와줘...