-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstory_generator.py
1158 lines (958 loc) · 52.2 KB
/
story_generator.py
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import uuid
import hashlib
import shutil
import requests
import io
from pydub import AudioSegment
import threading
import queue
import time
import random
from PIL import Image
import cohere
from cohere.core.request_options import RequestOptions
from pickle_memory import save_memory, load_memory
from dotenv import load_dotenv
load_dotenv()
import sound_selector
import pose_selector
import voice_selector
from comfyuiclient import ComfyUIClient
class PromptGenerator:
def __init__(self, client):
self.client = client
def get_claude(self, system, text):
key = f"get_claude: system={system} , text={text}"
cached = load_memory(key)
if cached is not None:
return cached
key2 = f"response: {system} {text}"
response = load_memory(key2)
if not response:
response = self.client.chat(
chat_history=[
{"role": "SYSTEM", "message": str(system)},
],
message=str(text),
model="command-r-plus"
).text
save_memory(key2, response)
return response
def get_background(self, text):
prompt_background = """
# Order
You are a game CG background designer.
Based on the theme the customer wants, we show them what will be reflected in the background image and explain what to draw. We will supplement what should be in the background, or what will fit perfectly.
You don't need your own explanations, explanations, or emotional poetics. Please explain exactly what you are drawing and what you are drawing about the background image.
The image you create is a background image, so it should never feature a person as the main character. However, it is permissible for people to exist as part of the background, such as in cityscapes or intersections.
Please output in English."""
return self.get_claude(prompt_background, text)
def get_charactor(self, text):
prompt_cast = """You are a character designer.
You can describe the character's appearance in detail according to your desired theme.
You choose your character's posture, facial expressions, character settings, and character appearance, but don't explain why or explain them. When doing so, please focus on explaining only the character design and omitting emotional expressions.
Do not express the character's age using numbers; instead, use English words that allow you to guess the approximate age, such as boy, girl, young man, beauty, middle-aged, elderly, senile, age unknown.
Don't describe things your character owns. Please explain the details of the character design (clothes, accessories, hair color, hairstyle, presence of bangs, bangs style, eye color, chest size for female characters).
Your thoughts and poetic expressions are not required. All output must be in English.
Please output in English."""
return self.get_claude(prompt_cast, text)
def get_face(self, text):
prompt_face = """You are a character designer.
From the specified text, find out the characteristics of the person's face, such as the color and shape of the eyes, the presence or absence of bangs and their style, the color of the hair, the presence or absence of makeup, the color of makeup, if any, the presence or absence of glasses, etc.
Write down only information about facial features such as color and shape in an easy-to-understand manner.
Your opinions, explanations, and explanations are not necessary. Please output as English sentences without using bullet points.
Please output in English."""
return self.get_claude(prompt_face, text)
def get_title(self, text):
prompt_title = """
# Order
You are a game designer.
You design an attractive game title screen according to user requests.
The designs you describe are all done in English words and explain the character's posture, pose, background, etc. Please do not explain text, logos, or captions at this time. Please do not think about the logo or text, as the logo and text will be designed by a different designer.
In the design you describe, first explain the visuals and posture of each character. After that, please explain the background, effects, etc.
Use the user's input language for input only, and be sure to output all your explanations in English.
Your thoughts, thoughts, and opinions are not needed.
"""
return self.get_claude(prompt_title, text)
class DataSelector:
def __init__(self, client):
self.client = client
def bgm(self, text:str) -> str:
key = f"sound_selector result : {text}"
result = load_memory(key)
if result:
return result
with open("bgm_data.json") as f:
all_data = json.load(f)
bgm_select_prompt=f"""
# Order
You are a pro when it comes to music selection.
You can provide the hash information of the music file that is closest to the situation the user is looking for.
Since the song file name is a hash value, the user only needs the hash information to play the song.
Please output only the hash value of the selected song. Your comments and explanations are not necessary.
Be sure to output from \"hash:...\".
# Song list data
```json
{json.dumps(all_data, indent=4, ensure_ascii=False)}
```
""".strip()
key2 = f"response: {text} {bgm_select_prompt}"
response = load_memory(key2)
if not result:
response = self.client.chat(
chat_history=[
{"role": "SYSTEM", "message": bgm_select_prompt},
],
message=text,
model="command-r-plus"
).text
save_memory(key2, response)
result = response.replace('hash:', '').strip()
save_memory(key, result)
return result
def voice(self, text:str, exclude_voice_list=None, nocache=False) -> str:
key = f"voice_selector: {text} : {exclude_voice_list}"
result = load_memory(key)
if nocache:
result = None
if result:
return result
voice_names = []
voice_list = []
with open('voice_list.txt', 'r', encoding='utf8') as f:
for voice in f.read().splitlines():
voice_csv = voice.split(',')
voice_name = voice_csv[0].strip()
voice_tags = voice_csv[1].strip().split('|')
voice_names.append(voice_name)
if exclude_voice_list and voice_name in exclude_voice_list:
continue
voice_list.append({'name': voice_name, 'tags': voice_tags})
if not voice_list:
raise Exception("No voice list available.")
voice_select_prompt = f"""
# Order
You are a character designer.
Based on the theme requested by the customer, you select one voice list from the character setting data, considering the best voice for the character.
Please do not output your thoughts or supplementary information, just tell us the voice name from the voice list.
Be sure to start the output with \"Voice name:...\".
# Voice list
```json
{json.dumps(voice_list, indent=4, ensure_ascii=False)}
```
""".strip()
key2 = f"response: {text} {voice_select_prompt}"
response = load_memory(key2)
if nocache:
response = None
if not response:
for _ in range(3):
response = self.client.chat(
chat_history=[
{"role": "SYSTEM", "message": voice_select_prompt},
],
message=text,
model="command-r-plus"
).text.replace('Voice name:', '').strip()
if response in voice_names:
break
else:
raise Exception("Failed to select voice.")
save_memory(key2, response)
save_memory(key, response)
return response
def expression(self, expression):
response = load_memory(expression)
if response is not None:
return response
if not response:
for _ in range(3):
response = self.client.chat(
chat_history=[
{"role": "SYSTEM", "message": "You are a game character designer.\nYou can answer various character expressions according to user requests.\n\nThe following six types of images are available as game materials.\n[\"sad\", \"surprise\", \"evil smile\", \"shy\", \"angly\", \"smile\"]\n\nPlease select the one that is closest to the facial expression/emotion specified by the user and output only the answer. Your comments and explanations are not necessary."},
],
message=expression,
model="command-r-plus"
).text.strip('"').strip().lower().replace('evil smile', 'evil')
if response in ["sad", "surprise", "evil", "shy", "angly", "smile"]:
break
save_memory(expression, response)
return response
class VoiceGenerator:
def __init__(self, server = 'http://localhost:5000'):
self.server = server
self.reflesh_voice_model()
def get_voice_model_id_by_name(self, name):
print(f"Getting model id by name: {self.server}, {name}")
if name is None:
raise ValueError("Model name is required")
name = name.strip()
response = requests.get(f'{self.server}/models/info')
data = response.json()
for model_id, model_info in data.items():
model_name = model_info.get('config_path').split("\\")[1]
if name == model_name:
return model_id
raise ValueError(f"Model not found: '{name}'")
def reflesh_voice_model(self):
url = f'{self.server}/models/refresh'
headers = {'accept': 'application/json'}
requests.post(url, headers=headers)
def text2mp3(self, name, text) -> io.BytesIO:
try:
model_id = self.get_voice_model_id_by_name(name)
url = f"{self.server}/voice"
params = {
'text': text,
'model_id': model_id,
'speaker_id': '0',
'sdp_ratio': '0.2',
'noise': '0.6',
'noisew': '0.8',
'length': '1',
'language': 'JP',
'auto_split': 'true',
'split_interval': '0.5',
'assist_text_weight': '1',
'style': 'Neutral',
'style_weight': '10'
}
headers = {'accept': 'audio/wav'}
response = requests.post(url, params=params, headers=headers)
if response.status_code == 200:
sound = AudioSegment.from_wav(io.BytesIO(response.content))
mp3_data = io.BytesIO()
sound.export(mp3_data, format="mp3")
mp3_data.seek(0)
return mp3_data
else:
print(f"Request failed with status code: {response.status_code}")
except requests.exceptions.Timeout:
print(f"Request to {self.server} timed out. Retrying...")
except requests.exceptions.RequestException as e:
print(f"Request to {self.server} failed: {e}. Retrying...")
def save_voice(self, filename, name, text):
print(f"Saving voice to {filename}, name: {name}, text: {text}")
mp3_data = self.text2mp3(name, text)
with open(filename, "wb") as f:
f.write(mp3_data.read())
class CharactorGenerator:
def __init__(self, client):
self.client = client
def generator(self, text, file_path=None, base_name=None):
promptGenerator = PromptGenerator(self.client)
cast = promptGenerator.get_charactor(text)
face = promptGenerator.get_face(cast)
comfyui_client = ComfyUIClient("127.0.0.1:8188", "text2cast_api.json")
comfyui_client.connect()
comfyui_client.set_data(key='KSampler', seed=random.randint(0, 1000000))
comfyui_client.set_data(key='Input Charactor Prompt', text=cast)
comfyui_client.set_data(key='Input Face Prompt', text=face)
used_poses = load_memory("used_poses") or []
pose_file = random.choice(pose_selector.get_pose_image(cast, used_poses))
used_poses.append(pose_file)
save_memory("used_poses", used_poses)
comfyui_client.set_data(key='Load Image', image=Image.open(pose_file))
output_dir_final = ""
if file_path is None:
output_dir = "output/charactor"
output_dir_final = output_dir
countup = 0
while os.path.exists(output_dir_final):
output_dir_final = f"{output_dir}_{countup}"
countup += 1
os.makedirs(output_dir_final, exist_ok=True)
for key, image in comfyui_client.generate(["Result Smile", "Result Angly", "Result Shy", "Result Evil", "Result Surprise", "Result Sad"]).items():
expression = key.replace("Result ", "").lower()
if file_path is not None:
image.save(f"{file_path}/{base_name}_{expression}.png")
else:
image.save(f"{output_dir_final}/{key}.png")
comfyui_client.close()
class StoryNode:
def __init__(self, client, parent=None, user_choise:str=None, story_prompt=None):
self.client = client
self.parent = parent
self.main_story = None
self.previous_story = None
self.synopsis = None
self.uuid = None
if parent:
self.main_story = self.parent.main_story
self.previous_story = self.parent.story
self.uuid = hashlib.sha256(str(self.parent.uuid + user_choise).encode()).hexdigest()
else:
self.main_story = self.create_main_story(story_prompt)
title = self.main_story.get("title engilsh", self.main_story.get("title japanese"))
self.uuid = hashlib.sha256(title.encode()).hexdigest()
self.user_choise = user_choise
self.story = None
self.choices = []
# setting
self.settings = {"used_voices":{}}
if self.parent:
self.settings = self.parent.settings
def generate(self, elect_level=0):
old_synopsis = None
story_logs = []
if self.parent:
if self.parent.parent:
story_data = self.parent.parent.story.get("story data")
if story_data:
story_logs.extend(story_data)
story_data = self.parent.story.get("story data")
if story_data:
story_logs.extend(story_data)
if self.parent.parent:
if self.parent.parent.synopsis:
old_synopsis = self.parent.parent.synopsis
self.synopsis = self.create_synopsis(self.main_story, old_synopsis, story_logs)
self.story = self.create_story(self.main_story, self.previous_story, self.synopsis, self.user_choise, elect_level=elect_level)
self.auto_fix_tags()
#print(json.dumps(self.main_story.get('charactors'), indent=4, ensure_ascii=False))
for choice in self.story["next choices"]:
self.choices.append(StoryNode(self.client, self, choice["select"]))
def json_format(self, text:str) -> dict:
try:
return json.loads(text)
except json.decoder.JSONDecodeError:
try:
return eval(text)
except (Exception, SyntaxError):
try:
if '```json' in text:
text = text.split('```json')[1].split('```')[0]
return self.json_format(text)
except ValueError:
lines = text.split("\n")
for i, line in enumerate(lines):
line = line.strip()
if not line.startswith("\"") and line.endswith("\""):
lines[i] = f"\"{line}"
elif not line.startswith("\"") and line.endswith("\","):
lines[i] = f"\"{line}"
if line.startswith("\"") and not (line.endswith("\"") or line.endswith("\",")):
line_temp = line.strip().strip("\"")
if '\"' not in line_temp:
lines[i] = f"\"{line_temp}\","
if line.startswith("「") and line.endswith("」,"):
lines[i] = line.replace("「", "\"").replace("」", "\"").replace(":", ":")
return self.json_format("\n".join(lines))
print (f"Failed to parse JSON format: {text}")
raise ValueError("Failed to parse JSON format")
def create_main_story(self, prompt:str) -> dict:
result = load_memory(prompt)
if result:
result["user input theme"] = prompt.strip()
if 'uuid' not in result:
result["uuid"] = uuid.uuid4().hex
save_memory(prompt, result)
return result
main_story_prompt = """
# Order
You are a screenwriter. You can write characters, scenarios, and endings according to the requested theme.
The output format is JSON. Specifically, it is as follows.
# User input example
異世界探検部。古い地図を手掛かりに異世界へ旅立ち冒険する。
# Example of your output
```json
{
"user input theme": "異世界探検部。古い地図を手掛かりに異世界へ旅立ち冒険する。",
"title japanese": "異世界探検部",
"title english": "Exploration Club of Another World",
"story":"主人公は高校の探検部に所属する平凡な男子学生です。ある日、部室で見つけた古い地図を手がかりに、部員たちと共に学校の地下に眠る秘密の扉を発見します。扉の先には、不思議な異世界が広がっていました。その世界を探検するうちに、彼らは異世界の危機に巻き込まれ、世界を救うために奮闘することになります。",
"settings":[
"異世界の起源:実は、異世界は人間の夢や想像力が具現化した世界である。扉は人の心の奥深くに存在し、強い想いを持つ者だけがそこに辿り着ける。主人公たちは無意識のうちに、自分たちの心の扉を開いてしまったのだ。",
"異世界と現実世界の関係:異世界の出来事は、実は現実世界にも影響を及ぼしている。異世界の危機が深刻になればなるほど、現実世界でも異変が起きる。逆に、現実世界の人々の心が穏やかになれば、異世界も平和になる。",
"主人公たちが異世界の危機を乗り越えた後、彼らは異世界と現実世界が表裏一体であることを知る。自分たちの心と向き合い、前向きに生きることが、両方の世界を守ることにつながるのだと気づくのである。"
],
"Events that occur in parallel behind the story of this world":{
"0%": "魔王は力を貯めています。",
"20%": "魔王が力を爆発させ、現実で校舎が揺れるなどの影響が出始めます。",
"40%": "魔王の力が限界を突破して、異世界と現実世界が融合し始めます。",
"80%": "魔王の力により異世界と現実が完全に融合してしまい、両方の世界が混沌としてしまいます。",
"100%": "魔王を止める事が出来ず世界が崩壊してしまい Game Over となります。"
},
"Facility and place name":
{
"垣岡高校": "主人公たちが通う高校。探検部の部室は地下にあり、古い地図や探検用具が置かれている。",
"異世界の扉": "垣岡高校の地下に眠る、異世界へ続く扉。普段は見えないが、古い地図を手がかりにすることで見つけることができる。",
"異世界:トルゥーワールド": "主人公たちが探検する異世界。現実世界とは異なる風景や生物が存在し、不思議な力が働いている。",
"魔王の城": "異世界の中心にそびえる、魔王の住む城。魔王の力が最も強い場所であり、異世界の危機の根源となっている。",
"神秘の祠": "異世界に点在する神秘的な祠。そこには異世界の秘密が隠されており、主人公たちの冒険の鍵となる。"
},
"ending conditions":[
"True End: 主人公と仲間たちは平和を取り戻した異世界に別れを告げ、現実世界に戻ってきた彼らは、経験を胸に新たな冒険に旅立ちます。",
"Bitter Sweet End: 魔王を倒し異世界の危機は去りましたが、犠牲も大きくありました。主人公は仲間を失った悲しみを乗り越え、その思い出を胸に現実世界で生きていくことを決意します。",
"Revelation End: 異世界の探検を通して、自分の弱さと向き合い、乗り越えていく中で、真の強さを手に入れていきます。",
"Tragic End: 主人公は異世界で出会った特別な存在と深い絆を築きますが、世界の危機を前に、その存在は犠牲となってしまいます。深く心に傷を負いながらも、主人公は前を向いて生きていくことを誓います。",
"Bad End: 主人公たちは異世界の危機を解決できず、現実世界に戻ることができません。異世界の闇に取り込まれ、永遠に閉ざされてしまいます。",
"Secret End: 探検を進め、隠されたヒントを集めることで、異世界の秘密を解き明かし、誰も知らない真実へとたどり着くことができるのです。",
"Game Over: 主人公たちは異世界の危機に立ち向かいますが、その力不足を痛感し、敗北を味わいます。異世界は闇に包まれ、主人公たちはそのまま閉ざされてしまいます。",
"Dream End: 異世界での冒険の末、主人公は目を覚まします。全ては夢だったのでしょうか。"
],
"charactors":[
{"charactor name":"田中太郎", "display name":"太郎", "appearance":"18歳、男性、黒髪で黒い目、ショートヘア、迷彩のズボンに白いシャツ", "personality":"普通の男子", "status":"健康", "player":"True", "voice":"普通の男性の声"},
{"charactor name":"田中詩織", "display name":"詩織","appearance":"女の子、白いワンピース、赤いヘアピン、青い髪、ロングツインテール、赤い瞳", "personality":"正義感が強い", "Relationship with players":"顔見知り", "status":"右手を怪我している", "voice":"少女らしい透き通った声"},
{"charactor name":"岡田龍平", "display name":"龍平","appearance":"男の子、学生服、緑の髪、ショートヘア、青い瞳", "personality":"気が弱い", "Relationship with players":"初対面", "status":"元気", "voice":"声変わりした低めの男性の声"},
{"charactor name":"魔王グランディ", "display name":"魔王グランディ", "appearance":"200歳、女性、魔王、黒いロングヘア、禍々しいドレス、黒い翼、ツノが生えている", "personality":"心を病んでおり攻撃的", "Relationship with players":"初対面、敵対", "status":"力を貯めている", "voice":"低い女性の声"}
]
}
```
"""
key = f"response: {prompt} {main_story_prompt}"
response = load_memory(key)
if not result:
print("Creating main story...")
response = self.client.chat(
chat_history=[
{"role": "SYSTEM", "message": main_story_prompt},
],
message=prompt,
model="command-r-plus",
request_options = RequestOptions(timeout_in_seconds=60 * 5)
).text
save_memory(key, response)
result = self.json_format(response)
result["uuid"] = uuid.uuid4().hex
result["user input theme"] = prompt.strip()
save_memory(prompt, result)
return result
def create_story(self, main_story:dict, previous_story:dict=None, synopsis:str=None, user_choise:str=None, nocache=False, elect_level=0) -> dict:
for n in range(5):
print(f"Creating story({n}): {user_choise}")
try:
result = self._create_story(main_story, previous_story, synopsis, user_choise, nocache, elect_level)
required_keys = ["charactor status", "story data", "next choices"]
for key in required_keys:
if key not in result:
print(f"KeyError: {key}; retrying...")
nocache = True
continue
if 'add charactors' in result:
main_story["charactors"].extend(result["add charactors"])
print("Added new charactors.")
return result
except Exception as e:
print(f"Error: {e}/{type(e)}; retrying...")
nocache = True
continue
raise Exception("Failed to create story.")
def _create_story(self, main_story:dict, previous_story:dict=None, synopsis:str=None, user_choise:str=None, nocache=False, elect_level=0) -> dict:
print(f"_Creating story: {user_choise}")
key = f"create_story: {main_story}_{previous_story}_{user_choise}"
result = load_memory(key)
if nocache:
result = None
if result:
return result
main_story_prompt = f"""
# Order
Please use the following story settings to output the story data for your visual novel game in JSON format.
# Story Settings
{json.dumps(main_story, indent=4, ensure_ascii=False)}
""".strip()
if synopsis:
main_story_prompt += f"""
# Previous Synopsis
{synopsis}
"""
sex_flag = ""
if elect_level > 0:
sex_flag = """- Please create a story and choices that will lead to sexual activity with the heroine in the next choice.
"""
story_prompt = """
# Order
You are a game engine for visual novel games. You create the following stream from the input data and output it in JSON format.
- "story data": 30 or more are required.
- "next choices": 2 to 3 are required.
- If the atmosphere of the scene changes, please specify the BGM atmosphere with "change bgm".
""" + sex_flag + """
# Example of your output JSON format.
```json
{
"charactor status":[
{"charactor name":"田中太郎", "inventory":["スマートフォン", "日本地図"], "mental state"="正常", "physical state"="健康", "location":"日本、屋外、住宅街", "understanding":["異世界に魔王がいるらしい","田中詩織はかわいい"]},
{"charactor name":"田中詩織", "Relationship with player":"顔見知り、気になる", "inventory":["リュックサック","地図","コンパス","水筒","おにぎり"], "mental state"="元気", "physical state"="右手を怪我している", "location":"日本、屋外、住宅街", "understanding":["田中太郎は幼馴染"]},
{"charactor name":"岡田龍平", "Relationship with player":"初対面", "inventory":["神秘の魔石","魔封じの剣"], "mental state"="不安", "physical state"="元気", "location":"日本、屋内、岡田龍平の自宅", "understanding":["異世界には魔王がいる"]},
{"charactor name":"魔王グランディ", "Relationship with player":"初対面、全人類に敵対", "inventory":["魔王の剣"], "mental state"="怒り", "physical state"="力を貯めている", "location":"異世界、魔王城、玉座の間", "understanding":["人間は信用ならない","近いうちに人間が来る"]}
],
"add charactors":[
{"charactor name":"リディア・マッカーソン", "display name":"リディア", "appearance":"180歳、女性、エルフ、金髪ロングヘア、", "personality":"優しい", "Relationship with players":"初対面", "status":"元気", "voice":"透き通った声"}
],
"story data":[
{"mode":"change background image", "data":"日本、昼間、屋外、住宅街"},
{"mode":"play sound effect", "data":"スズメの鳴声、ちゅんちゅん"},
{"mode":"change bgm", "data":"快晴、明るい、楽しい、平和な音楽"},
{"mode":"sound effect", "data":"女性の足音、ハイヒール、コツコツ"},
{"mode":"talk", "charactor name":"田中太郎", "display name":"太郎", "talk":"こんにちは!", "expression":"Smile"},
{"mode":"show charactor", "data":"田中詩織"},
{"mode":"talk", "charactor name":"田中詩織", "display name":"詩織", "talk":"あら?こんにちは", "expression":"Shy"},
{"mode":"player characters inner voice", "data":"彼女は俺の幼馴染だ"},
{"mode":"talk", "charactor name":"田中詩織", "display name":"詩織", "talk":"あたし急いでるから", "expression":"Shy"},
{"mode":"hide charactor", "data":"田中詩織"},
{"mode":"player characters inner voice", "data":"行ってしまった……"},
{"mode":"play sound effect", "data":"コケる音。ドッシン!"},
{"mode":"show charactor", "data":"リディア・マッカーソン"},
{"mode":"talk", "charactor name":"リディア・マッカーソン", "display name":"???", "talk":"いったーい!", "expression":"Sad"},
{"mode":"talk","charactor name":"田中太郎", "display name":"主人公", "talk":"え?誰?", "expression":"Surprise"},
{"mode":"talk", "charactor name":"リディア・マッカーソン", "display name":"リディア", "talk":"わたし?わたしはリディアよ!", "expression":"Smile"},
],
"next choices":[
{"select":"エルフをデートに誘う", "info":"リディアをデートをデートに誘います。"},
{"select":"家に帰る", "info":"自宅に帰ります。"},
{"select":"UFOを見つける", "info":"UFOを探しに行きます。"}
]
}
```
"""
payload = [
{"role": "SYSTEM", "message": story_prompt},
]
if previous_story:
payload.append({"role": "USER", "message": main_story_prompt})
payload.append({"role": "CHATBOT", "message": json.dumps(previous_story, indent=4, ensure_ascii=False)})
message = user_choise
else:
message = main_story_prompt + "\n\n# Previous Story\nThe story has just begun.You need to write an introduction that draws players into the world of your work."
key2 = f"response: {payload}_{message}"
response = load_memory(key2)
if nocache:
response = None
if not response:
response = self.client.chat(
chat_history=payload,
message=str(message),
model="command-r-plus",
max_tokens=4000,
request_options = RequestOptions(timeout_in_seconds=60*5),
seed=random.randint(0, 1000000)
).text
save_memory(key2, response)
result = self.json_format(response)
save_memory(key, result)
return result
def create_synopsis(self, main_story:dict, synopsis:str=None, story_data:dict=None, nocache=False) -> str:
key = f"create_synopsis: {main_story}_{synopsis}_{story_data}"
result = load_memory(key)
if nocache:
result = None
if result:
return result
synopsis_prompt = f"""
# Order
You can summarize stories entered by users. You will focus on the main character and summarize what has changed and how it has changed, the psychological state of the characters, the tools, the promises between the characters, the new setting that has appeared and its state, etc.
"""
synopsis_data = ""
if main_story:
synopsis_data += f"""
# Main Story
```json
{json.dumps(main_story, indent=4, ensure_ascii=False)}
```
"""
if synopsis:
synopsis_data += f"""
# Previous Synopsis
{synopsis}
"""
if story_data:
synopsis_data += f"""
# Previous Story Data
```json
{json.dumps(story_data, indent=4, ensure_ascii=False)}
```
"""
payload = [
{"role": "SYSTEM", "message": synopsis_prompt},
]
key2 = f"response: {synopsis_prompt} {synopsis_data}"
response = load_memory(key2)
if nocache:
response = None
if not response:
response = self.client.chat(
chat_history=payload,
message=str(synopsis_data),
model="command-r-plus"
).text
save_memory(key2, response)
result = response
save_memory(key, result)
return result
def get_voices(self) -> dict:
name_voice = {}
for charactor in self.main_story.get("charactors"):
name = charactor.get("charactor name")
voice = charactor.get("voice")
#print(f"Getting voice for {name} ({voice}) - {self.settings.get('used_voices')}")
if name not in self.settings.get("used_voices"):
exclude_voices = []
for chara in self.settings.get("used_voices"):
exclude_voices.append(self.settings.get("used_voices")[chara]["voice_name"])
self.settings.get("used_voices")[name] = {
"define":voice,
"voice_name": voice_selector.get_voice_name(charactor, exclude_voices)}
print(f"New voice: {name} ({voice}) - {self.settings.get('used_voices')[name]['voice_name']}")
name_voice[name] = self.settings.get("used_voices")[name]["voice_name"]
#print(name_voice)
result = {}
for story in self.story.get("story data"):
if story["mode"] == "talk":
data = {}
name = story["charactor name"]
voice = name_voice.get(name)
data["voice_name"] = voice
data["talk"] = story["talk"]
result[self.get_voice_key(name, story["talk"])] = data
return result
def get_backgrounds(self) -> dict:
result = {}
for story in self.story.get("story data"):
if story["mode"] == "change background image":
result[self.get_image_key(story["data"])] = "異世界ファンタジー、" + story["data"]
return result
def get_charactors(self) -> dict:
result = {}
for chara in self.main_story["charactors"]:
result[self.get_image_key(chara["charactor name"])] = chara
return result
def get_bgm(self) -> dict:
result = []
for story in self.story.get("story data"):
if story["mode"] == "change bgm":
if story.get("data"):
#select_bgm = sound_selector.sound_selector(story['data'])
select_bgm = DataSelector(self.client).bgm(story['data'])
result.append(select_bgm)
print(f"Selected BGM: {story['data']} - {select_bgm}")
return result
def get_voice_key(self, charactor_name:str, talk:str) -> str:
return hashlib.md5(f"{charactor_name}{talk}".encode()).hexdigest()
def get_image_key(self, charactor_name:str) -> str:
return hashlib.md5(f"{charactor_name}".encode()).hexdigest()
def my_splitfunc(self, string, delimiters):
import re
pattern = '([' + ''.join(map(re.escape, delimiters)) + '])'
return re.split(pattern, string)
def replace_underscore_in_dict(self, data):
if isinstance(data, dict):
new_data = {}
for key, value in data.items():
new_key = key.replace("_", " ")
new_data[new_key] = self.replace_underscore_in_dict(value)
return new_data
elif isinstance(data, list):
return [self.replace_underscore_in_dict(item) for item in data]
elif isinstance(data, str):
return data.replace("_", " ")
else:
return data
def auto_fix_tags(self):
fix_data = {
"play sound effect": ["se", "sound effect", "play sound", "play se"],
"change background image": ["background", "bg", "change background", "change bg", "background image"],
"change bgm": ["bgm", "change background music", "set bgm", "bgm change"],
"player characters inner voice": ["inner voice", "player inner voice", "players inner voice", "charactor inner voice"],
"talk": ["dialogue", "talking", "speak", "say", "dialog", "speech"],
"show charactor": ["show", "display", "charactor"],
"hide charactor": ["hide", "remove", "charactor"],
}
def is_fix_target(data, values):
for val in values:
if val == data:
return True
return False
print("Fixing tags...")
self.story = self.replace_underscore_in_dict(self.story)
#print(self.story)
fix_storys = []
charactor_name_table = {}
for charactor in self.main_story.get("charactors"):
charactor_name_table[charactor["charactor name"]] = []
charactor_name_table[charactor["charactor name"]].append(charactor["display name"])
bg_image_cache = None
bgm_found = False
for story in self.story.get("story data"):
clone_story = story.copy()
for key, values in fix_data.items():
if is_fix_target(clone_story["mode"], values):
clone_story["mode"] = key
print(f"Fixed: {story['mode']} -> {key}")
break
if clone_story["mode"] == "talk":
exp = clone_story.get("expression", "smile")
if not exp:
exp = "smile"
exp = exp.strip().lower().replace("evil smile", "evil")
if exp not in ["sad", "surprise", "evil", "shy", "angly", "smile"]:
exp = DataSelector(self.client).expression(exp) #pose_selector.get_expression(exp)
if clone_story["charactor name"] not in charactor_name_table:
for charactor_name, val_names in charactor_name_table.items():
for val_name in val_names:
if clone_story["charactor name"] == val_name:
clone_story["charactor name"] = charactor_name
break
clone_story["expression"] = exp
if clone_story["mode"] == "change bgm":
bgm_found = True
if clone_story["mode"] == "change background image" and not bg_image_cache:
bg_image_cache = clone_story["data"]
self.settings["temp bgm"] = DataSelector(self.client).bgm(bg_image_cache)#sound_selector.sound_selector(bg_image_cache)
fix_storys.append(clone_story)
if not bgm_found:
fix_storys.insert(0, {"mode":"change bgm", "data": bg_image_cache})
self.story["story data"] = fix_storys
def save(self, save_dir:str) -> None:
with open(f"{save_dir}/{self.uuid}.txt", "w", encoding="utf8") as f:
f.write(f"changeFigure:none -left -next;\n")
f.write(f"changeFigure:none -right -next;\n")
f.write(f"changeFigure:none -next;\n")
if self.settings.get("temp bgm"):
f.write(f"bgm:{self.settings.get('temp bgm')}.mp3 -enter=3000 -next;\n")
for story in self.story.get("story data"):
if story["mode"] == "talk":
exp = story.get('expression',"smile").lower().replace("evil smile", "evil")
if exp not in ["sad", "surprise", "evil", "shy", "angly", "smile"]:
exp = "smile"
#print(story)
image_key = self.get_image_key(story["charactor name"])
voice_key = self.get_voice_key(story["charactor name"], story["talk"])
f.write(f"changeFigure:{image_key}_{exp}.png -next;\n")
f.write(f"{story['display name']}:{story['talk']} -{voice_key}.mp3;\n")
if story["mode"] == "player characters inner voice":
data = story.get("data", None)
if data:
f.write(f":{data};\n")
if story["mode"] == "change background image":
image_key = self.get_image_key(story["data"])
f.write(f"changeBg:{image_key}.jpg -next;\n")
if story["mode"] == "change bgm":
bgm_data = story.get("data")
if bgm_data:
#result = sound_selector.sound_selector(story['data'])
result = DataSelector(self.client).bgm(bgm_data)
f.write(f"bgm:{result}.mp3 -next;\n")
choice_list = []
for choice in self.choices:
choice_list.append(f"{choice.user_choise}:{choice.uuid}.txt")
f.write(f"choose:{'|'.join(choice_list)};\n")
def voice_worker(client, q):
pg = VoiceGenerator()
while True:
item = q.get()
print(f"Voice worker: {item}")
if item is None:
break
if os.path.exists(item["file_name"]):
q.task_done()
print(f"Voice exists: {item['file_name']}")
continue
try:
pg.save_voice(item["file_name"], item["voice_name"], item["text"])
except Exception as e:
print(f"Failed to save voice: {e}")
q.task_done()
def image_worker(client, q):
pg = PromptGenerator(client)
while True:
item = q.get()
if item is None:
break
print(f"Image worker: {item}")
if os.path.exists(item["file_name"]):
q.task_done()
print(f"Image exists: {item['file_name']}")
continue
bg_prompt = pg.get_background(item["image"])
comfyui_client = None
try:
comfyui_client = ComfyUIClient("127.0.0.1:8188", "bgimage_api.json")
comfyui_client.connect()
comfyui_client.set_data(key='KSampler', seed=random.randint(0, 1000000))
comfyui_client.set_data(key='Input Prompt', text=bg_prompt)
for key, image in comfyui_client.generate(["Result Image"]).items():
image.save(item["file_name"])
finally:
if comfyui_client is not None:
comfyui_client.close()
q.task_done()
def charactor_worker(client, q, server):
pg = PromptGenerator(client)
def _inwork(item):
if os.path.exists(f"{item['file_name']}_smile.png"):
q.task_done()
print(f"Charactor exists: {item['file_name']}")
return True
print(f"Charactor worker: {item}")
chara_prompt = pg.get_charactor(item["charactor"])
face_prompt = pg.get_face(chara_prompt)
comfyui_client = ComfyUIClient(server, "text2cast_api.json")
comfyui_client.connect()
comfyui_client.set_data(key='KSampler', seed=random.randint(0, 1000000))
comfyui_client.set_data(key='Input Charactor Prompt', text=chara_prompt)
comfyui_client.set_data(key='Input Face Prompt', text=face_prompt)
used_poses = load_memory("used_poses list") or []
while len(used_poses) > 10:
used_poses.pop(0)
pose_file = random.choice(pose_selector.get_pose_image(chara_prompt, used_poses))
used_poses.append(pose_file)
save_memory("used_poses list", used_poses)
comfyui_client.set_data(key='Load Image', image=Image.open(pose_file))
for key, image in comfyui_client.generate(["Result Smile", "Result Angly", "Result Shy", "Result Evil", "Result Surprise", "Result Sad"]).items():
expression = key.replace("Result ", "").lower().strip()
image.save(f"{item['file_name']}_{expression}.png")
comfyui_client.close()
return True
while True:
item = q.get()
if item is None:
break
for _ in range(3):
try:
if _inwork(item):
q.task_done()
break
except Exception as e:
print(f"Failed to save charactor: {e}")
else:
print(f"Failed to save charactor: {item}")
def title_worker(client, q, server):
pg = PromptGenerator(client)
while True:
item = q.get()
print(f"Title worker")
if item is None:
break
if os.path.exists(item["file_name"]):
q.task_done()
print(f"Title exists: {item['file_name']}")
continue
title_prompt = pg.get_title(item['data'])
comfyui_client = ComfyUIClient(server, "titleimage_api.json")
comfyui_client.connect()
comfyui_client.set_data(key='KSampler', seed=random.randint(0, 1000000))
comfyui_client.set_data(key='Prompt Input', text=title_prompt)
for key, image in comfyui_client.generate(["Result Image"]).items():
image.save(item["file_name"])