Spaces:
Runtime error
Runtime error
File size: 24,812 Bytes
2e5e07d |
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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
import openai
import re
import ast
# Enter your openai key here
# Allow multiple keys to be filled in to prevent the number of visits from being restricted
openai_key = [""]
global_key_num = 0
def smart_openai_key():
global global_key_num
global openai_key
openai.api_key = openai_key[global_key_num]
global_key_num += 1
global_key_num %= len(openai_key)
def json_completion(prompt):
try_time = 3
for i in range(try_time):
try:
smart_openai_key()
completions = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
{"role": "assistant", "content": "["}
]
)
break
except:
print("key error: ", openai.api_key)
message = completions['choices'][0]['message']['content']
return message
def ExtractProtagonist(story, file_path):
ask = f"""The following is a story enclosed in three single quotes '''{story}''', please help me summarize all the main protagonists and places that appear in the story.""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: id, name, description.""" \
f"""You must answer like the content in following three single quotes:""" \
f"""'''[
{{
"id": 1,
"name": "Lincoln",
"description": "(the physical characteristics description of Lincoln)",
}},
{{
"id": 2,
"name": "Everest"
"description": "(the physical characteristics description of Everest)",
}}
]'''""" \
f"""The descriptions of the protagonists should adhere to the following guidelines:\n""" \
f"""1.The description should be as simple as possible, as long as it doesn't conflict with the story\n""" \
f"""2.Do not include another thing in the description of one thing\n""" \
f"""3.Most important: Only the physical characteristics of the character or place need to be described in detail, such as color and class label, no mood description is required, etc. """
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
protagonists_places_dict = {}
protagonists_places = ast.literal_eval(answer)
for protagonist_place in protagonists_places:
protagonists_places_dict[protagonist_place["name"]] = protagonist_place["description"]
return protagonists_places_dict
def ExtractAProtagonist(story, file_path):
ask = f"""The following is a story enclosed in three single quotes '''{story}''', please help me summarize a main protagonist that appear in the story.""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: id, name, description.""" \
f"""You must answer like the content in following three single quotes:""" \
f"""'''[
{{
"id": 1,
"name": "Lincoln",
"description": "(the physical characteristics description of Lincoln)",
}}
]'''""" \
f"""The descriptions of the protagonist should adhere to the following guidelines:\n""" \
f"""1.The description should be as simple as possible, as long as it doesn't conflict with the story\n""" \
f"""2.Most important: Only the physical characteristics of the character need to be described in detail, such as color and class label, no mood description is required, etc. """
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
protagonists_places_dict = {}
protagonists_places = ast.literal_eval(answer)
for protagonist_place in protagonists_places:
protagonists_places_dict[protagonist_place["name"]] = protagonist_place["description"]
return protagonists_places_dict
def protagonist_place_reference(video_list, character_places):
new_video_list = []
num = 1
for video in video_list:
prompt = str(num) + ". " + video
new_video_list.append(prompt)
num += 1
key_list = []
i = 1
for key, value in character_places.items():
key_list.append(str(i) + ". " + key)
i += 1
ask = f"""I would like to make a video. Here are this video script in the following three single quotes '''{new_video_list}''', """ \
f"""Here are some characters and places in the following three single quotes '''{key_list}''', """ \
f"""Please help me identify the characters or places in the list where each segment of the video script appears""" \
f"""You can only choose characters and places that match exactly, and you can't choose even the slightest doubt.""" \
f"""Just answer me the serial number(2 selections are possible, but no more, pick out what you think is most likely. If you select less than 2, you can fill it with 0.)""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: video segment id, character/place id.""" \
f"""You must answer like the content in following three single quotes:""" \
f"""'''[
{{
"video segment id": 1,
"character/place id": [1, 0],
}},
{{
"video segment id": 2,
"character/place id": [1, 2],
}},
{{
"video segment id": 3,
"character/place id": [0, 0],
}}
]'''"""
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open("MakeVideo/protagonist_place_reference.txt", "w")
f.write(answer)
f.close()
print(answer)
reference_list = []
protagonists_places_reference = ast.literal_eval(answer)
for protagonist_place_reference in protagonists_places_reference:
reference_list.append(protagonist_place_reference["character/place id"])
return reference_list
def protagonist_place_reference1(video_list, character_places, file_path):
new_video_list = []
num = 1
for video in video_list:
prompt = str(num) + ". " + video
new_video_list.append(prompt)
num += 1
key_list = []
i = 1
for key, value in character_places.items():
key_list.append(str(i) + ". " + key)
i += 1
ask = f"""I would like to make a video. Here are this video script in the following three single quotes '''{new_video_list}''', """ \
f"""Here are some characters and places in the following three single quotes '''{key_list}''', """ \
f"""Please help me identify the characters or places in the list where each segment of the video script appears""" \
f"""You can only choose characters and places that match exactly, and you can't choose even the slightest doubt.""" \
f"""Just answer me the serial number(1 selection is possible, but no more, pick out what you think is most likely. If you select less than 1, you can fill it with 0.)""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: video segment id, character/place id.""" \
f"""You must answer like the content in following three single quotes:""" \
f"""'''[
{{
"video segment id": 1,
"character/place id": [1],
}},
{{
"video segment id": 2,
"character/place id": [0],
}},
{{
"video segment id": 3,
"character/place id": [2],
}}
]'''"""
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
# print(answer)
reference_list = []
protagonists_places_reference = ast.literal_eval(answer)
for i, prompt in enumerate(video_list):
prompt = prompt.lower()
for key, value in character_places.items():
if key.lower() in prompt:
protagonists_places_reference[i]["character/place id"] = [1]
for protagonist_place_reference in protagonists_places_reference:
reference_list.append(protagonist_place_reference["character/place id"])
return reference_list
def split_story(story, file_path):
ask = f"""The following is a story enclosed in three single quotes '''{story}''' and I would like to request your assistance in """ \
f"""writing a script for a video based on this story. Provide the script in JSON format(do not answer anything else) with the following keys: video fragment id, video fragment description.""" \
f"""You must answer like the content in following three single quotes:\n""" \
f"""'''[
{{
"video fragment id": 1,
"video fragment description": "(the description, describe the characters, actions, and backgrounds in the video fragment)",
}},
{{
"video fragment id": 2,
"video fragment description": "(the description, describe the characters, actions, and backgrounds in the video fragment)",
}}
]'''""" \
f"""The descriptions of the video segments should adhere to the following guidelines:\n""" \
f"""1.Fits the original storyline\n""" \
f"""2.All video fragment descriptions cannot conflict with each other, and the descriptions corresponding to successive fragments in the original story must have a certain continuity\n""" \
f"""3.The description only needs to describe the visual elements presented, such as the subject, action, background, etc., and do not appear useless descriptions, such as mental activities\n""" \
f"""Each description should include the subject, place, and action as much as possible.""" \
f"""Read this script carefully and don't pull down any details.\n""" \
f"""As more fragment as possible, as detail as possible!\n""" \
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
video_fragments = ast.literal_eval(answer)
video_list = []
for video_fragment in video_fragments:
video_list.append(video_fragment["video fragment description"])
return video_list
def patch_story_scripts(story, video_list, file_path):
ask = f"""The following is a story enclosed in three single quotes '''{story}'''. I want to make a video according to this story, this is my video production script in the following three single quotes '''{video_list}''', a paragraph in the script corresponds to a clip """ \
f"""of the video. However, there may be some plots in the story missing, such as important plot missing, or transitions between pictures, please check and complete it for me. """ \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: video fragment id, video fragment description.""" \
f"""You must must must answer like the content in following three single quotes:\n""" \
f"""'''[
{{
"video fragment id": 1,
"video fragment description": "(the description)",
}},
{{
"video fragment id": 2,
"video fragment description": "(the description)",
}},
{{
"video fragment id": 3,
"video fragment description": "(the description)",
}}
]'''""" \
f"""Remember to make sure that the description of each video clip is not long, no more than fifteen words, but there can be so many video clips.\n""" \
f"""Each description should include the subject, place, and action as much as possible.""" \
f"""As more fragment as possible, as detail as possible!\n""" \
f"""Read this script carefully and don't pull down any details.\n"""
# f"""Very important!!!: avoid character-to-character interactions and character-to-object interactions in descriptions."""
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
video_fragments = ast.literal_eval(answer)
video_list = []
for video_fragment in video_fragments:
video_list.append(video_fragment["video fragment description"])
return video_list
def refine_story_scripts(video_list, file_path):
ask = f"""I want to make a video, this is my video production script in the following three single quotes '''{video_list}''', a paragraph in the script corresponds to a clip """ \
f"""of the video, But the description of some video clips is too complicated, please help me analyze and rewrite a video script, split each description into at least three short descriptions and as more as possible. """ \
f"""For example, if there are one paragraphs in the script I gave you, then you should split it into fifteen paragraphs.""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: video fragment id, video fragment description.""" \
f"""You must must must answer like the content in following three single quotes:\n""" \
f"""'''[
{{
"video fragment id": 1,
"video fragment description": "(the description)",
}},
{{
"video fragment id": 2,
"video fragment description": "(the description)",
}},
{{
"video fragment id": 3,
"video fragment description": "(the description)",
}}
]'''""" \
f"""Remember to make sure that the description of each video clip is not long, no more than ten words, but there can be so many video clips.\n""" \
f"""Most important thing: Read this script carefully and don't pull down any details.\n""" \
f"""Ensure that all description statements are as natural and syntactically correct as possible.\n""" \
f"""Most important: Try to have only one character in the description and avoid complex actions in video fragment description, such as: loaded in, fight, etc.\n"""
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("\n")
answer = answer.strip("[")
answer = answer.strip("\n")
answer = answer.strip("]")
answer = answer.strip("\n")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
video_fragments = ast.literal_eval(answer)
video_list = []
for video_fragment in video_fragments:
video_list.append(video_fragment["video fragment description"])
return video_list
def time_scripts(video_list, file_path):
try_times = 3
for i in range(try_times):
try:
new_video_list = []
num = 1
for video in video_list:
prompt = str(num) + ". " + video
new_video_list.append(prompt)
num += 1
ask = f"""I want to make a video, this is my video production script in the following three single quotes '''{new_video_list}''', a paragraph in the script corresponds to a clip """ \
f"""of the video, Now that you know that 16-frame videos have a length of 2 seconds, please help me plan how much time it will take for each video clip to fully interpret the meaning of the script.""" \
f"""Each clip can only be 10 seconds maximum.""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: video fragment id, time.""" \
f"""You must answer like the content in following three single quotes:\n""" \
f"""'''[
{{
"video fragment id": 1,
"time": 3,
}},
{{
"video fragment id": 2,
"time": 9,
}},
{{
"video fragment id": 3,
"time": 7,
}},
{{
"video fragment id": 4,
"time": 2,
}},
]'''""" \
f"""Remember that time must be less than 10."""
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
time_scripts = ast.literal_eval(answer)
time_list = []
for time_script in time_scripts:
time = time_script["time"]
if time > 10:
time = 10
time_list.append(time)
assert len(time_list) == len(video_list)
return time_list
except:
continue
assert len(time_list) == len(video_list)
return time_list
def translate_video_script(video_list, file_path):
try_times = 5
for i in range(try_times):
try:
ask = f"""I want to make a video, this is my video production script in the following three single quotes '''{video_list}''', """ \
f"""please help me to translate every video fragment description into Chinese.""" \
f"""Provide me the answer in JSON format(do not answer anything else) with the following keys: ๅบๅท, ๆ่ฟฐ.""" \
f"""You must must must answer like the content in following three single quotes:\n""" \
f"""'''[
{{
"ๅบๅท": 1,
"ๆ่ฟฐ": "(่ง้ข็ๆฎตๆ่ฟฐ)",
}},
{{
"ๅบๅท": 2,
"ๆ่ฟฐ": "(่ง้ข็ๆฎตๆ่ฟฐ)",
}}
]'''"""
answer = json_completion(ask)
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = answer.strip("'")
answer = answer.strip("[")
answer = answer.strip("]")
answer = "[\n" + answer + "\n]"
f = open(file_path, "w")
f.write(answer)
f.close()
video_fragments = ast.literal_eval(answer)
zh_video_list = []
for video_fragment in video_fragments:
zh_video_list.append(video_fragment["ๆ่ฟฐ"])
assert len(video_list) == len(zh_video_list)
return zh_video_list
except:
continue
assert len(video_list) == len(zh_video_list)
return zh_video_list
def readscript(script_file_path):
with open(script_file_path, "r", encoding='utf-8') as f:
script = f.read()
video_fragments = ast.literal_eval(script)
video_list = []
for video_fragment in video_fragments:
video_list.append(video_fragment["video fragment description"])
return video_list
def readzhscript(zh_file_path):
with open(zh_file_path, "r", encoding='utf-8') as f:
script = f.read()
video_fragments = ast.literal_eval(script)
video_list = []
for video_fragment in video_fragments:
video_list.append(video_fragment["ๆ่ฟฐ"])
return video_list
def readtimescript(time_file_path):
with open(time_file_path, "r", encoding='utf-8') as f:
time_scripts = f.read()
time_scripts = ast.literal_eval(time_scripts)
time_list = []
for time_script in time_scripts:
frames = time_script["time"]
time_list.append(frames)
return time_list
def readprotagonistscript(protagonist_file_path):
with open(protagonist_file_path, "r", encoding='utf-8') as f:
protagonist_scripts = f.read()
protagonist_scripts = ast.literal_eval(protagonist_scripts)
protagonists_places_dict = {}
for protagonist_script in protagonist_scripts:
protagonists_places_dict[protagonist_script["name"]] = protagonist_script["description"]
return protagonists_places_dict
def readreferencescript(video_list, character_places, reference_file_path):
new_video_list = []
num = 1
for video in video_list:
prompt = str(num) + ". " + video
new_video_list.append(prompt)
num += 1
key_list = []
i = 1
for key, value in character_places.items():
key_list.append(str(i) + ". " + key)
with open(reference_file_path, "r", encoding='utf-8') as f:
reference_file = f.read()
reference_list = []
protagonists_places_reference = ast.literal_eval(reference_file)
for i, prompt in enumerate(video_list):
prompt = prompt.lower()
for j, key in enumerate(key_list):
if key.lower() in prompt:
protagonists_places_reference[i]["character/place id"] = [j + 1]
for protagonist_place_reference in protagonists_places_reference:
reference_list.append(protagonist_place_reference["character/place id"])
return reference_list
|