Spaces:
Runtime error
Runtime error
File size: 2,614 Bytes
19f315c 0837f13 19f315c 04f0ad3 19f315c 328e66f 19f315c 4596128 ad77781 19f315c bd3935d 24b84b4 19f315c 1b41b1b 24b84b4 b7e68fc 89c39b0 0837f13 70b5112 5b793b3 0837f13 f3850b1 0837f13 a0e371a ab1ec1d 2ce9293 0837f13 ab1ec1d 0837f13 70b5112 0837f13 4baa2f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
from transformers import pipeline, set_seed
import gradio as grad, random, re
import os
import sys
gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
def generate(starting_text):
with open("ideas.txt", "r") as f:
line = f.readlines()
seed = random.randint(100, 1000000)
set_seed(seed)
if starting_text == "":
starting_text: str = line[random.randrange(0, len(line))].replace("\n", "").capitalize()
starting_text: str = re.sub(r"\.", '', starting_text)
response = gpt2_pipe(starting_text, max_length=(len(starting_text) + random.randint(60, 80)), num_return_sequences=1)
response_list = []
for x in response:
resp = x['generated_text'].strip()
if resp != starting_text and len(resp) > (len(starting_text) + 4) and resp.endswith((":", "-", "—")) is False:
response_list.append(resp)
response_end = "\n".join(response_list)
response_end = re.sub('[^ ]+\.[^ ]+','', response_end)
response_end = response_end.replace("<", "").replace(">", "")
if response_end != "":
return response_end
with grad.Blocks(theme='SebastianBravo/simci_css') as demo:
grad.HTML(
"""
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
<div>
<h4>
Magic Prompt generator
</h4>
"""
)
with grad.Column(elem_id="col-container"):
with grad.Row(variant="compact"):
txt = grad.Textbox(
label="Initial Text",
show_label=False,
max_lines=1,
placeholder="Enter a basic idea",
container=False,
)
run = grad.Button("✨ Magic Prompt ✨")
with grad.Row(variant="compact"):
out = grad.Textbox(
label="Generated Text",
show_label=False,
lines=5,
container=False,
)
run.click(generate, inputs=[txt], outputs=[out])
with grad.Row():
grad.HTML(
"""
<div class="footer">
<p> Powered by <a href="https://huggingface.co/Gustavosta">Gustavosta</a> Stable Diffusion model
</p>
</div>
<div class="acknowledgments" style="font-size: 115%">
<p> Input a short prompt idea, get a uniq advanced prompt in seconds </p>
</div>
"""
)
fn=generate,
run=generate,
inputs=txt,
outputs=out
demo.launch() |