import tensorflow
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.text import text_to_word_sequence
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.layers import Input
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Embedding, LSTM, Concatenate
from keras.models import Model


# Séparer les images par langue
def separate_images_by_language(source_folder):
    language_ranges = {
        (1, 1000): "Arabic",
        (1001, 2000): "English",
        (2001, 3000): "French",
        (3001, 4000): "Chinese",
        (4001, 5000): "German",
        (5001, 6000): "Korean",
        (6001, 7000): "Japanese",
        (7001, 8000): "Italian",
        (8001, 9000): "Bangla",
        (9001, 10000): "Hindi"
    }

    image_extensions = (".jpg", ".jpeg", ".png", ".gif", ".txt")

    for filename in os.listdir(source_folder):
        if filename.lower().endswith(image_extensions):
            index = int(filename.split("_")[2].split(".")[0])

            for range_, folder_name in language_ranges.items():
                if range_[0] <= index <= range_[1]:
                    destination_folder = os.path.join(source_folder, folder_name)
                    break

            os.makedirs(destination_folder, exist_ok=True)

            source_path = os.path.join(source_folder, filename)
            destination_path = os.path.join(destination_folder, filename)
            os.rename(source_path, destination_path)

separate_images_by_language("ImagesPart1")
separate_images_by_language("AKRAPO7")

# Définir les paramètres
img_width, img_height = 224, 224
batch_size = 32
epochs = 10

# Charger les données d'images
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
    '/content/ImagesPart1',
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode=None
)

# Charger les données de texte
def load_text_data(directory):
    text_data = []
    for root, dirs, files in os.walk(directory):
        for file in sorted(files):
            if file.endswith(".txt"):
                file_path = os.path.join(root, file)
                with open(file_path, "r") as f:
                    text = f.read()
                    text_data.append(text)
    return text_data

text_data = load_text_data('/content/AKRAPO7')

# Tokeniser et encoder les textes
tokenizer = Tokenizer()
tokenizer.fit_on_texts(text_data)
sequences = tokenizer.texts_to_sequences(text_data)
max_length = max([len(seq) for seq in sequences])
padded_sequences = pad_sequences(sequences, maxlen=max_length, padding='post')


image_input = Input(shape=(img_width, img_height, 3))
text_input = Input(shape=(max_length,))

# Branche du modèle d'image
x = Conv2D(32, (3, 3), activation='relu')(image_input)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = MaxPooling2D((2, 2))(x)
x = Conv2D(64, (3, 3), activation='relu')(x)
x = Flatten()(x)
x = Dense(64, activation='relu')(x)

# Branche du modèle de texte
y = Embedding(len(tokenizer.word_index) + 1, 128)(text_input)
y = LSTM(64)(y)

# Fusionner les branches d'image et de texte
merged = Concatenate()([x, y])
output = Dense(len(tokenizer.word_index) + 1, activation='softmax')(merged)

# Créer le modèle
model = Model(inputs=[image_input, text_input], outputs=output)

# Compiler le modèle
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Entraîner le modèle
model.fit(
    [train_generator, padded_sequences],
    np.expand_dims(padded_sequences, -1),
    epochs=epochs,
    steps_per_epoch=train_generator.samples // batch_size
)

# Sauvegarder le modèle
model.save('model.h5')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-37f17f3d180f> in <cell line: 99>()
     97 
     98 # Entraîner le modèle
---> 99 model.fit(
    100     [train_generator, padded_sequences],
    101     np.expand_dims(padded_sequences, -1),

1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

/usr/local/lib/python3.10/dist-packages/keras/src/engine/data_adapter.py in select_data_adapter(x, y)
   1103     if not adapter_cls:
   1104         # TODO(scottzhu): This should be a less implementation-specific error.
-> 1105         raise ValueError(
   1106             "Failed to find data adapter that can handle input: {}, {}".format(
   1107                 _type_name(x), _type_name(y)

ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'numpy.ndarray'>", "<class 'keras.src.preprocessing.image.DirectoryIterator'>"}), <class 'numpy.ndarray'>