Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import random
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
with open('models.pickle', 'rb') as f:
|
| 7 |
+
models = pickle.load(f)
|
| 8 |
+
|
| 9 |
+
LORA_TOKEN = '' # '<|>LORA_TOKEN<|>'
|
| 10 |
+
NOT_SPLIT_TOKEN = '<|>NOT_SPLIT_TOKEN<|>'
|
| 11 |
+
|
| 12 |
+
def sample_next(ctx: str, model, k):
|
| 13 |
+
ctx = ', '.join(ctx.split(', ')[-k:])
|
| 14 |
+
if model.get(ctx) is None:
|
| 15 |
+
return " "
|
| 16 |
+
possible_chars = list(model[ctx].keys())
|
| 17 |
+
possible_values = list(model[ctx].values())
|
| 18 |
+
return np.random.choice(possible_chars, p=possible_values)
|
| 19 |
+
|
| 20 |
+
def generateText(model, minLen=100, size=5, user_idea=None):
|
| 21 |
+
keys = list(model.keys())
|
| 22 |
+
k = len(random.choice(keys).split(', '))
|
| 23 |
+
|
| 24 |
+
# If user provides an idea, use it as the starting point; otherwise, choose randomly
|
| 25 |
+
if user_idea and user_idea.strip():
|
| 26 |
+
starting_sent = user_idea.strip()
|
| 27 |
+
# Ensure the starting sentence is compatible with the model's context format
|
| 28 |
+
starting_sent = starting_sent.replace(', ', NOT_SPLIT_TOKEN)
|
| 29 |
+
else:
|
| 30 |
+
starting_sent = random.choice(keys)
|
| 31 |
+
|
| 32 |
+
sentence = starting_sent
|
| 33 |
+
ctx = ', '.join(starting_sent.split(', ')[-k:]) if ', ' in starting_sent else starting_sent
|
| 34 |
+
|
| 35 |
+
while True:
|
| 36 |
+
next_prediction = sample_next(ctx, model, k)
|
| 37 |
+
sentence += f", {next_prediction}"
|
| 38 |
+
ctx = ', '.join(sentence.split(', ')[-k:])
|
| 39 |
+
if '\n' in sentence:
|
| 40 |
+
break
|
| 41 |
+
|
| 42 |
+
sentence = sentence.replace(NOT_SPLIT_TOKEN, ', ')
|
| 43 |
+
prompt = sentence.split('\n')[0]
|
| 44 |
+
|
| 45 |
+
# Ensure the prompt meets the minimum length requirement
|
| 46 |
+
if len(prompt) < minLen:
|
| 47 |
+
return generateText(model, minLen, size=1, user_idea=user_idea)
|
| 48 |
+
|
| 49 |
+
size = size - 1
|
| 50 |
+
if size == 0:
|
| 51 |
+
return [prompt]
|
| 52 |
+
|
| 53 |
+
output = [prompt]
|
| 54 |
+
for _ in range(size):
|
| 55 |
+
# Generate additional prompts without user_idea to maintain diversity
|
| 56 |
+
new_prompt = generateText(model, minLen, size=1)[0]
|
| 57 |
+
output.append(new_prompt)
|
| 58 |
+
|
| 59 |
+
return output
|
| 60 |
+
|
| 61 |
+
def sentence_builder(quantity, minLen, Type, negative, user_idea):
|
| 62 |
+
if Type == "NSFW":
|
| 63 |
+
idx = 1
|
| 64 |
+
elif Type == "SFW":
|
| 65 |
+
idx = 2
|
| 66 |
+
else:
|
| 67 |
+
idx = 0
|
| 68 |
+
model = models[idx]
|
| 69 |
+
output = ""
|
| 70 |
+
for i in range(quantity):
|
| 71 |
+
# Pass user_idea only for the first prompt if provided
|
| 72 |
+
prompt = generateText(model[0], minLen=minLen, size=1, user_idea=user_idea if i == 0 else None)[0]
|
| 73 |
+
output += f"PROMPT: {prompt}\n\n"
|
| 74 |
+
if negative:
|
| 75 |
+
negative_prompt = generateText(model[1], minLen=minLen, size=5)[0]
|
| 76 |
+
output += f"NEGATIVE PROMPT: {negative_prompt}\n"
|
| 77 |
+
output += "----------------------------------------------------------------\n\n\n"
|
| 78 |
+
|
| 79 |
+
return output[:-3]
|
| 80 |
+
|
| 81 |
+
ui = gr.Interface(
|
| 82 |
+
sentence_builder,
|
| 83 |
+
[
|
| 84 |
+
gr.Slider(1, 10, value=4, label="Count", info="Choose between 1 and 10", step=1),
|
| 85 |
+
gr.Slider(100, 1000, value=300, label="minLen", info="Choose between 100 and 1000", step=50),
|
| 86 |
+
gr.Radio(["NSFW", "SFW", "BOTH"], label="TYPE", info="NSFW stands for NOT SAFE FOR WORK, so choose any one you want?"),
|
| 87 |
+
gr.Checkbox(label="Negative Prompt", info="Do you want to generate negative prompt as well as prompt?"),
|
| 88 |
+
gr.Textbox(label="Your Idea", info="Enter your idea to start the prompt (optional). The AI will enhance it.", placeholder="e.g., A futuristic city with flying cars")
|
| 89 |
+
],
|
| 90 |
+
"text"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
ui.launch()
|