AdamOswald1 commited on
Commit
3e92ae8
·
1 Parent(s): c291b5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -366,4 +366,57 @@ with gr.Blocks(css=css) as demo:
366
  ''')
367
 
368
  demo.queue()
369
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
  ''')
367
 
368
  demo.queue()
369
+ demo.launch()
370
+
371
+ from transformers import pipeline, set_seed
372
+ import gradio as grad, random, re
373
+
374
+
375
+ gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
376
+ with open("ideas.txt", "r") as f:
377
+ line = f.readlines()
378
+
379
+
380
+ def generate(starting_text):
381
+ seed = random.randint(100, 1000000)
382
+ set_seed(seed)
383
+
384
+ if starting_text == "":
385
+ starting_text: str = line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize()
386
+ starting_text: str = re.sub(r"[,:\-–.!;?_]", '', starting_text)
387
+
388
+ response = gpt2_pipe(starting_text, max_length=(len(starting_text) + random.randint(60, 90)), num_return_sequences=4)
389
+ response_list = []
390
+ for x in response:
391
+ resp = x['generated_text'].strip()
392
+ if resp != starting_text and len(resp) > (len(starting_text) + 4) and resp.endswith((":", "-", "—")) is False:
393
+ response_list.append(resp+'\n')
394
+
395
+ response_end = "\n".join(response_list)
396
+ response_end = re.sub('[^ ]+\.[^ ]+','', response_end)
397
+ response_end = response_end.replace("<", "").replace(">", "")
398
+
399
+ if response_end != "":
400
+ return response_end
401
+
402
+
403
+ txt = grad.Textbox(lines=1, label="Initial Text", placeholder="English Text here")
404
+ out = grad.Textbox(lines=4, label="Generated Prompts")
405
+
406
+ examples = []
407
+ for x in range(8):
408
+ examples.append(line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize())
409
+
410
+ title = "Stable Diffusion Prompt Generator"
411
+ 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>'
412
+
413
+ grad.Interface(fn=generate,
414
+ inputs=txt,
415
+ outputs=out,
416
+ examples=examples,
417
+ title=title,
418
+ description=description,
419
+ article='',
420
+ allow_flagging='never',
421
+ cache_examples=False,
422
+ theme="default").launch(enable_queue=True, debug=True)