Spaces:
Build error
Build error
File size: 14,248 Bytes
a1ca2de 3332aa4 a1ca2de 3332aa4 a1ca2de 936d161 cc9f713 a1ca2de 936d161 a1ca2de 936d161 a1ca2de 936d161 a1ca2de 936d161 a1ca2de 936d161 3332aa4 a1ca2de 3332aa4 a1ca2de 936d161 a1ca2de 3332aa4 936d161 a1ca2de 3332aa4 936d161 a1ca2de 936d161 3332aa4 a1ca2de 3332aa4 a1ca2de 3332aa4 a1ca2de 3332aa4 936d161 3332aa4 936d161 a1ca2de 3332aa4 936d161 a1ca2de 3332aa4 936d161 a1ca2de 936d161 3332aa4 a1ca2de 3332aa4 a1ca2de 936d161 a1ca2de 3332aa4 936d161 a1ca2de 3332aa4 936d161 a1ca2de 3332aa4 a1ca2de 3332aa4 a1ca2de 3332aa4 a1ca2de 936d161 a1ca2de 3332aa4 a1ca2de 3332aa4 a1ca2de 3332aa4 a1ca2de 936d161 a1ca2de 936d161 a1ca2de eb50f40 |
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
import re
import copy
import random
import gradio as gr
from gradio_client import Client
from pathlib import Path
from modules import (
ImageMaker, MusicMaker, merge_video
)
from modules.llms import get_llm_factory
from interfaces import utils
from pingpong import PingPong
from pingpong.context import CtxLastWindowStrategy
img_maker = ImageMaker('https://huggingface.co/jphan32/Zero2Story/landscapeAnimePro_v20Inspiration.safetensors',
vae="https://huggingface.co/jphan32/Zero2Story/cute20vae.safetensors")
bgm_maker = MusicMaker(model_size='small', output_format='mp3')
video_gen_client_url = None # e.g. "https://0447df3cf5f7c49c46.gradio.live"
async def update_story_gen(
cursors, cur_cursor_idx,
genre, place, mood,
main_char_name, main_char_age, main_char_personality, main_char_job,
side_char_enable1, side_char_name1, side_char_age1, side_char_personality1, side_char_job1,
side_char_enable2, side_char_name2, side_char_age2, side_char_personality2, side_char_job2,
side_char_enable3, side_char_name3, side_char_age3, side_char_personality3, side_char_job3,
):
if len(cursors) == 1:
return await first_story_gen(
cursors,
genre, place, mood,
main_char_name, main_char_age, main_char_personality, main_char_job,
side_char_enable1, side_char_name1, side_char_age1, side_char_personality1, side_char_job1,
side_char_enable2, side_char_name2, side_char_age2, side_char_personality2, side_char_job2,
side_char_enable3, side_char_name3, side_char_age3, side_char_personality3, side_char_job3,
cur_cursor_idx=cur_cursor_idx
)
else:
return await next_story_gen(
cursors,
None,
genre, place, mood,
main_char_name, main_char_age, main_char_personality, main_char_job,
side_char_enable1, side_char_name1, side_char_age1, side_char_personality1, side_char_job1,
side_char_enable2, side_char_name2, side_char_age2, side_char_personality2, side_char_job2,
side_char_enable3, side_char_name3, side_char_age3, side_char_personality3, side_char_job3,
cur_cursor_idx=cur_cursor_idx
)
async def next_story_gen(
cursors,
action,
genre, place, mood,
main_char_name, main_char_age, main_char_personality, main_char_job,
side_char_enable1, side_char_name1, side_char_age1, side_char_personality1, side_char_job1,
side_char_enable2, side_char_name2, side_char_age2, side_char_personality2, side_char_job2,
side_char_enable3, side_char_name3, side_char_age3, side_char_personality3, side_char_job3,
cur_cursor_idx=None,
llm_type="PaLM"
):
factory = get_llm_factory(llm_type)
prompts = factory.create_prompt_manager().prompts
llm_service = factory.create_llm_service()
stories = ""
cur_side_chars = 1
action = cursors[cur_cursor_idx]["action"] if cur_cursor_idx is not None else action
end_idx = len(cursors) if cur_cursor_idx is None else len(cursors)-1
for cursor in cursors[:end_idx]:
stories = stories + cursor["story"]
side_char_prompt = utils.add_side_character(
[side_char_enable1, side_char_enable2, side_char_enable3],
[side_char_name1, side_char_name2, side_char_name3],
[side_char_job1, side_char_job2, side_char_job3],
[side_char_age1, side_char_age2, side_char_age3],
[side_char_personality1, side_char_personality2, side_char_personality3],
)
prompt = prompts['story_gen']['next_story_gen'].format(
genre=genre, place=place, mood=mood,
main_char_name=main_char_name,
main_char_job=main_char_job,
main_char_age=main_char_age,
main_char_personality=main_char_personality,
side_char_placeholder=side_char_prompt,
stories=stories, action=action,
)
print(f"generated prompt:\n{prompt}")
parameters = llm_service.make_params(mode="text", temperature=1.0, top_k=40, top_p=0.9, max_output_tokens=4096)
try:
response_json = await utils.retry_until_valid_json(prompt, parameters=parameters)
except Exception as e:
print(e)
raise gr.Error(e)
story = response_json["paragraphs"]
if isinstance(story, list):
story = "\n\n".join(story)
if cur_cursor_idx is None:
cursors.append({
"title": "",
"story": story,
"action": action
})
else:
cursors[cur_cursor_idx]["story"] = story
cursors[cur_cursor_idx]["action"] = action
return (
cursors, len(cursors)-1,
story,
gr.update(
maximum=len(cursors), value=len(cursors),
label=f"{len(cursors)} out of {len(cursors)} stories",
visible=True, interactive=True
),
gr.update(interactive=True),
gr.update(interactive=True),
gr.update(value=None, visible=False, interactive=True),
gr.update(value=None, visible=False, interactive=True),
gr.update(value=None, visible=False, interactive=True),
)
async def actions_gen(
cursors,
genre, place, mood,
main_char_name, main_char_age, main_char_personality, main_char_job,
side_char_enable1, side_char_name1, side_char_age1, side_char_personality1, side_char_job1,
side_char_enable2, side_char_name2, side_char_age2, side_char_personality2, side_char_job2,
side_char_enable3, side_char_name3, side_char_age3, side_char_personality3, side_char_job3,
cur_cursor_idx=None,
llm_type="PaLM"
):
factory = get_llm_factory(llm_type)
prompts = factory.create_prompt_manager().prompts
llm_service = factory.create_llm_service()
stories = ""
cur_side_chars = 1
end_idx = len(cursors) if cur_cursor_idx is None else len(cursors)-1
for cursor in cursors[:end_idx]:
stories = stories + cursor["story"]
summary_prompt = prompts['story_gen']['summarize'].format(stories=stories)
print(f"generated prompt:\n{summary_prompt}")
parameters = llm_service.make_params(mode="text", temperature=1.0, top_k=40, top_p=1.0, max_output_tokens=4096)
try:
_, summary = await llm_service.gen_text(summary_prompt, mode="text", parameters=parameters)
except Exception as e:
print(e)
raise gr.Error(e)
side_char_prompt = utils.add_side_character(
[side_char_enable1, side_char_enable2, side_char_enable3],
[side_char_name1, side_char_name2, side_char_name3],
[side_char_job1, side_char_job2, side_char_job3],
[side_char_age1, side_char_age2, side_char_age3],
[side_char_personality1, side_char_personality2, side_char_personality3],
)
prompt = prompts['story_gen']['actions_gen'].format(
genre=genre, place=place, mood=mood,
main_char_name=main_char_name,
main_char_job=main_char_job,
main_char_age=main_char_age,
main_char_personality=main_char_personality,
side_char_placeholder=side_char_prompt,
summary=summary,
)
print(f"generated prompt:\n{prompt}")
parameters = llm_service.make_params(mode="text", temperature=1.0, top_k=40, top_p=1.0, max_output_tokens=4096)
try:
response_json = await utils.retry_until_valid_json(prompt, parameters=parameters)
except Exception as e:
print(e)
raise gr.Error(e)
actions = response_json["options"]
random_actions = random.sample(actions, 3)
return (
gr.update(value=random_actions[0], interactive=True),
gr.update(value=random_actions[1], interactive=True),
gr.update(value=random_actions[2], interactive=True),
" "
)
async def first_story_gen(
cursors,
genre, place, mood,
main_char_name, main_char_age, main_char_personality, main_char_job,
side_char_enable1, side_char_name1, side_char_age1, side_char_personality1, side_char_job1,
side_char_enable2, side_char_name2, side_char_age2, side_char_personality2, side_char_job2,
side_char_enable3, side_char_name3, side_char_age3, side_char_personality3, side_char_job3,
cur_cursor_idx=None,
llm_type="PaLM"
):
factory = get_llm_factory(llm_type)
prompts = factory.create_prompt_manager().prompts
llm_service = factory.create_llm_service()
cur_side_chars = 1
side_char_prompt = utils.add_side_character(
[side_char_enable1, side_char_enable2, side_char_enable3],
[side_char_name1, side_char_name2, side_char_name3],
[side_char_job1, side_char_job2, side_char_job3],
[side_char_age1, side_char_age2, side_char_age3],
[side_char_personality1, side_char_personality2, side_char_personality3],
)
prompt = prompts['story_gen']['first_story_gen'].format(
genre=genre, place=place, mood=mood,
main_char_name=main_char_name,
main_char_job=main_char_job,
main_char_age=main_char_age,
main_char_personality=main_char_personality,
side_char_placeholder=side_char_prompt,
)
print(f"generated prompt:\n{prompt}")
parameters = llm_service.make_params(mode="text", temperature=1.0, top_k=40, top_p=1.0, max_output_tokens=4096)
try:
response_json = await utils.retry_until_valid_json(prompt, parameters=parameters)
except Exception as e:
print(e)
raise gr.Error(e)
story = response_json["paragraphs"]
if isinstance(story, list):
story = "\n\n".join(story)
if cur_cursor_idx is None:
cursors.append({
"title": "",
"story": story
})
else:
cursors[cur_cursor_idx]["story"] = story
return (
cursors, len(cursors)-1,
story,
gr.update(
maximum=len(cursors), value=len(cursors),
label=f"{len(cursors)} out of {len(cursors)} stories",
visible=False if len(cursors) == 1 else True, interactive=True
),
gr.update(interactive=True),
gr.update(interactive=True),
gr.update(value=None, visible=False, interactive=True),
gr.update(value=None, visible=False, interactive=True),
gr.update(value=None, visible=False, interactive=True),
)
def video_gen(
image, audio, title, cursors, cur_cursor, use_ffmpeg=True
):
if use_ffmpeg:
output_filename = merge_video(image, audio, story_title="")
if not use_ffmpeg or not output_filename:
client = Client(video_gen_client_url)
result = client.predict(
"",
audio,
image,
f"{utils.id_generator()}.mp4",
api_name="/predict"
)
output_filename = result[0]
cursors[cur_cursor]["video"] = output_filename
return (
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=True, value=output_filename),
cursors,
" "
)
def image_gen(
genre, place, mood, title, story_content, cursors, cur_cursor, llm_type="PaLM"
):
# generate prompts for background image with LLM
for _ in range(3):
try:
prompt, neg_prompt = img_maker.generate_background_prompts(genre, place, mood, title, "", story_content, llm_type)
print(f"Image Prompt: {prompt}")
print(f"Negative Prompt: {neg_prompt}")
break
except Exception as e:
print(e)
raise gr.Error(e)
if not prompt:
raise ValueError("Failed to generate prompts for background image.")
# generate image
try:
img_filename = img_maker.text2image(prompt, neg_prompt=neg_prompt, ratio='16:9', cfg=6.5)
except ValueError as e:
print(e)
img_filename = str(Path('.') / 'assets' / 'nsfw_warning_wide.png')
cursors[cur_cursor]["img"] = img_filename
return (
gr.update(visible=True, value=img_filename),
cursors,
" "
)
def audio_gen(
genre, place, mood, title, story_content, cursors, cur_cursor, llm_type="PaLM"
):
# generate prompt for background music with LLM
for _ in range(3):
try:
prompt = bgm_maker.generate_prompt(genre, place, mood, title, "", story_content, llm_type)
print(f"Music Prompt: {prompt}")
break
except Exception as e:
print(e)
raise gr.Error(e)
if not prompt:
raise ValueError("Failed to generate prompt for background music.")
# generate music
bgm_filename = bgm_maker.text2music(prompt, length=60)
cursors[cur_cursor]["audio"] = bgm_filename
return (
gr.update(visible=True, value=bgm_filename),
cursors,
" "
)
def move_story_cursor(moved_cursor, cursors):
cursor_content = cursors[moved_cursor-1]
max_cursor = len(cursors)
action_btn = (
gr.update(interactive=False),
gr.update(interactive=False),
gr.update(interactive=False)
)
if moved_cursor == max_cursor:
action_btn = (
gr.update(interactive=True),
gr.update(interactive=True),
gr.update(interactive=True)
)
if "video" in cursor_content:
outputs = (
moved_cursor-1,
gr.update(label=f"{moved_cursor} out of {len(cursors)} chapters"),
cursor_content["story"],
gr.update(value=None, visible=False),
gr.update(value=None, visible=False),
gr.update(value=cursor_content["video"], visible=True),
)
else:
image_container = gr.update(value=None, visible=False)
audio_container = gr.update(value=None, visible=False)
if "img" in cursor_content:
image_container = gr.update(value=cursor_content["img"], visible=True)
if "audio" in cursor_content:
audio_container = gr.update(value=cursor_content["audio"], visible=True)
outputs = (
moved_cursor-1,
gr.update(label=f"{moved_cursor} out of {len(cursors)} stories"),
cursor_content["story"],
image_container,
audio_container,
gr.update(value=None, visible=False),
)
return outputs + action_btn
def update_story_content(story_content, cursors, cur_cursor):
cursors[cur_cursor]["story"] = story_content
return cursors
def disable_btns():
return (
gr.update(interactive=False), # image_gen_btn
gr.update(interactive=False), # audio_gen_btn
gr.update(interactive=False), # img_audio_combine_btn
gr.update(interactive=False), # regen_actions_btn
gr.update(interactive=False), # regen_story_btn
gr.update(interactive=False), # custom_prompt_txt
gr.update(interactive=False), # action_btn1
gr.update(interactive=False), # action_btn2
gr.update(interactive=False), # action_btn3
gr.update(interactive=False), # custom_action_txt
gr.update(interactive=False), # restart_from_story_generation_btn
gr.update(interactive=False), # story_writing_done_btn
)
def enable_btns(story_image, story_audio):
video_gen_btn_state = gr.update(interactive=False)
if story_image is not None and \
story_audio is not None:
video_gen_btn_state = gr.update(interactive=True)
return (
gr.update(interactive=True), # image_gen_btn
gr.update(interactive=True), # audio_gen_btn
video_gen_btn_state, # img_audio_combine_btn
gr.update(interactive=True), # regen_actions_btn
gr.update(interactive=True), # regen_story_btn
gr.update(interactive=True), # custom_prompt_txt
gr.update(interactive=True), # action_btn1
gr.update(interactive=True), # action_btn2
gr.update(interactive=True), # action_btn3
gr.update(interactive=True), # custom_action_txt
gr.update(interactive=True), # restart_from_story_generation_btn
gr.update(interactive=True), # story_writing_done_btn
) |