ゴートうと対話 とりま完成(ついでにやばぃバグ修正)

このコミットが含まれているのは:
2024-04-06 05:39:34 +09:00
コミット 0bda9547b1
2個のファイルの変更96行の追加68行の削除
+1 -4
ファイルの表示
@@ -1,7 +1,4 @@
from pydantic import BaseModel
class CWindow (BaseModel, frozen = True):
class CWindow:
WIDTH: int = 1024
HEIGHT: int = 768
+44 -13
ファイルの表示
@@ -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,20 +104,37 @@ class Main:
message: str = chat_item.message
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_mode).replace ('\n', ' ')
answer: str = Talk.main (message, chat_item.author['name'], histories, goatoh_talking).replace ('\n', ' ')
# 履歴に追加
histories = (histories
+ [{'role': 'user', 'content': message},
{'role': 'assistant', 'content': answer}])[(-6):]
{'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')
# 吹出し描画(ニジカは上,ゴートうは下)
screen.blit (balloon, (0, 384) if goatoh_mode else (0, 0))
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 (
@@ -129,7 +147,7 @@ class Main:
+ '...')),
True,
(0, 0, 0)),
(120, 70 + 384) if goatoh_mode else (120, 70))
(120, 70 + 384) if goatoh_mode else (120 + (64 if (double_mode and not goatoh_talking) else 0), 70))
# ニジカの返答描画
screen.blit (
@@ -139,7 +157,7 @@ class Main:
else CommonModule.mid_by_full (answer, 0, 16)),
True,
(192, 0, 0)),
(100, 150 + 384) if goatoh_mode else (100, 150))
(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 (
@@ -150,13 +168,13 @@ class Main:
+ '...')),
True,
(192, 0, 0)),
(100, 200 + 384) if goatoh_mode else (100, 200))
(100, 200 + 384) if goatoh_mode else (100 + (64 if (double_mode and not goatoh_talking) else 0), 200))
pygame.display.update ()
# 鳴く.
if goatoh_mode:
if random.random () < 0.1:
if goatoh_talking:
if random.random () < .1:
kusa.play ()
else:
mumumumu.play ()
@@ -167,11 +185,11 @@ class Main:
# 返答の読上げを WAV ディタとして生成,取得
try:
wav: bytearray | None = Aques.main (answer, goatoh_mode)
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)
@@ -179,6 +197,19 @@ class Main:
playsound ('./nizika_talking.wav')
time.sleep (10)
if not double_mode or random.random () < .5:
break
screen.fill ((0, 255, 0))
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'
goatoh_talking = not goatoh_talking
message = histories.pop (-1)['content']
else:
# Chat オブジェクトが無効