見出し画像

フィラーをランダムに入れる雛形スクリプト

以下の記事の続きです。

とりあえずお試しということで、GPTに相談してみました。

import random
filler_list = ["えーと", "あのー", "うーん", "そうですね", "まあ", "なんか", "なんというか", "とりあえず", "まあね", "まあまあ"]

というのを頭にいれておいて、ユーザー入力の直後に以下のスクリプト


      random_fillers = random.sample(filler_list, 2)
      text = ' '.join(random_fillers)
      # Run the speak function asynchronously in the background
      asyncio.create_task(speak(speaker, text))

この場合は、await speak(speaker, text) との違いは、こういうことらしい。

If you want the filler part to proceed independently without blocking the execution of the subsequent code, you can use asyncio.create_task() to run the speak function asynchronously in the background. This way, the execution can continue to the next script without waiting for the speak function to complete.

下記の記事に上のスクリプトを組み込んで試してみました。

試してみたら、生成が早い場合は、フィラー途中で割り込むように喋り出す感じです。ただ70Bモデルの karakuri-lm-70b や wizardlm-70b などの場合は、一応、間をもたすようなフィラーが発言に入る感じですね。


一応全体のコード karakuriを指定してます。
MacOS標準のVoiceでしゃべります。

import shutil
import asyncio
import argparse

import ollama

## fillerのため
import random
filler_list = ["えーと", "あのー", "うーん", "そうですね", "まあ", "なんか", "なんというか", "とりあえず", "まあね", "まあまあ"]
##

async def speak(speaker, content):
  if speaker:
    p = await asyncio.create_subprocess_exec(speaker, content)
    await p.communicate()


async def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('--speak', default=False, action='store_true')
  args = parser.parse_args()

  speaker = None
  if not args.speak:
    ...
  elif say := shutil.which('say'):
    speaker = say
  elif (espeak := shutil.which('espeak')) or (espeak := shutil.which('espeak-ng')):
    speaker = espeak

  client = ollama.AsyncClient()

  messages = []

  while True:
    if content_in := input('>>> '):
        ##
      random_fillers = random.sample(filler_list, 2)
      text = ' '.join(random_fillers)
      # Run the speak function asynchronously in the background
      asyncio.create_task(speak(speaker, text))
      ##
      messages.append({'role': 'user', 'content': content_in})

      content_out = ''
      message = {'role': 'assistant', 'content': ''}
      async for response in await client.chat(model='karakuri-lm-70b', messages=messages, stream=True):
        if response['done']:
          messages.append(message)

        content = response['message']['content']
        print(content, end='', flush=True)

        content_out += content
        if content in ['.', '。', '、', '!', '?','?', '\n']:
          await speak(speaker, content_out)
          content_out = ''

        message['content'] += content

      if content_out:
        await speak(speaker, content_out)
      print()


try:
  asyncio.run(main())
except (KeyboardInterrupt, EOFError):
  ...

(Ctrl +Z でないと中断しません)


#AI #AIとやってみた #やってみた #ローカルLLM #Ollama #フィラー

この記事が参加している募集

やってみた

AIとやってみた

この記事を最後までご覧いただき、ありがとうございます!もしも私の活動を応援していただけるなら、大変嬉しく思います。