import gradio as gr from transformers import pipeline, set_seed import random, re from gradio.themes.utils import sizes theme = gr.themes.Default(radius_size=sizes.radius_none).set( block_label_text_color = '#4D63FF', block_title_text_color = '#4D63FF', button_primary_text_color = '#4D63FF', button_primary_background_fill='#FFFFFF', button_primary_border_color='#4D63FF', button_primary_background_fill_hover='#EDEFFF', ) gpt2_pipe = pipeline('text-generation', model='Gustavosta/MagicPrompt-Stable-Diffusion', tokenizer='gpt2') def generate_prompt(text): seed = random.randint(100, 1000000) set_seed(seed) response = gpt2_pipe(text, max_length=(len(text) + random.randint(60, 90)), num_return_sequences=4) response_list = [] for x in response: resp = x['generated_text'].strip() if resp != text and len(resp) > (len(text) + 4) and resp.endswith((":", "-", "—")) is False: response_list.append(resp+'\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 with gr.Blocks(theme=theme, css="footer {visibility: hidden}") as demo: gr.Markdown("""
prompt生成
""") with gr.Row(): with gr.Column(): box1 = gr.Textbox(label="文本") with gr.Row(): button = gr.Button("提交", variant="primary") clear = gr.Button("清除", variant="primary") box2 = gr.Textbox(label="文本") button.click(fn=generate_prompt, inputs=box1, outputs=box2) clear.click(lambda x: gr.update(value=''), [], [box1]) examples = gr.Examples(examples=[["A new hours out of fiend"], ["Third compendium of prague"]], inputs=[box1], label="例子") if __name__ == "__main__": demo.queue(concurrency_count=3) demo.launch(server_name = "0.0.0.0")