import os import requests import gradio as gr import pickle import random import numpy as np url = "https://huggingface.co/thefcraft/prompt-generator-stable-diffusion/resolve/main/models.pickle" if not os.path.exists('models.pickle'): response = requests.get(url, stream=True) with open('models.pickle', "wb") as handle: for data in response.iter_content(): handle.write(data) with open('models.pickle', 'rb')as f: models = pickle.load(f) LORA_TOKEN = ''#'<|>LORA_TOKEN<|>' # WEIGHT_TOKEN = '<|>WEIGHT_TOKEN<|>' NOT_SPLIT_TOKEN = '<|>NOT_SPLIT_TOKEN<|>' def sample_next(ctx:str,model,k): ctx = ', '.join(ctx.split(', ')[-k:]) if model.get(ctx) is None: return " " possible_Chars = list(model[ctx].keys()) possible_values = list(model[ctx].values()) # print(possible_Chars) # print(possible_values) return np.random.choice(possible_Chars,p=possible_values) def generateText(model, minLen=100, size=5): keys = list(model.keys()) starting_sent = random.choice(keys) k = len(random.choice(keys).split(', ')) sentence = starting_sent ctx = ', '.join(starting_sent.split(', ')[-k:]) while True: next_prediction = sample_next(ctx,model,k) sentence += f", {next_prediction}" ctx = ', '.join(sentence.split(', ')[-k:]) # if sentence.count('\n')>size: break if '\n' in sentence: break sentence = sentence.replace(NOT_SPLIT_TOKEN, ', ') # sentence = re.sub(WEIGHT_TOKEN.replace('|', '\|'), lambda match: f":{random.randint(0,2)}.{random.randint(0,9)}", sentence) # sentence = sentence.replace(":0.0", ':0.1') # return sentence prompt = sentence.split('\n')[0] if len(prompt)