Commit
·
ec47c3f
1
Parent(s):
18c587d
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ model_id = "runwayml/stable-diffusion-v1-5"
|
|
12 |
pipe = StableDiffusionPipeline.from_pretrained(model_id).to('cpu')
|
13 |
|
14 |
gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
|
|
|
15 |
|
16 |
def infer1(starting_text):
|
17 |
seed = random.randint(100, 1000000)
|
@@ -34,7 +35,28 @@ def infer1(starting_text):
|
|
34 |
if response_end != "":
|
35 |
return response_end
|
36 |
|
37 |
-
def infer2(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
generator = torch.Generator(device='cpu').manual_seed(seed)
|
39 |
img = pipe(
|
40 |
prompt,
|
@@ -52,6 +74,11 @@ block = gr.Blocks()
|
|
52 |
with block:
|
53 |
with gr.Group():
|
54 |
with gr.Box():
|
|
|
|
|
|
|
|
|
|
|
55 |
with gr.Row() as row:
|
56 |
with gr.Column():
|
57 |
txt = gr.Textbox(lines=1, label="Initial Text", placeholder="English Text here")
|
@@ -61,8 +88,29 @@ with block:
|
|
61 |
)
|
62 |
with gr.Column():
|
63 |
out = gr.Textbox(lines=4, label="Generated Prompts")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
with gr.Box():
|
|
|
|
|
|
|
|
|
|
|
66 |
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
67 |
with gr.Column():
|
68 |
text = gr.Textbox(
|
@@ -100,8 +148,8 @@ with block:
|
|
100 |
scale = gr.Slider(label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1, interactive=True)
|
101 |
seed = gr.Slider(label="Random seed",minimum=0,maximum=2147483647,step=1,randomize=True,interactive=True)
|
102 |
|
103 |
-
title = "Stable Diffusion Prompt Generator"
|
104 |
gpt_btn.click(infer1,inputs=txt,outputs=out)
|
105 |
-
|
|
|
106 |
|
107 |
block.launch(show_api=False,enable_queue=True, debug=True)
|
|
|
12 |
pipe = StableDiffusionPipeline.from_pretrained(model_id).to('cpu')
|
13 |
|
14 |
gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
|
15 |
+
gpt2_pipe2 = pipeline('text-generation', model='succinctly/text2image-prompt-generator')
|
16 |
|
17 |
def infer1(starting_text):
|
18 |
seed = random.randint(100, 1000000)
|
|
|
35 |
if response_end != "":
|
36 |
return response_end
|
37 |
|
38 |
+
def infer2(starting_text):
|
39 |
+
seed = random.randint(100, 1000000)
|
40 |
+
set_seed(seed)
|
41 |
+
|
42 |
+
if starting_text == "":
|
43 |
+
starting_text: str = re.sub(r"[,:\-–.!;?_]", '', starting_text)
|
44 |
+
|
45 |
+
response = gpt2_pipe2(starting_text, max_length=(len(starting_text) + random.randint(60, 90)), num_return_sequences=4)
|
46 |
+
response_list = []
|
47 |
+
for x in response:
|
48 |
+
resp = x['generated_text'].strip()
|
49 |
+
if resp != starting_text and len(resp) > (len(starting_text) + 4) and resp.endswith((":", "-", "—")) is False:
|
50 |
+
response_list.append(resp+'\n')
|
51 |
+
|
52 |
+
response_end = "\n".join(response_list)
|
53 |
+
response_end = re.sub('[^ ]+\.[^ ]+','', response_end)
|
54 |
+
response_end = response_end.replace("<", "").replace(">", "")
|
55 |
+
|
56 |
+
if response_end != "":
|
57 |
+
return response_end
|
58 |
+
|
59 |
+
def infer3(prompt, negative, steps, scale, seed):
|
60 |
generator = torch.Generator(device='cpu').manual_seed(seed)
|
61 |
img = pipe(
|
62 |
prompt,
|
|
|
74 |
with block:
|
75 |
with gr.Group():
|
76 |
with gr.Box():
|
77 |
+
gr.Markdown(
|
78 |
+
"""
|
79 |
+
Model: Gustavosta/MagicPrompt-Stable-Diffusion
|
80 |
+
"""
|
81 |
+
)
|
82 |
with gr.Row() as row:
|
83 |
with gr.Column():
|
84 |
txt = gr.Textbox(lines=1, label="Initial Text", placeholder="English Text here")
|
|
|
88 |
)
|
89 |
with gr.Column():
|
90 |
out = gr.Textbox(lines=4, label="Generated Prompts")
|
91 |
+
|
92 |
+
with gr.Box():
|
93 |
+
gr.Markdown(
|
94 |
+
"""
|
95 |
+
Model: succinctly/text2image-prompt-generator
|
96 |
+
"""
|
97 |
+
)
|
98 |
+
with gr.Row() as row:
|
99 |
+
with gr.Column():
|
100 |
+
txt2 = gr.Textbox(lines=1, label="Initial Text", placeholder="English Text here")
|
101 |
+
gpt_btn2 = gr.Button("Generate prompt").style(
|
102 |
+
margin=False,
|
103 |
+
rounded=(False, True, True, False),
|
104 |
+
)
|
105 |
+
with gr.Column():
|
106 |
+
out2 = gr.Textbox(lines=4, label="Generated Prompts")
|
107 |
|
108 |
with gr.Box():
|
109 |
+
gr.Markdown(
|
110 |
+
"""
|
111 |
+
Model: stable diffusion v1.5
|
112 |
+
"""
|
113 |
+
)
|
114 |
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
115 |
with gr.Column():
|
116 |
text = gr.Textbox(
|
|
|
148 |
scale = gr.Slider(label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1, interactive=True)
|
149 |
seed = gr.Slider(label="Random seed",minimum=0,maximum=2147483647,step=1,randomize=True,interactive=True)
|
150 |
|
|
|
151 |
gpt_btn.click(infer1,inputs=txt,outputs=out)
|
152 |
+
gpt_btn2.click(infer2,inputs=txt2,outputs=out2)
|
153 |
+
btn.click(infer3, inputs=[text, negative, steps, scale, seed], outputs=[gallery])
|
154 |
|
155 |
block.launch(show_api=False,enable_queue=True, debug=True)
|