MatsuoLab/text2image_3
0
1import os2from io import BytesIO3 4import gradio as gr5import numpy as np6import replicate7import requests8from PIL import Image9 10model_id = "stability-ai/sdxl:2b017d9b67edd2ee1401238df49d75da53c523f36e363881e057f5dc3ed3c5b2"11default_steps = 2512def generate(prompt: str, secret_key: str):13 """14 プロンプトから生成画像(PIL.Image.open)を取得15 """16 if secret_key == os.environ["SECRET_KEY"]:17 output = replicate.run(18 model_id,19 input={20 "prompt": prompt,21 "num_inference_steps": default_steps,22 },23 )24 # リンク取得25 png_link = output[0]26 # PNGファイルをリンクから取得27 response = requests.get(png_link)28 # イメージをメモリ上に開く29 img = Image.open(BytesIO(response.content))30 return img31 32 33examples = [34 ["station"],35 ["station, ghibli style"],36 ["Elon Musk"],37 ["Elon Musk playing Shogi"],38 # ["An astronaut riding a rainbow unicorn, cinematic, dramatic", ""],39 # ["A robot painted as graffiti on a brick wall. a sidewalk is in front of the wall, and grass is growing out of cracks in the concrete.", ""],40 # ["Panda mad scientist mixing sparkling chemicals, artstation.", ""],41 # ["An astronaut riding a rainbow unicorn, cinematic, dramatic"],42 # [43 # "photo of a rhino dressed suit and tie sitting at a table in a bar with a bar stools, award winning photography."44 # ],45 # ["a giant monster hybrid of dragon and spider, in dark dense foggy forest"],46 # [47 # "a man in a space suit playing a piano, highly detailed illustration, full color illustration, very detailed illustration"48 # ],49]50 51with gr.Blocks(title="Stable Diffusion XL (SDXL 1.0)") as demo:52 with gr.Row():53 with gr.Column(scale=1, min_width=600):54 gr_prompt = gr.Textbox(label="プロンプト")55 gr_password = gr.Textbox(label="パスワード")56 gr_generate_button = gr.Button("生成")57 # with gr.Accordion("advanced settings", open=False):58 # gr_steps = gr.Number(label="steps", value=default_steps)59 # gr_seed = gr.Number(label="seed", value=-1)60 with gr.Column(scale=1, min_width=600):61 gr_image = gr.Image()62 # examples=examples63 gr_generate_button.click(64 generate,65 inputs=[gr_prompt, gr_password],66 outputs=[gr_image],67 )68 with gr.Row():69 gr.Examples(examples, inputs=[gr_prompt], label="プロンプト例")70 71demo.launch()72 