thefcraft commited on
Commit
856f2f9
1 Parent(s): 62e2d59

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from tqdm import tqdm
4
+ import gradio as gr
5
+ import pickle
6
+ import random
7
+ import numpy as np
8
+
9
+ url = "https://huggingface.co/thefcraft/prompt-generator-stable-diffusion/resolve/main/models.pickle"
10
+
11
+ if not os.path.exists('models.pickle'):
12
+ response = requests.get(url, stream=True)
13
+
14
+ with open('models.pickle', "wb") as handle:
15
+ for data in tqdm(response.iter_content()):
16
+ handle.write(data)
17
+
18
+
19
+ with open('models.pickle', 'rb')as f:
20
+ models = pickle.load(f)
21
+
22
+ LORA_TOKEN = ''#'<|>LORA_TOKEN<|>'
23
+ # WEIGHT_TOKEN = '<|>WEIGHT_TOKEN<|>'
24
+ NOT_SPLIT_TOKEN = '<|>NOT_SPLIT_TOKEN<|>'
25
+
26
+ def sample_next(ctx:str,model,k):
27
+
28
+ ctx = ', '.join(ctx.split(', ')[-k:])
29
+ if model.get(ctx) is None:
30
+ return " "
31
+ possible_Chars = list(model[ctx].keys())
32
+ possible_values = list(model[ctx].values())
33
+
34
+ # print(possible_Chars)
35
+ # print(possible_values)
36
+
37
+ return np.random.choice(possible_Chars,p=possible_values)
38
+
39
+ def generateText(model, minLen=100, size=5):
40
+ keys = list(model.keys())
41
+ starting_sent = random.choice(keys)
42
+ k = len(random.choice(keys).split(', '))
43
+
44
+ sentence = starting_sent
45
+ ctx = ', '.join(starting_sent.split(', ')[-k:])
46
+
47
+ while True:
48
+ next_prediction = sample_next(ctx,model,k)
49
+ sentence += f", {next_prediction}"
50
+ ctx = ', '.join(sentence.split(', ')[-k:])
51
+
52
+ # if sentence.count('\n')>size: break
53
+ if '\n' in sentence: break
54
+ sentence = sentence.replace(NOT_SPLIT_TOKEN, ', ')
55
+ # sentence = re.sub(WEIGHT_TOKEN.replace('|', '\|'), lambda match: f":{random.randint(0,2)}.{random.randint(0,9)}", sentence)
56
+ # sentence = sentence.replace(":0.0", ':0.1')
57
+ # return sentence
58
+
59
+ prompt = sentence.split('\n')[0]
60
+ if len(prompt)<minLen:
61
+ prompt = generateText(model, minLen, size=1)[0]
62
+
63
+ size = size-1
64
+ if size == 0: return [prompt]
65
+ output = []
66
+ for i in range(size+1):
67
+ prompt = generateText(model, minLen, size=1)[0]
68
+ output.append(prompt)
69
+
70
+ return output
71
+
72
+ def sentence_builder(quantity, Type, negative):
73
+ if Type == "NSFW": idx=1
74
+ elif Type == "SFW": idx=2
75
+ else: idx=0
76
+ model = models[idx]
77
+ output = ""
78
+ for i in range(quantity):
79
+ prompt = generateText(model[0], minLen=300, size=1)[0]
80
+ output+=f"PROMPT: {prompt}\n\n"
81
+ if negative:
82
+ negative_prompt = generateText(model[1], minLen=300, size=5)[0]
83
+ output+=f"NEGATIVE PROMPT: {prompt}\n"
84
+ output+="----------------------------------------------------------------"
85
+ output+="\n\n\n"
86
+
87
+ return output[:-3]
88
+
89
+
90
+ ui = gr.Interface(
91
+ sentence_builder,
92
+ [
93
+ gr.Slider(1, 10, value=4, label="Count", info="Choose between 1 and 10", step=1),
94
+ gr.Radio(["NSFW", "SFW", "BOTH"], label="TYPE", info="NSFW stands for NOT SAFE FOR WORK, so choose any one you want?"),
95
+ gr.Checkbox(label="negitive Prompt", info="Do you want to generate negative prompt as well as prompt?"),
96
+ ],
97
+ "text"
98
+ )
99
+
100
+ if __name__ == "__main__":
101
+ ui.launch()