Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -10,8 +10,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
10 |
|
11 |
device = torch.device("cuda:0")
|
12 |
|
13 |
-
llm = AutoModelForCausalLM.from_pretrained("Azure99/blossom-v5-9b", torch_dtype=torch.float16, device_map="auto")
|
14 |
-
tokenizer = AutoTokenizer.from_pretrained("Azure99/blossom-v5-9b")
|
15 |
diffusion_pipe = DiffusionPipeline.from_pretrained(
|
16 |
"playgroundai/playground-v2.5-1024px-aesthetic",
|
17 |
torch_dtype=torch.float16,
|
@@ -21,68 +19,22 @@ diffusion_pipe = DiffusionPipeline.from_pretrained(
|
|
21 |
).to(device)
|
22 |
|
23 |
|
24 |
-
def get_input_ids(inst, bot_prefix):
|
25 |
-
return tokenizer.encode("A chat between a human and an artificial intelligence bot. "
|
26 |
-
"The bot gives helpful, detailed, and polite answers to the human's questions.\n"
|
27 |
-
f"|Human|: {inst}\n|Bot|: {bot_prefix}", add_special_tokens=True)
|
28 |
-
|
29 |
-
|
30 |
def save_image(img):
|
31 |
unique_name = str(uuid.uuid4()) + ".png"
|
32 |
img.save(unique_name)
|
33 |
return unique_name
|
34 |
|
35 |
|
36 |
-
LLM_PROMPT = '''你的任务是从输入的[作画要求]中抽取画面描述(description),然后description翻译为英文(en_description),最后对en_description进行扩写(expanded_description),增加足够多的细节,且符合人类的第一直觉。
|
37 |
-
[输出]是一个json,包含description、en_description、expanded_description三个字符串字段,请直接输出一个完整的json,不要输出任何解释或其他无关内容。
|
38 |
-
|
39 |
-
下面是一些示例:
|
40 |
-
[作画要求]->"画一幅画:落霞与孤鹜齐飞,秋水共长天一色。"
|
41 |
-
[输出]->{"description": "落霞与孤鹜齐飞,秋水共长天一色", "en_description": "The setting sun and the solitary duck fly together, the autumn water shares a single hue with the vast sky", "expanded_description": "A lone duck gracefully gliding across the tranquil surface of a shimmering lake, bathed in the warm golden glow of the setting sun, creating a breathtaking scene of natural beauty and tranquility."}
|
42 |
-
|
43 |
-
[作画要求]->"原神中的可莉"
|
44 |
-
[输出]->{"description": "原神中的可莉", "en_description": "Klee in Genshin Impact", "expanded_description": "An artistic portrait of Klee from Genshin Impact, standing in a vibrant meadow with colorful explosions of her elemental abilities in the background."}
|
45 |
-
|
46 |
-
[作画要求]->"create an image for me. a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone"
|
47 |
-
[输出]->{"description": "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone", "en_description": "a close up of a woman wearing a transparent, prismatic, elaborate nemeses headdress, over the should pose, brown skin-tone", "expanded_description": "A close-up portrait of an elegant woman with rich brown skin, wearing a stunning transparent, prismatic, and intricately detailed Nemes headdress, striking a confident and alluring over-the-shoulder pose."}
|
48 |
-
|
49 |
-
[作画要求]->"一只高贵的柯基犬,素描画风格\n根据上面的描述生成一张图片吧!"
|
50 |
-
[输出]->{"description": "一只高贵的柯基犬,素描画风格", "en_description": "A noble corgi dog, sketch style", "expanded_description": "A majestic corgi with a regal bearing, depicted in a detailed and intricate pencil sketch, capturing the essence of its noble lineage and dignified presence."}
|
51 |
-
|
52 |
-
[作画要求]->$USER_PROMPT
|
53 |
-
[输出]->'''
|
54 |
-
|
55 |
-
BOT_PREFIX = '{"description": "'
|
56 |
-
|
57 |
-
|
58 |
@spaces.GPU(enable_queue=True)
|
59 |
def generate(
|
60 |
prompt: str,
|
61 |
progress=gr.Progress(track_tqdm=True),
|
62 |
):
|
63 |
-
input_ids = get_input_ids(LLM_PROMPT.replace("$USER_PROMPT", json.dumps(prompt, ensure_ascii=False)), BOT_PREFIX)
|
64 |
-
generation_kwargs = dict(input_ids=torch.tensor([input_ids]).to(llm.device), do_sample=True,
|
65 |
-
max_new_tokens=512, temperature=0.5, top_p=0.85, top_k=50, repetition_penalty=1.05)
|
66 |
-
llm_result = llm.generate(**generation_kwargs)
|
67 |
-
llm_result = llm_result.cpu()[0][len(input_ids):]
|
68 |
-
llm_result = BOT_PREFIX + tokenizer.decode(llm_result, skip_special_tokens=True)
|
69 |
-
print("----------")
|
70 |
-
print(prompt)
|
71 |
-
print(llm_result)
|
72 |
-
en_prompt = prompt
|
73 |
-
expanded_prompt = prompt
|
74 |
-
try:
|
75 |
-
en_prompt = json.loads(llm_result)["en_description"]
|
76 |
-
expanded_prompt = json.loads(llm_result)["expanded_description"]
|
77 |
-
except:
|
78 |
-
print("error, fallback to original prompt")
|
79 |
-
pass
|
80 |
-
|
81 |
seed = random.randint(0, 2147483647)
|
82 |
generator = torch.Generator().manual_seed(seed)
|
83 |
|
84 |
images = diffusion_pipe(
|
85 |
-
prompt=[
|
86 |
negative_prompt=None,
|
87 |
width=1024,
|
88 |
height=1024,
|
|
|
10 |
|
11 |
device = torch.device("cuda:0")
|
12 |
|
|
|
|
|
13 |
diffusion_pipe = DiffusionPipeline.from_pretrained(
|
14 |
"playgroundai/playground-v2.5-1024px-aesthetic",
|
15 |
torch_dtype=torch.float16,
|
|
|
19 |
).to(device)
|
20 |
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def save_image(img):
|
23 |
unique_name = str(uuid.uuid4()) + ".png"
|
24 |
img.save(unique_name)
|
25 |
return unique_name
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
@spaces.GPU(enable_queue=True)
|
29 |
def generate(
|
30 |
prompt: str,
|
31 |
progress=gr.Progress(track_tqdm=True),
|
32 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
seed = random.randint(0, 2147483647)
|
34 |
generator = torch.Generator().manual_seed(seed)
|
35 |
|
36 |
images = diffusion_pipe(
|
37 |
+
prompt=[prompt],
|
38 |
negative_prompt=None,
|
39 |
width=1024,
|
40 |
height=1024,
|