File size: 6,840 Bytes
694468d 2e06814 694468d 2e06814 fb7a1fc ef187eb 694468d ef187eb 694468d 63b6eaf fb7a1fc 2b0f02c 694468d 07b2a73 fc4c561 a8da2f4 694468d 7009445 a8da2f4 ec35e66 7009445 a8da2f4 343e2ed 694468d ec35e66 8b1e96d 7009445 694468d a8da2f4 3602629 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 11d8762 694468d 2a9a44a d6aa524 694468d 8e98b35 694468d 5244cb4 694468d a8da2f4 e0fd934 694468d 3a2b9b2 01c2067 694468d e0fd934 de5eb4d 7009445 0941574 e0fd934 a8da2f4 93cb3f7 a8da2f4 fb7a1fc 2e06814 e0fd934 464124b e0fd934 a8da2f4 fb7a1fc a8da2f4 2e06814 a8da2f4 fb7a1fc 45ef37e 5685662 45ef37e 5685662 45ef37e f497fc4 0cffd40 8b3ca8d 11d8762 694468d 11d8762 8b3ca8d 694468d 56a78e5 694468d 8b1e96d 0cffd40 694468d a8da2f4 694468d a8da2f4 694468d 5244cb4 694468d 5244cb4 694468d 7009445 694468d a8da2f4 c57a58b 694468d 657a746 937b15e 657a746 8b1e96d 694468d 5244cb4 694468d a8da2f4 e0fd934 694468d |
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
import os
import re
import random
import json
import requests
import gradio as gr
from gradio_client import Client, file
import numpy as np
from PIL import Image
from typing import Tuple
from translatepy import Translator
from io import BytesIO
MODEL = os.environ.get("MODEL")
MODEL2 = os.environ.get("MODEL2")
MODEL_TEST = os.environ.get("MODEL_TEST")
DESCRIPTION = """
# IMGEN🧚♀️
<center>🧚♂️输入描述生成图片,选择喜欢的风格~<br>🧚默认使用Turbo快速生成,高级选项可更改。</center>
"""
MODEL_ID="stabilityai/sdxl-turbo"
LICENSE = "采用 SDXL 模型"
translator = Translator()
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
return seed
MAX_SEED = np.iinfo(np.int32).max
client = Client(MODEL)
client2 = Client(MODEL2)
client_test=Client(MODEL_TEST)
style_list = [
{
"name": "(无风格)",
"prompt": "{prompt}",
},
{
"name": "电影",
"prompt": "cinematic, {prompt}, detailed, bokeh, moody, film grain",
},
{
"name": "摄影",
"prompt": "realistic photo, {prompt}, 35mm photograph, 4k",
},
{
"name": "动画",
"prompt": "anime style, {prompt}, studio anime",
},
{
"name": "漫画",
"prompt": "manga style, {prompt}, detailed, Japanese comic style",
},
{
"name": "数绘",
"prompt": "{prompt}, digital artwork, detailed",
},
{
"name": "像素",
"prompt": "pixel-art, {prompt}, 8-bit graphics",
},
{
"name": "幻想",
"prompt": "fantasy style, {prompt}, epic, magical, dreamy",
},
{
"name": "朋克",
"prompt": "cyberpunk style, {prompt}, stunningly beautiful, ultra detailed, realistic",
},
{
"name": "三维",
"prompt": "3d model, {prompt}, highly quality, dramatic lighting",
},
]
styles = {k["name"]: (k["prompt"]) for k in style_list}
STYLE_NAMES = list(styles.keys())
DEFAULT_STYLE_NAME = "(无风格)"
def apply_style(style_name: str, positive: str) -> Tuple[str]:
p = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
return p.replace("{prompt}", positive)
def list_dir(path):
for root, dirs, files in os.walk(path):
for file in files:
print(os.path.join(root, file))
def generate(
prompt: str,
seed: float = 0,
steps: float = 1,
style: str = DEFAULT_STYLE_NAME,
randomize_seed: bool = False,
use_turbo: bool = True,
use_flash: bool = False,
progress=gr.Progress(track_tqdm=True),
):
prompt = str(translator.translate(prompt, 'English'))
print(f'prompt:{prompt}')
seed = int(randomize_seed_fn(seed, randomize_seed))
#print(client_test.view_api())
prompt = apply_style(style, prompt)
strength = 0.7
images = []
if use_turbo and use_flash is False:
result = client.predict(
None,
prompt,
strength,
steps,
seed,
fn_index=1
)
print(result)
image_url = MODEL+"file="+result
images_request = requests.get(image_url)
print(f'A:{images_request}')
pil_image = Image.open(BytesIO(images_request.content))
images.append(pil_image)
print(f'B:{images}')
elif use_flash is True:
result = client_test.predict(
prompt,
0,
True,
api_name="/infer"
)
print(result)
image_url = MODEL_TEST+"file="+result
images_request = requests.get(image_url)
print(f'A:{images_request}')
pil_image = Image.open(BytesIO(images_request.content))
images.append(pil_image)
print(f'B:{images}')
else:
seed = 0
job = client2.submit(
prompt,
"low_quality",
7.5,
"(No style)",
fn_index=3
)
result = job.result()
for root, dirs, files in os.walk(result):
for file in files:
print(file)
if file.endswith('.jpg'):
images.append(os.path.join(root,file))
return images, seed
examples = [
"坐在火箭上的秋田犬",
"一只吃芝士片的猫",
"太空中骑马的宇航员",
"放学回家的孩子们",
"一个可爱的机器人在照镜子",
"钢铁侠在恐龙时代"
]
CSS = '''
.gradio-container{max-width: 560px !important}
h1{text-align:center}
footer {
visibility: hidden
}
'''
with gr.Blocks(css=CSS, theme="xiaobaiyuan/theme_brief") as demo:
gr.Markdown(DESCRIPTION)
with gr.Group():
with gr.Row():
prompt = gr.Text(
label="描述",
show_label=False,
max_lines=1,
placeholder="画什么好呢",
container=False,
scale=3,
)
run_button = gr.Button("生成", scale=1)
result = gr.Gallery(label="作品", columns = 1, preview=True)
with gr.Accordion("高级选项", open=False):
with gr.Row():
steps = gr.Slider(
label="步数",
minimum=1,
maximum=10,
step=1,
value=2,
)
seed = gr.Slider(
label="种子",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
visible=True
)
randomize_seed = gr.Checkbox(label="随机种子", value=True)
use_turbo = gr.Checkbox(label="快速生成", value=True)
use_flash = gr.Checkbox(label="Flash", value=False)
with gr.Row(visible=True):
style_selection = gr.Radio(
show_label=True,
container=True,
interactive=True,
choices=STYLE_NAMES,
value=DEFAULT_STYLE_NAME,
label="风格化",
)
with gr.Row():
gr.Examples(
examples=examples,
inputs=prompt,
outputs=[result, seed],
fn=generate,
cache_examples='lazy',
label="示例",
)
with gr.Row():
gr.Markdown(LICENSE)
gr.on(
triggers=[
prompt.submit,
run_button.click,
],
fn=generate,
inputs=[
prompt,
seed,
steps,
style_selection,
randomize_seed,
use_turbo,
use_flash,
],
outputs=[result, seed],
api_name="run",
)
if __name__ == "__main__":
demo.queue(max_size=20).launch(show_api=False, debug=False) |