Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -195,7 +195,59 @@ models = [
|
|
| 195 |
|
| 196 |
current_model = models[0]
|
| 197 |
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
models2 = []
|
| 201 |
for model in models:
|
|
|
|
| 195 |
|
| 196 |
current_model = models[0]
|
| 197 |
|
| 198 |
+
from transformers import pipeline, set_seed
|
| 199 |
+
import gradio as grad, random, re
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
|
| 203 |
+
with open("ideas.txt", "r") as f:
|
| 204 |
+
line = f.readlines()
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def generate(starting_text):
|
| 208 |
+
seed = random.randint(100, 1000000)
|
| 209 |
+
set_seed(seed)
|
| 210 |
+
|
| 211 |
+
if starting_text == "":
|
| 212 |
+
starting_text: str = line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize()
|
| 213 |
+
starting_text: str = re.sub(r"[,:\-β.!;?_]", '', starting_text)
|
| 214 |
+
|
| 215 |
+
response = gpt2_pipe(starting_text, max_length=(len(starting_text) + random.randint(60, 90)), num_return_sequences=4)
|
| 216 |
+
response_list = []
|
| 217 |
+
for x in response:
|
| 218 |
+
resp = x['generated_text'].strip()
|
| 219 |
+
if resp != starting_text and len(resp) > (len(starting_text) + 4) and resp.endswith((":", "-", "β")) is False:
|
| 220 |
+
response_list.append(resp+'\n')
|
| 221 |
+
|
| 222 |
+
response_end = "\n".join(response_list)
|
| 223 |
+
response_end = re.sub('[^ ]+\.[^ ]+','', response_end)
|
| 224 |
+
response_end = response_end.replace("<", "").replace(">", "")
|
| 225 |
+
|
| 226 |
+
if response_end != "":
|
| 227 |
+
return response_end
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
txt = grad.Textbox(lines=1, label="Initial Text", placeholder="English Text here")
|
| 231 |
+
out = grad.Textbox(lines=4, label="Generated Prompts")
|
| 232 |
+
|
| 233 |
+
examples = []
|
| 234 |
+
for x in range(8):
|
| 235 |
+
examples.append(line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize())
|
| 236 |
+
|
| 237 |
+
title = "Stable Diffusion Prompt Generator"
|
| 238 |
+
description = 'This is a demo of the model series: "MagicPrompt", in this case, aimed at: "Stable Diffusion". To use it, simply submit your text or click on one of the examples. To learn more about the model, [click here](https://huggingface.co/Gustavosta/MagicPrompt-Stable-Diffusion).<br>'
|
| 239 |
+
|
| 240 |
+
text_gen = grad.Interface(fn=generate,
|
| 241 |
+
inputs=txt,
|
| 242 |
+
outputs=out,
|
| 243 |
+
examples=examples,
|
| 244 |
+
title=title,
|
| 245 |
+
description=description,
|
| 246 |
+
article='',
|
| 247 |
+
allow_flagging='never',
|
| 248 |
+
cache_examples=False,
|
| 249 |
+
theme="default").launch(enable_queue=True, debug=True)
|
| 250 |
+
# text_gen = gr.Interface.load("spaces/Omnibus/MagicPrompt-Stable-Diffusion_link")
|
| 251 |
|
| 252 |
models2 = []
|
| 253 |
for model in models:
|