import json
import tensorflow as tf
from tensorflow.keras.layers import Input, Embedding, Dense, Dropout, Attention, GlobalAveragePooling1D
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

# Load conversation data from a JSON file
def load_data_from_json(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data

# Replace 'your_conversation.json' with the actual JSON file containing your data
conversations_data = load_data_from_json('chatbotTurkishDataset.json')

# Extract input and target texts from the loaded data
input_texts = [text[0] for text in conversations_data]
target_texts = [text[1] for text in conversations_data]

# Preprocess the data
tokenizer = Tokenizer(filters='')
tokenizer.fit_on_texts(input_texts + target_texts)

input_sequences = tokenizer.texts_to_sequences(input_texts)
target_sequences = tokenizer.texts_to_sequences(target_texts)

max_length = 100  # Adjust the max_length parameter based on your data
input_sequences = pad_sequences(input_sequences, maxlen=max_length)
target_sequences = pad_sequences(target_sequences, maxlen=max_length)

# Define the transformer model
def transformer_model(vocab_size, d_model, n_heads, ff_dim, dropout=0.1, max_length=100):
    inputs = Input(shape=(max_length,))
    embedding_layer = Embedding(input_dim=vocab_size, output_dim=d_model)(inputs)
    transformer_block = TransformerBlock(embedding_dim=d_model, num_heads=n_heads, ff_dim=ff_dim)(embedding_layer)
    output_layer = Dense(vocab_size, activation='softmax')(transformer_block)
    
    model = Model(inputs=inputs, outputs=output_layer)
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    
    return model

# Define the TransformerBlock layer
class TransformerBlock(tf.keras.layers.Layer):
    def __init__(self, embedding_dim, num_heads, ff_dim, rate=0.1):
        super(TransformerBlock, self).__init__()
        self.att = Attention(use_scale=True, dropout=rate)
        self.ffn = tf.keras.Sequential([
            Dense(ff_dim, activation='relu'),
            Dense(embedding_dim),
        ])
        self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
        self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
        self.dropout1 = Dropout(rate)
        self.dropout2 = Dropout(rate)

    def call(self, inputs):
        attn_output = self.att([inputs, inputs])
        attn_output = self.dropout1(attn_output)
        out1 = self.layernorm1(inputs + attn_output)

        ffn_output = self.ffn(out1)
        ffn_output = self.dropout2(ffn_output)
        return self.layernorm2(out1 + ffn_output)

# Set hyperparameters
vocab_size = len(tokenizer.word_index) + 1
d_model = 128
n_heads = 8
ff_dim = 256

# Create and train the model
model = transformer_model(vocab_size, d_model, n_heads, ff_dim)
model.fit(input_sequences, target_sequences, epochs=20, batch_size=2)

# User interaction loop
while True:
    user_input = input("You: ")
    
    if user_input.lower() == 'save':
        model.save('turkish_chatbot_model.h5')
        print("Model saved successfully!")
        break
    
    # Preprocess user input
    user_input_sequence = tokenizer.texts_to_sequences([user_input])
    user_input_sequence = pad_sequences(user_input_sequence, maxlen=max_length)
    
    # Generate model response
    model_response_sequence = model.predict(user_input_sequence).argmax(axis=-1)
    
    # Decode the response sequence
    model_response = tokenizer.sequences_to_texts(model_response_sequence)[0]
    
    print("Chatbot:", model_response)
PS C:\Users\mehme> & C:/Users/mehme/AppData/Local/Microsoft/WindowsApps/python3.11.exe c:/Users/mehme/chat.py
2024-01-05 12:05:35.613041: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
WARNING:tensorflow:From C:\Users\mehme\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

WARNING:tensorflow:From C:\Users\mehme\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\backend.py:1398: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.

2024-01-05 12:05:45.911234: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:From C:\Users\mehme\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\optimizers\__init__.py:309: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

Epoch 1/20
WARNING:tensorflow:From C:\Users\mehme\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\utils\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead.

WARNING:tensorflow:From C:\Users\mehme\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\engine\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.
fix the errors