Browse Source

#31

ai-migration
みてるぞ 3 weeks ago
parent
commit
29b831e380
2 changed files with 55 additions and 12 deletions
  1. +1
    -1
      nizika_ai
  2. +54
    -11
      test.py

+ 1
- 1
nizika_ai

@@ -1 +1 @@
Subproject commit 8e1d305b4ab1346ef2d2faf6eacda440674b06e8
Subproject commit 662b7d69de66bb8c09f8297817449179166c0f3d

+ 54
- 11
test.py View File

@@ -1,6 +1,7 @@
from __future__ import annotations

import math
import os
import random
import sys
from datetime import datetime, timedelta
@@ -13,6 +14,7 @@ import ephem # type: ignore
import pygame
import pygame.gfxdraw
import pytchat # type: ignore
import requests
from cv2 import VideoCapture
from ephem import Moon, Observer, Sun # type: ignore
from pygame import Rect, Surface
@@ -24,7 +26,7 @@ from pytchat.processors.default.processor import Chat # type: ignore

from common_module import CommonModule
from nizika_ai.models import Answer, AnsweredFlag, Query, User
from nizika_ai.consts import Platform
from nizika_ai.consts import AnswerType, Character, GPTModel, Platform, QueryType

pygame.init ()

@@ -46,7 +48,7 @@ def main (
Deerjika (game, DeerjikaPattern.RELAXED, x = CWindow.WIDTH * 3 / 4, y = CWindow.HEIGHT - 120)
balloon = Balloon (game)
CurrentTime (game, SYSTEM_FONT)
broadcast = Broadcast ()
broadcast = Broadcast (os.environ['BROADCAST_CODE'])
try:
Sound ('assets/bgm.mp3').play (loops = -1)
except Exception:
@@ -56,14 +58,17 @@ def main (
if event.type == pygame.QUIT:
pygame.quit ()
sys.exit ()
chat = broadcast.fetch_chat ()
if chat is not None:
chat.message = emoji.emojize (chat.message)
message: str = chat.message
user = (User.where ('platform', Platform.YOUTUBE.value)
.where ('code', chat.author.channelId)
.get ())
# chat.author
answer_flags = (AnsweredFlag.where ('platform', Platform.YOUTUBE.value)
.where ('answered', False)
.get ())
if not balloon.enabled:
if answer_flags:
answer_id: int = random.choice (answer_flags).answer_id
answer = Answer.find (answer_id)
if answer.answer_type == AnswerType.YOUTUBE_REPLY.value:
query = Query.find (answer.query_id)
balloon.talk (query.content, answer.content)
add_query (broadcast)
game.redraw ()


@@ -666,8 +671,9 @@ class Broadcast:

def __init__ (
self,
broadcast_code,
):
self.chat = pytchat.create (os.environ['BROADCAST_CODE'])
self.chat = pytchat.create (broadcast_code)

def fetch_chat (
self,
@@ -684,5 +690,42 @@ class Log:
...


def fetch_bytes_from_url (
url: str,
) -> bytes | None:
res = requests.get (url, timeout = 60)
if res.status_code != 200:
return None
return res.content


def add_query (
broadcast: Broadcast,
) -> None:
chat = broadcast.fetch_chat ()
if chat is None:
return
chat.message = emoji.emojize (chat.message)
message: str = chat.message
user = (User.where ('platform', Platform.YOUTUBE.value)
.where ('code', chat.author.channelId)
.first ())
if user is None:
user = User ()
user.platform = Platform.YOUTUBE.value
user.code = chat.author.channelId
user.name = chat.author.name
user.icon = fetch_bytes_from_url (chat.author.imageUrl)
user.save ()
query = Query ()
query.user_id = user.id
query.target_character = Character.DEERJIKA.value
query.content = chat.message
query.query_type = QueryType.YOUTUBE_COMMENT.value
query.model = GPTModel.GPT3_TURBO.value
query.sent_at = datetime.now ()
query.answerd = False
query.save ()

if __name__ == '__main__':
main ()

Loading…
Cancel
Save