Skip to content

Instantly share code, notes, and snippets.

@ina111
Created March 11, 2023 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ina111/64a92c53171f96a678adf5f77f292cca to your computer and use it in GitHub Desktop.
Save ina111/64a92c53171f96a678adf5f77f292cca to your computer and use it in GitHub Desktop.
Slack上にOpenAI社の文字起こし(speech to text)の音声認識モデルであるWhisperのAPIから文字起こしさせるスクリプト
import os
import requests
from dotenv import load_dotenv
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
import openai
load_dotenv()
app = App(token=os.environ["SLACK_BOT_TOKEN"])
openai.api_key = os.environ["OPENAI_API_KEY"]
@app.event("app_mention")
def whisper(event, say):
if not ("files" in event): # Check if message contains an attachment
output = "ファイルが見つかりません。\n私はspeech to text AIです。以下、いずれかの形式のファイルを添付してください。\nmp3, mp4, mpeg, mpga, m4a, wav, webm"
else:
file = event["files"][0]
filetype = file["filetype"]
print("file type = " + filetype) # for debug
if filetype == "mp3" or filetype == "mp4" or filetype == "mpeg" or filetype == "m4a" or \
filetype == "mpga" or filetype == "webm" or filetype == "wav":
title = file["title"]
url = file["url_private"]
filename = "temp." + filetype
print("音声ファイル発見: " + title) # for debug
resp = requests.get(url, headers={'Authorization': 'Bearer %s' % os.environ["SLACK_BOT_TOKEN"]})
with open(filename, 'wb') as f:
f.write(resp.content)
print("ファイルオープン") # for debug
if os.path.getsize(filename) > 25000000:
output = "ファイルサイズオーバー。ファイルサイズは25MBにしてください。"
else:
with open(filename, "rb") as audio_file:
language = "en" if "english" in event["text"].casefold() else "ja" # メッセージにenglishが入っていたら英語音声とする
transcript = openai.Audio.transcribe("whisper-1", audio_file, language=language)
output = f"文字起こし致しました:{title}.{filetype}\n----\n" + transcript["text"]
os.remove(filename)
else:
output = "適合するファイルではありません。以下、いずれかの形式のファイルを添付してください。\nmp3, mp4, mpeg, mpga, m4a, wav, webm"
thread_ts = event.get("thread_ts") or None
channel = event["channel"]
if thread_ts is not None:
parent_thread_ts = event["thread_ts"]
say(text=output, thread_ts=parent_thread_ts, channel=channel)
else:
say(text=output, channel=channel)
print(output)
if __name__ == "__main__":
handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
handler.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment