|
import os |
|
import requests |
|
from tqdm import tqdm |
|
from datasets import load_dataset |
|
import numpy as np |
|
import tensorflow as tf |
|
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input |
|
from tensorflow.keras.preprocessing import image |
|
from tensorflow.keras.layers import Dense, Input, Concatenate, Embedding, Flatten |
|
from tensorflow.keras.models import Model |
|
from tensorflow.keras.preprocessing.text import Tokenizer |
|
from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
from sklearn.preprocessing import LabelEncoder |
|
import joblib |
|
from PIL import UnidentifiedImageError, Image |
|
import gradio as gr |
|
|
|
|
|
MAX_TEXT_LENGTH = 200 |
|
EMBEDDING_DIM = 100 |
|
IMAGE_SIZE = 224 |
|
BATCH_SIZE = 32 |
|
|
|
def load_and_preprocess_data(subset_size=2700): |
|
|
|
dataset = load_dataset("thefcraft/civitai-stable-diffusion-337k") |
|
dataset_subset = dataset['train'].shuffle(seed=42).select(range(subset_size)) |
|
|
|
|
|
dataset_subset = dataset_subset.filter(lambda x: not x['nsfw']) |
|
|
|
return dataset_subset |
|
|
|
def process_text_data(dataset_subset): |
|
|
|
text_data = [f"{sample['prompt']} {sample['negativePrompt']}" for sample in dataset_subset] |
|
|
|
|
|
tokenizer = Tokenizer() |
|
tokenizer.fit_on_texts(text_data) |
|
sequences = tokenizer.texts_to_sequences(text_data) |
|
text_data_padded = pad_sequences(sequences, maxlen=MAX_TEXT_LENGTH) |
|
|
|
return text_data_padded, tokenizer |
|
|
|
def process_image_data(dataset_subset): |
|
image_dir = 'civitai_images' |
|
os.makedirs(image_dir, exist_ok=True) |
|
|
|
image_data = [] |
|
valid_indices = [] |
|
|
|
for idx, sample in enumerate(tqdm(dataset_subset)): |
|
img_url = sample['url'] |
|
img_path = os.path.join(image_dir, os.path.basename(img_url)) |
|
|
|
try: |
|
|
|
response = requests.get(img_url) |
|
response.raise_for_status() |
|
|
|
if 'image' not in response.headers['Content-Type']: |
|
continue |
|
|
|
with open(img_path, 'wb') as f: |
|
f.write(response.content) |
|
|
|
|
|
img = image.load_img(img_path, target_size=(IMAGE_SIZE, IMAGE_SIZE)) |
|
img_array = image.img_to_array(img) |
|
img_array = preprocess_input(img_array) |
|
|
|
image_data.append(img_array) |
|
valid_indices.append(idx) |
|
|
|
except Exception as e: |
|
print(f"Error processing image {img_url}: {e}") |
|
continue |
|
|
|
return np.array(image_data), valid_indices |
|
|
|
def create_multimodal_model(num_words, num_classes): |
|
|
|
image_input = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) |
|
cnn_base = ResNet50(weights='imagenet', include_top=False, pooling='avg') |
|
cnn_features = cnn_base(image_input) |
|
|
|
|
|
text_input = Input(shape=(MAX_TEXT_LENGTH,)) |
|
embedding_layer = Embedding(num_words, EMBEDDING_DIM)(text_input) |
|
flatten_text = Flatten()(embedding_layer) |
|
text_features = Dense(256, activation='relu')(flatten_text) |
|
|
|
|
|
combined = Concatenate()([cnn_features, text_features]) |
|
|
|
|
|
x = Dense(512, activation='relu')(combined) |
|
x = Dense(256, activation='relu')(x) |
|
output = Dense(num_classes, activation='softmax')(x) |
|
|
|
model = Model(inputs=[image_input, text_input], outputs=output) |
|
return model |
|
|
|
def train_model(): |
|
|
|
dataset_subset = load_and_preprocess_data() |
|
|
|
|
|
text_data_padded, tokenizer = process_text_data(dataset_subset) |
|
|
|
|
|
image_data, valid_indices = process_image_data(dataset_subset) |
|
|
|
|
|
text_data_padded = text_data_padded[valid_indices] |
|
model_names = [dataset_subset[i]['Model'] for i in valid_indices] |
|
|
|
|
|
label_encoder = LabelEncoder() |
|
encoded_labels = label_encoder.fit_transform(model_names) |
|
|
|
|
|
model = create_multimodal_model( |
|
num_words=len(tokenizer.word_index) + 1, |
|
num_classes=len(label_encoder.classes_) |
|
) |
|
|
|
model.compile( |
|
optimizer='adam', |
|
loss='sparse_categorical_crossentropy', |
|
metrics=['accuracy'] |
|
) |
|
|
|
|
|
history = model.fit( |
|
[image_data, text_data_padded], |
|
encoded_labels, |
|
batch_size=BATCH_SIZE, |
|
epochs=10, |
|
validation_split=0.2 |
|
) |
|
|
|
|
|
model.save('multimodal_model') |
|
joblib.dump(tokenizer, 'tokenizer.pkl') |
|
joblib.dump(label_encoder, 'label_encoder.pkl') |
|
|
|
return model, tokenizer, label_encoder |
|
|
|
def get_recommendations(image_input, text_input, model, tokenizer, label_encoder, top_k=5): |
|
|
|
img_array = image.img_to_array(image_input) |
|
img_array = tf.image.resize(img_array, (IMAGE_SIZE, IMAGE_SIZE)) |
|
img_array = preprocess_input(img_array) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
text_sequence = tokenizer.texts_to_sequences([text_input]) |
|
text_padded = pad_sequences(text_sequence, maxlen=MAX_TEXT_LENGTH) |
|
|
|
|
|
predictions = model.predict([img_array, text_padded]) |
|
top_indices = np.argsort(predictions[0])[-top_k:][::-1] |
|
|
|
|
|
recommendations = [ |
|
(label_encoder.inverse_transform([idx])[0], predictions[0][idx]) |
|
for idx in top_indices |
|
] |
|
|
|
return recommendations |
|
|
|
|
|
def create_gradio_interface(): |
|
|
|
model = tf.keras.models.load_model('multimodal_model') |
|
tokenizer = joblib.load('tokenizer.pkl') |
|
label_encoder = joblib.load('label_encoder.pkl') |
|
|
|
def predict(img, text): |
|
recommendations = get_recommendations(img, text, model, tokenizer, label_encoder) |
|
return "\n".join([f"Model: {name}, Confidence: {conf:.2f}" for name, conf in recommendations]) |
|
|
|
interface = gr.Interface( |
|
fn=predict, |
|
inputs=[ |
|
gr.Image(type="pil", label="Upload Image"), |
|
gr.Textbox(label="Enter Prompt") |
|
], |
|
outputs=gr.Textbox(label="Recommended Models"), |
|
title="Multimodal Model Recommendation System", |
|
description="Upload an image and enter a prompt to get model recommendations" |
|
) |
|
|
|
return interface |
|
|
|
if __name__ == "__main__": |
|
|
|
if not os.path.exists('multimodal_model'): |
|
model, tokenizer, label_encoder = train_model() |
|
|
|
|
|
interface = create_gradio_interface() |
|
interface.launch() |