mabzak's picture
Update app.py
88c6133
raw
history blame
2.6 kB
from transformers import pipeline, set_seed
import gradio as grad, random, re
from googletrans import Translator
gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2')
with open("ideas.txt", "r") as f:
line = f.readlines()
def generate(starting_text):
seed = random.randint(100, 1000000)
set_seed(seed)
if starting_text == "":
starting_text: str = line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize()
starting_text: str = re.sub(r"[,:\-–.!;?_]", '', starting_text)
response = gpt2_pipe(starting_text, max_length=(len(starting_text) + random.randint(60, 90)), num_return_sequences=4)
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+'\n')
translator = Translator()
for i, resp in enumerate(response_list):
translated_resp = translator.translate(resp, src='id', dest='en')
response_list[i] = translated_resp.text + '\n'
response_end = "\n".join(response_list)
response_end = re.sub('[^ ]+\.[^ ]+','', response_end)
response_end = response_end.replace("<", "").replace(">", "")
if response_end != "":
return response_end
txt = grad.Textbox(lines=1, label="Initial Text", placeholder="Text here")
out = grad.Textbox(lines=4, label="Generated Prompts")
examples = []
for x in range(8):
examples.append(line[random.randrange(0, len(line))].replace("\n", "").lower().capitalize())
title = "Translator Stable Diffusion Prompt Generator"
description = 'ni adalah demo dari seri model: "MagicPrompt", dalam kasus ini, ditujukan untuk: "Stable Diffusion". Untuk menggunakannya, cukup kirim teks Anda atau klik salah satu contoh. Untuk mempelajari lebih lanjut tentang model ini, klik di sini<br>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>'
grad.Interface(fn=generate,
inputs=txt,
outputs=out,
examples=examples,
title=title,
description=description,
article='',
allow_flagging='never',
cache_examples=False,
theme="default").launch(enable_queue=True, debug=True)