{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"Try this Free online SD 1.5 generator with the results: https://perchance.org/fusion-ai-image-generator\n",
"\n",
" This Notebook is a Stable-diffusion tool which allows you to find similiar prompts to an existing prompt. It uses the Nearest Neighbor decoder method listed here:https://arxiv.org/pdf/2303.03032"
],
"metadata": {
"id": "cRV2YWomjMBU"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UEYEdzjgOEOE"
},
"outputs": [],
"source": [
"# @title β³οΈ Load/initialize values\n",
"#Imports\n",
"#!pip install safetensors\n",
"from safetensors.torch import load_file\n",
"import json , os , shelve , torch\n",
"import pandas as pd\n",
"#----#\n",
"\n",
"def my_mkdirs(folder):\n",
" if os.path.exists(folder)==False:\n",
" os.makedirs(folder)\n",
"\n",
"def fix_bad_symbols(txt):\n",
" result = txt\n",
" for symbol in ['}', '{' , ')', '(', '[' , ']' , ':' , '=' , '^']:\n",
" result = result.replace(symbol,'\\\\' + symbol)\n",
" for symbol in ['^']:\n",
" result = result.replace(symbol,'')\n",
" #------#\n",
" result = result.replace('\\\\|','|').replace(' |',' |')\n",
" return result;\n",
"\n",
"\n",
"def getPrompts(_path, separator):\n",
"\n",
" path = _path + '/text'\n",
" path_enc = _path + '/text_encodings'\n",
" #-----#\n",
" index = 0\n",
" file_index = 0\n",
" prompts = {}\n",
" text_encodings = {}\n",
" _text_encodings = {}\n",
" #-----#\n",
" for filename in os.listdir(f'{path}'):\n",
"\n",
" print(f'reading {filename}....')\n",
" _index = 0\n",
" %cd {path}\n",
" with open(f'{filename}', 'r') as f:\n",
" data = json.load(f)\n",
" #------#\n",
" _df = pd.DataFrame({'count': data})['count']\n",
" _prompts = {\n",
" key : value for key, value in _df.items()\n",
" }\n",
" for key in _prompts:\n",
" _index = int(key)\n",
" value = _prompts[key]\n",
"\n",
" #Read the 'header' file in the JSON\n",
" if _index <= 0 :\n",
" _NUM_ITEMS = int(value)\n",
" prompts[f'{index}'] = _prompts[f'{_index}'] + separator\n",
" index = index + 1\n",
" continue\n",
" if _index <= 1 :\n",
" _file_name = f'{value}'\n",
" %cd {path_enc}\n",
" _text_encodings = load_file(f'{_file_name}.safetensors')\n",
" #Store text_encodings for the header items\n",
" text_encodings[f'{index-1}'] = _text_encodings[f'{_index-1}']\n",
" text_encodings[f'{index}'] = _text_encodings[f'{_index}']\n",
" #------#\n",
" prompts[f'{index}'] = _prompts[f'{_index}'] + separator\n",
" index = index + 1\n",
" continue\n",
" #------#\n",
" #Read the text_encodings + prompts\n",
" text_encodings[f'{index}'] = _text_encodings[f'{_index}']\n",
" prompts[f'{index}'] = _prompts[f'{_index}'] + separator\n",
" index = index + 1\n",
" continue\n",
" #-------#\n",
" #--------#\n",
" #_text_encodings.close() #close the text_encodings file\n",
" file_index = file_index + 1\n",
" #----------#\n",
" NUM_ITEMS = index -1\n",
" return prompts , text_encodings , NUM_ITEMS\n",
"#--------#\n",
"\n",
"def append_from_url(dictA, tensA , nA , url , separator):\n",
" dictB , tensB, nB = getPrompts(url, separator)\n",
" dictAB = dictA\n",
" tensAB = tensA\n",
" nAB = nA\n",
" for key in dictB:\n",
" nAB = nAB + 1\n",
" dictAB[f'{nA + int(key)}'] = dictB[key]\n",
" tensAB[f'{nA + int(key)}'] = tensB[key]\n",
" #-----#\n",
" return dictAB, tensAB , nAB-1\n",
"#-------#\n",
"\n",
"home_directory = '/content/'\n",
"using_Kaggle = os.environ.get('KAGGLE_URL_BASE','')\n",
"if using_Kaggle : home_directory = '/kaggle/working/'\n",
"%cd {home_directory}\n",
"\n",
"#πΈπΉ\n",
"# Load the data if not already loaded\n",
"try:\n",
" loaded\n",
"except:\n",
" %cd {home_directory}\n",
" !git clone https://huggingface.co/datasets/codeShare/text-to-image-prompts\n",
" loaded = True\n",
"#--------#\n",
"\n",
"#default NEG values\n",
"try: name_NEG\n",
"except: name_NEG = ''\n",
"try: image_NEG\n",
"except: image_NEG = ''\n",
"try: strength_image_NEG\n",
"except: strength_image_NEG = 1\n",
"try: strength_NEG\n",
"except: strength_NEG = 1\n",
"try: NUM_VOCAB_ITEMS\n",
"except: NUM_VOCAB_ITEMS = 0\n",
"try: using_NEG\n",
"except: using_NEG = False\n",
"try: using_image_NEG\n",
"except: using_image_NEG = False\n",
"#------#\n",
"\n",
"def getJSON(path , filename):\n",
" %cd {path}\n",
" with open(f'{filename}', 'r') as f:\n",
" data = json.load(f)\n",
" #------#\n",
" print(f'reading {filename}....')\n",
" _df = pd.DataFrame({'count': data})['count']\n",
" _prompts = {\n",
" key : value for key, value in _df.items()\n",
" }\n",
" return _prompts\n",
"\n",
"#----#\n",
"\n",
"def getPromptsAndLinks(_path):\n",
" path = _path + '/text'\n",
" path_enc = _path + '/text_encodings'\n",
" #-----#\n",
" path_images = _path + '/images'\n",
" path_enc_images = _path + '/image_encodings'\n",
" #----#\n",
" _file_name = ''\n",
" _file_name_image = ''\n",
" #-----#\n",
" index = 0\n",
" prompts = {}\n",
" _prompts = {}\n",
" #-------#\n",
" urls = {}\n",
" _urls = {}\n",
" #------#\n",
" text_encodings = {}\n",
" _text_encodings = {}\n",
" image_encodings = {}\n",
" _image_encodings = {}\n",
" #-----#\n",
" for filename in os.listdir(f'{path}'):\n",
"\n",
" print(f'reading {filename}.json...')\n",
" _index = 0\n",
" %cd {path}\n",
" with open(f'{filename}', 'r') as f:\n",
" data = json.load(f)\n",
" _df = pd.DataFrame({'count': data})['count']\n",
" _prompts = {\n",
" key : value for key, value in _df.items()\n",
" }\n",
"\n",
" for key in _prompts:\n",
" _index = int(key)\n",
" value = _prompts[key]\n",
" if _index<=0: continue\n",
" if _index<=1:\n",
" _file_name = f'{value}'\n",
" _file_name_images = _prompts[f'{0}']\n",
" #-------#\n",
" print(f'reading {_file_name_images}.json..')\n",
" %cd {path_images}\n",
" with open(f'{_file_name_images}.json', 'r') as f:\n",
" data = json.load(f)\n",
" _df = pd.DataFrame({'count': data})['count']\n",
" _urls = {\n",
" key : value for key, value in _df.items()\n",
" }\n",
" #--------#\n",
" %cd {path_enc}\n",
" _text_encodings = load_file(f'{_file_name}.safetensors')\n",
" text_encodings[f'{index-1}'] = _text_encodings[f'{_index-1}']\n",
" text_encodings[f'{index}'] = _text_encodings[f'{_index}']\n",
" #-------#\n",
" %cd {path_enc_images}\n",
" _image_encodings = load_file(f'{_file_name_images}.safetensors')\n",
" image_encodings[f'{index-1}'] = _image_encodings[f'{_index-1}']\n",
" image_encodings[f'{index}'] = _image_encodings[f'{_index}']\n",
" #-------#\n",
" prompts[f'{index-1}'] = _prompts[f'{_index-1}']\n",
" urls[f'{index-1}'] = _urls[f'{_index-1}']\n",
" prompts[f'{index}'] = _prompts[f'{_index}']\n",
" urls[f'{index}'] = _urls[f'{_index}']\n",
" #-------#\n",
" index = index + 1\n",
" continue\n",
" #--------#\n",
" #Read the text_encodings + prompts\n",
" text_encodings[f'{index}'] = _text_encodings[f'{_index}']\n",
" image_encodings[f'{index}'] = _image_encodings[f'{_index}']\n",
" prompts[f'{index}'] = _prompts[f'{_index}']\n",
" urls[f'{index}'] = _urls[f'{_index}']\n",
" index = index + 1\n",
" continue\n",
" #-------#\n",
" #--------#\n",
" #----------#\n",
" NUM_ITEMS = index -1\n",
" return prompts , text_encodings , urls , image_encodings , NUM_ITEMS\n",
"#--------#\n",
"\n"
]
},
{
"cell_type": "code",
"source": [
"# @title π Select items to sample from\n",
"\n",
"prompt_features = False # @param {\"type\":\"boolean\",\"placeholder\":\"π¦\"}\n",
"civitai_blue_set = False # @param {\"type\":\"boolean\",\"placeholder\":\"π\"}\n",
"civitai_yellow_set = False # @param {\"type\":\"boolean\",\"placeholder\":\"π\"}\n",
"artby_prompts = False # @param {\"type\":\"boolean\",\"placeholder\":\"π\"}\n",
"suffix = False # @param {\"type\":\"boolean\",\"placeholder\":\"πΉ\"}\n",
"prefix = False # @param {\"type\":\"boolean\",\"placeholder\":\"πΈ\"}\n",
"emojis = False # @param {\"type\":\"boolean\",\"placeholder\":\"π\"}\n",
"#------#\n",
"suffix_pairs = True # @param {\"type\":\"boolean\",\"placeholder\":\"πΉ\"}\n",
"first_names = False # @param {\"type\":\"boolean\",\"placeholder\":\"πΉ\"}\n",
"last_names = False # @param {\"type\":\"boolean\",\"placeholder\":\"πΈ\"}\n",
"celebs = False # @param {\"type\":\"boolean\",\"placeholder\":\"ππ¨\"}\n",
"#-------#\n",
"danbooru_tags = True # @param {\"type\":\"boolean\",\"placeholder\":\"π\"}\n",
"lyrics = False # @param {\"type\":\"boolean\",\"placeholder\":\"πΌ\"}\n",
"tripple_nouns = False # @param {\"type\":\"boolean\",\"placeholder\":\"πΌ\"}\n",
"#-----#\n",
"female_fullnames = False # @param {\"type\":\"boolean\",\"placeholder\":\"π\"}\n",
"debug = False\n",
"#------#\n",
"prompts = {}\n",
"text_encodings = {}\n",
"nA = 0\n",
"#--------#\n",
"\n",
"if suffix_pairs:\n",
" url = '/content/text-to-image-prompts/suffix_pairs'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"\n",
"if tripple_nouns:\n",
" url = '/content/text-to-image-prompts/nouns'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"\n",
"if lyrics:\n",
" url = '/content/text-to-image-prompts/lyrics'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"\n",
"if danbooru_tags:\n",
" url = '/content/text-to-image-prompts/danbooru'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if first_names:\n",
" url = '/content/text-to-image-prompts/names/firstnames'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if last_names:\n",
" url = '/content/text-to-image-prompts/names/lastnames'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if celebs:\n",
" url = '/content/text-to-image-prompts/names/celebs/mixed'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if female_fullnames:\n",
" url = '/content/text-to-image-prompts/names/fullnames'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"\n",
"if prompt_features:\n",
" url = '/content/text-to-image-prompts/civitai-prompts/green'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"\n",
"if emojis:\n",
" url = '/content/text-to-image-prompts/vocab/text_encodings/emoji'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"\n",
"if civitai_blue_set:\n",
" url = '/content/text-to-image-prompts/civitai-prompts/blue'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if civitai_yellow_set:\n",
" url = '/content/text-to-image-prompts/civitai-prompts/yellow'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if artby_prompts:\n",
" url = '/content/text-to-image-prompts/artby'\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#--------#\n",
"\n",
"if suffix :\n",
" tmp = '/content/text-to-image-prompts/vocab/text_encodings/suffix/'\n",
" for item in ['common','average','rare','weird','exotic'] :\n",
" url = tmp + item\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '')\n",
"#------#\n",
"\n",
"if prefix :\n",
" tmp = '/content/text-to-image-prompts/vocab/text_encodings/prefix/'\n",
" for item in ['common','average','rare','weird','exotic'] :\n",
" url = tmp + item\n",
" prompts , text_encodings, nA = append_from_url(prompts , text_encodings, nA , url , '-')\n",
"#------#\n",
"\n",
"if debug:\n",
" index = 0\n",
" for key in prompts: index = index + 1\n",
" print(index)\n",
" index = 0\n",
" for key in text_encodings : index = index + 1\n",
" print(index)\n",
"#------#\n",
"\n",
"NUM_VOCAB_ITEMS = nA\n",
"text_tensor = torch.zeros(NUM_VOCAB_ITEMS,768)\n",
"for index in range(NUM_VOCAB_ITEMS):\n",
" text_tensor[index] = text_encodings[f'{index}']\n",
"#---------#\n"
],
"metadata": {
"id": "CF53WIAKObg3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# @title \tβ Use a pre-encoded prompt + image pair from the fusion gen (note: NSFW!)\n",
"# @markdown πΌοΈ Choose a pre-encoded reference\n",
"index = 617 # @param {type:\"slider\", min:0, max:1666, step:1}\n",
"PROMPT_INDEX = index\n",
"\n",
"import math\n",
"# @markdown -----------\n",
"# @markdown πβ Enhance similarity to prompt(s)\n",
"POS = '' # @param {type:'string' ,placeholder:'item1 , item2 , ...'}\n",
"log_strength = 1.06 # @param {type:\"slider\", min:-5, max:5, step:0.01}\n",
"pos_strength = math.pow(10 ,log_strength-1)\n",
"# @markdown -----------\n",
"\n",
"# @markdown π« Penalize similarity to prompt(s)\n",
"NEG = '' # @param {type:'string' , placeholder:'item1 , item2 , ...'}\n",
"log_strength = 1 # @param {type:\"slider\", min:-5, max:5, step:0.01}\n",
"neg_strength = math.pow(10 ,log_strength-1)\n",
"\n",
"# @markdown β© Skip item(s) containing the word\n",
"SKIP = '_ass , ass_' # @param {type:'string' , placeholder:'item1 , item2 , ...'}\n",
"\n",
"# @markdown βοΈ How important is the prompt vs. the image?
\n",
"# @markdown