From 0bda9547b1285d073f47479fb293c4300d9cede3 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sat, 6 Apr 2024 05:39:34 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B4=E3=83=BC=E3=83=88=E3=81=86=E3=81=A8?= =?UTF-8?q?=E5=AF=BE=E8=A9=B1=20=E3=81=A8=E3=82=8A=E3=81=BE=E5=AE=8C?= =?UTF-8?q?=E6=88=90=EF=BC=88=E3=81=A4=E3=81=84=E3=81=A7=E3=81=AB=E3=82=84?= =?UTF-8?q?=E3=81=B0=E3=81=83=E3=83=90=E3=82=B0=E4=BF=AE=E6=AD=A3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common_const.py | 5 +- main.py | 173 ++++++++++++++++++++++++++++-------------------- 2 files changed, 103 insertions(+), 75 deletions(-) diff --git a/common_const.py b/common_const.py index f940ab5..899af72 100644 --- a/common_const.py +++ b/common_const.py @@ -1,7 +1,4 @@ -from pydantic import BaseModel - - -class CWindow (BaseModel, frozen = True): +class CWindow: WIDTH: int = 1024 HEIGHT: int = 768 diff --git a/main.py b/main.py index 3456874..9e628a6 100644 --- a/main.py +++ b/main.py @@ -23,15 +23,16 @@ class Main: @classmethod def main (cls, argv: list, argc: int) -> None: mode = Mode.NIZIKA - match (argc > 0) and argv[0]: + match (argc > 1) and argv[1]: case '-g': mode = Mode.GOATOH case '-w': mode = Mode.DOUBLE - goatoh_mode = mode == Mode.GOATOH - double_mode = mode == Mode.DOUBLE + nizika_mode: bool = mode == Mode.NIZIKA + goatoh_mode: bool = mode == Mode.GOATOH + double_mode: bool = mode == Mode.DOUBLE print (mode) @@ -103,82 +104,112 @@ class Main: message: str = chat_item.message - # ChatGPT API を呼出し,返答を取得 - answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_mode).replace ('\n', ' ') - - # 履歴に追加 - histories = (histories - + [{'role': 'user', 'content': message}, - {'role': 'assistant', 'content': answer}])[(-6):] - - # ログ書込み - with open ('log.txt', 'a') as f: - f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n') - - # 吹出し描画(ニジカは上,ゴートうは下) - screen.blit (balloon, (0, 384) if goatoh_mode else (0, 0)) - - # 視聴者コメント描画 - screen.blit ( - user_font.render ( - '> ' + (message - if (CommonModule.len_by_full (message) - <= 21) - else (CommonModule.mid_by_full ( - message, 0, 19.5) - + '...')), - True, - (0, 0, 0)), - (120, 70 + 384) if goatoh_mode else (120, 70)) - - # ニジカの返答描画 - screen.blit ( - nizika_font.render ( - (answer - if CommonModule.len_by_full (answer) <= 16 - else CommonModule.mid_by_full (answer, 0, 16)), - True, - (192, 0, 0)), - (100, 150 + 384) if goatoh_mode else (100, 150)) - if CommonModule.len_by_full (answer) > 16: + if nizika_mode: + goatoh_talking = False + if goatoh_mode: + goatoh_talking = True + if double_mode: + goatoh_talking: bool = random.random () < .5 + + while True: + # ChatGPT API を呼出し,返答を取得 + answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_talking).replace ('\n', ' ') + + # 履歴に追加 + histories = (histories + + [{'role': 'user', 'content': message}, + {'role': 'assistant', 'content': answer}])[(-12):] + + # ログ書込み + with open ('log.txt', 'a') as f: + f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n') + + # 吹出し描画(ニジカは上,ゴートうは下) + if nizika_mode: + screen.blit (balloon, (0, 0)) + if goatoh_mode: + screen.blit (balloon, (0, 384)) + if double_mode: + screen.blit (pygame.transform.flip ( + balloon, + not goatoh_talking, + False), + (0, 0)) + + # 視聴者コメント描画 + screen.blit ( + user_font.render ( + '> ' + (message + if (CommonModule.len_by_full (message) + <= 21) + else (CommonModule.mid_by_full ( + message, 0, 19.5) + + '...')), + True, + (0, 0, 0)), + (120, 70 + 384) if goatoh_mode else (120 + (64 if (double_mode and not goatoh_talking) else 0), 70)) + + # ニジカの返答描画 screen.blit ( nizika_font.render ( - (CommonModule.mid_by_full (answer, 16, 16) - if CommonModule.len_by_full (answer) <= 32 - else (CommonModule.mid_by_full ( - answer, 16, 14.5) - + '...')), - True, - (192, 0, 0)), - (100, 200 + 384) if goatoh_mode else (100, 200)) - - pygame.display.update () - - # 鳴く. - if goatoh_mode: - if random.random () < 0.1: - kusa.play () + (answer + if CommonModule.len_by_full (answer) <= 16 + else CommonModule.mid_by_full (answer, 0, 16)), + True, + (192, 0, 0)), + (100, 150 + 384) if goatoh_mode else (100 + (64 if (double_mode and not goatoh_talking) else 0), 150)) + if CommonModule.len_by_full (answer) > 16: + screen.blit ( + nizika_font.render ( + (CommonModule.mid_by_full (answer, 16, 16) + if CommonModule.len_by_full (answer) <= 32 + else (CommonModule.mid_by_full ( + answer, 16, 14.5) + + '...')), + True, + (192, 0, 0)), + (100, 200 + 384) if goatoh_mode else (100 + (64 if (double_mode and not goatoh_talking) else 0), 200)) + + pygame.display.update () + + # 鳴く. + if goatoh_talking: + if random.random () < .1: + kusa.play () + else: + mumumumu.play () else: - mumumumu.play () - else: - noon.play () + noon.play () + + time.sleep (1.5) + + # 返答の読上げを WAV ディタとして生成,取得 + try: + wav: bytearray | None = Aques.main (answer, goatoh_talking) + except: + wav: None = None + + # 読上げを再生 + if wav is not None: + with open ('./nizika_talking.wav', 'wb') as f: + f.write (wav) + + playsound ('./nizika_talking.wav') + + time.sleep (10) - time.sleep (1.5) + if not double_mode or random.random () < .5: + break - # 返答の読上げを WAV ディタとして生成,取得 - try: - wav: bytearray | None = Aques.main (answer, goatoh_mode) - except: - wav: None = None + screen.fill ((0, 255, 0)) - # 再生 - if wav is not None: - with open ('./nizika_talking.wav', 'wb') as f: - f.write (wav) + chat_item.author['name'] = 'ゴートうひとり' if goatoh_talking else '伊地知ニジカ' + chat_item.author['id'] = '' + chat_item.author['channelId'] = './favicon-goatoh.ico' if goatoh_talking else './favicon.ico' - playsound ('./nizika_talking.wav') + goatoh_talking = not goatoh_talking - time.sleep (10) + message = histories.pop (-1)['content'] else: # Chat オブジェクトが無効