Spaces:
Runtime error
Runtime error
Make it work
Browse files
README.md
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
---
|
2 |
-
title: Prompt Fungineer 355M
|
3 |
-
emoji:
|
4 |
colorFrom: red
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.24.1
|
8 |
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Midjourney / Dalle 2 / Stable Diffusion Prompt Generator (Prompt Fungineer 355M)
|
3 |
+
emoji: π§πΌββοΈ
|
4 |
colorFrom: red
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.24.1
|
8 |
app_file: app.py
|
9 |
+
pinned: false
|
|
|
|
|
|
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
#import peft
|
4 |
import transformers
|
5 |
import os
|
|
|
6 |
|
7 |
device = "cpu"
|
8 |
is_peft = False
|
@@ -18,25 +19,92 @@ auth_token = os.environ.get("hub_token") or True
|
|
18 |
model = transformers.AutoModelForCausalLM.from_pretrained(model_id, low_cpu_mem_usage=True,use_auth_token=auth_token)
|
19 |
tokenizer = transformers.AutoTokenizer.from_pretrained("gpt2")
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
if not prompt.startswith("BRF:"):
|
24 |
prompt = "BRF: " + prompt
|
25 |
|
|
|
|
|
|
|
26 |
model.eval()
|
27 |
# SOFT SAMPLE
|
28 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
29 |
samples = []
|
30 |
try:
|
31 |
for i in range(1):
|
32 |
-
outputs = model.generate(**inputs, max_length=256, do_sample=True, top_k=
|
33 |
for output in outputs:
|
34 |
sample = tokenizer.decode(output, skip_special_tokens=True)
|
|
|
35 |
samples.append(sample)
|
36 |
except Exception as e:
|
37 |
print(e)
|
38 |
|
39 |
return samples
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
#import peft
|
4 |
import transformers
|
5 |
import os
|
6 |
+
import re
|
7 |
|
8 |
device = "cpu"
|
9 |
is_peft = False
|
|
|
19 |
model = transformers.AutoModelForCausalLM.from_pretrained(model_id, low_cpu_mem_usage=True,use_auth_token=auth_token)
|
20 |
tokenizer = transformers.AutoTokenizer.from_pretrained("gpt2")
|
21 |
|
22 |
+
|
23 |
+
def format_prompt(prompt, enhancers=True, inspiration=False, negative_prompt=False):
|
24 |
+
try:
|
25 |
+
pattern = r"(BRF:|POS:|ENH:|INS:|NEG:) (.*?)(?= (BRF:|POS:|ENH:|INS:|NEG:)|$)"
|
26 |
+
matches = re.findall(pattern, prompt)
|
27 |
+
vals = {key: value.strip() for key, value,ex in matches}
|
28 |
+
result = vals["POS:"]
|
29 |
+
if enhancers:
|
30 |
+
result += " " + vals["ENH:"]
|
31 |
+
if inspiration:
|
32 |
+
result += " " + vals["INS:"]
|
33 |
+
if negative_prompt:
|
34 |
+
result += "\n\n--no " + vals["NEG:"]
|
35 |
+
|
36 |
+
return result
|
37 |
+
except Exception as e:
|
38 |
+
return "Failed to generate prompt."
|
39 |
+
|
40 |
+
|
41 |
+
def generate_text(prompt, extra=False, top_k=100, top_p=0.95, temperature=0.85, enhancers = True, inpspiration = False , negative_prompt = False):
|
42 |
|
43 |
if not prompt.startswith("BRF:"):
|
44 |
prompt = "BRF: " + prompt
|
45 |
|
46 |
+
if not extra:
|
47 |
+
prompt = prompt + " POS:"
|
48 |
+
|
49 |
model.eval()
|
50 |
# SOFT SAMPLE
|
51 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
52 |
samples = []
|
53 |
try:
|
54 |
for i in range(1):
|
55 |
+
outputs = model.generate(**inputs, max_length=256, do_sample=True, top_k=top_k, top_p=top_p, temperature=temperature, num_return_sequences=4, pad_token_id=tokenizer.eos_token_id)
|
56 |
for output in outputs:
|
57 |
sample = tokenizer.decode(output, skip_special_tokens=True)
|
58 |
+
sample = format_prompt(sample, enhancers, inpspiration, negative_prompt)
|
59 |
samples.append(sample)
|
60 |
except Exception as e:
|
61 |
print(e)
|
62 |
|
63 |
return samples
|
64 |
|
65 |
+
|
66 |
+
# inputs = [
|
67 |
+
# gr.Textbox(lines=5, label="Base Prompt", placeholder="An astronaut in space", info="Enter a very simple prompt that will be fungineered into something exciting!"),
|
68 |
+
# gr.Checkbox(value=True, label="Extra Fungineer Imagination", info="If checked, the model will be allowed to go wild with its imagination."),
|
69 |
+
# gr.Slider( minimum=10, maximum=1000, value=100, label="Top K", info="Top K sampling"),
|
70 |
+
# gr.Slider( minimum=0.1, maximum=1, value=0.95, step=0.01, label="Top P", info="Top P sampling"),
|
71 |
+
# gr.Slider( minimum=0.1, maximum=1.2, value=0.85, step=0.01, label="Temperature", info="Temperature sampling. Higher values will make the model more creative"),
|
72 |
+
# ]
|
73 |
+
|
74 |
+
# iface = gr.Interface(fn=generate_text, inputs=inputs, outputs=["text","text","text","text"] )
|
75 |
+
|
76 |
+
with gr.Blocks() as fungineer:
|
77 |
+
with gr.Row():
|
78 |
+
gr.Markdown("""# Midjourney / Dalle 2 / Stable Diffusion Prompt Generator
|
79 |
+
This is the 355M parameter model. There is also a 7B parameter model that is much better but far slower (access coming soon).
|
80 |
+
Just enter a basic prompt and the fungineering model will use its wildest imagination to expand the prompt in detail.""")
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column():
|
83 |
+
base_prompt = gr.Textbox(lines=5, label="Base Prompt", placeholder="An astronaut in space", info="Enter a very simple prompt that will be fungineered into something exciting!")
|
84 |
+
extra = gr.Checkbox(value=True, label="Extra Fungineer Imagination", info="If checked, the model will be allowed to go wild with its imagination.")
|
85 |
+
with gr.Accordion("Advanced Generation Settings", open=False):
|
86 |
+
top_k = gr.Slider( minimum=10, maximum=1000, value=100, label="Top K", info="Top K sampling")
|
87 |
+
top_p = gr.Slider( minimum=0.1, maximum=1, value=0.95, step=0.01, label="Top P", info="Top P sampling")
|
88 |
+
temperature = gr.Slider( minimum=0.1, maximum=1.2, value=0.85, step=0.01, label="Temperature", info="Temperature sampling. Higher values will make the model more creative")
|
89 |
+
|
90 |
+
with gr.Accordion("Advanced Output Settings", open=False):
|
91 |
+
gr.Checkbox(value=True, label="Enhancers", info="Add image meta information such as lens type, shuffter speed, camera model, etc.")
|
92 |
+
gr.Checkbox(value=False, label="Inpsiration", info="Include inspirational photographers that are known for this type of photography. Sometimes random people will appear here, needs more training.")
|
93 |
+
gr.Checkbox(value=False, label="Negative Prompt", info="Include a negative prompt, more often used in Stable Diffusion. If you're a Stable Diffusion user, chances are you already have a better negative prompt you like to use.")
|
94 |
+
|
95 |
+
with gr.Column():
|
96 |
+
outputs = [
|
97 |
+
gr.Textbox(lines=5, label="Fungineered Text 1"),
|
98 |
+
gr.Textbox(lines=5, label="Fungineered Text 2"),
|
99 |
+
gr.Textbox(lines=5, label="Fungineered Text 3"),
|
100 |
+
gr.Textbox(lines=5, label="Fungineered Text 4"),
|
101 |
+
]
|
102 |
+
|
103 |
+
inputs = [base_prompt, extra, top_k, top_p, temperature]
|
104 |
+
|
105 |
+
|
106 |
+
submit = gr.Button(label="Fungineer",variant="primary")
|
107 |
+
submit.click(generate_text, inputs=inputs, outputs=outputs)
|
108 |
+
|
109 |
+
fungineer.launch(enable_queue=True)
|
110 |
+
|