firestoreDBからメッセージ取得

firestoreDBからメッセージ取得

 python main3.py      

で実行

[2024-11-10 06:45:16.017] [ctranslate2] [thread 3848107] [warning] The compute type inferred from the saved model is float16, but the target device or backend do not support efficient float16 computation. The model weights have been automatically converted to use the float32 compute type instead.
stand by ready OK
recording...
finished
認識されたテキスト: 今度はiCloud Driveに保存しますか
Firestoreに保存を開始します: 今度はiCloud Driveに保存しますか
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1731188739.927080 3848107 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache
Firestoreに保存されました: 今度はiCloud Driveに保存しますか
Firestoreへの保存処理が完了しました
stand by ready OK
recording...
finished
10秒間音声が入力されなかったため、ループを終了します。

入力された音声テキスト一覧:
今度はiCloud Driveに保存しますか

でDB保存された後に

import warnings
from google.cloud import firestore
import datetime

# 警告を無視する設定
warnings.filterwarnings("ignore", category=UserWarning, module="google.cloud.firestore_v1.base_collection")

# Firestore クライアントの初期化
db = firestore.Client.from_service_account_json("serviceAccountKey.json")

def get_user_messages(user_id):
    messages_ref = db.collection("messages")
    
    # 複数条件のクエリを設定
    query = messages_ref.where("user_id", "==", user_id).where("timestamp", ">=", datetime.datetime.now() - datetime.timedelta(days=1))
    
    # メッセージを取得してリストに格納
    user_messages = []
    for doc in query.stream():
        user_messages.append(doc.to_dict())

    return user_messages

# 特定のユーザーIDを指定してメッセージを取得
user_id = ""
messages = get_user_messages(user_id)

for message in messages:
    print(f"メッセージ: {message['text']}, タイムスタンプ: {message['timestamp']}")

を実行したけど

/Users/snowpool/aw10s/linebot/get_message.py:15: UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.
  query = messages_ref.where("user_id", "==", user_id).where("timestamp", ">=", datetime.datetime.now() - datetime.timedelta(days=1))
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1731188828.849859 3850189 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache

となる

原因を調べるため
Firestoreへログインし
user_idの値を調べたら
user_id_example
となっていて
linebotのIDではなかった

このため

import warnings
from google.cloud import firestore
import datetime

# 警告を無視する設定
warnings.filterwarnings("ignore", category=UserWarning, module="google.cloud.firestore_v1.base_collection")

# Firestore クライアントの初期化
db = firestore.Client.from_service_account_json("serviceAccountKey.json")

def get_user_messages(user_id):
    messages_ref = db.collection("messages")
    
    # 複数条件のクエリを設定
    query = messages_ref.where("user_id", "==", user_id).where("timestamp", ">=", datetime.datetime.now() - datetime.timedelta(days=1))
    
    # メッセージを取得してリストに格納
    user_messages = []
    for doc in query.stream():
        user_messages.append(doc.to_dict())

    return user_messages

# 特定のユーザーIDを指定してメッセージを取得
user_id = "user_id_example"


messages = get_user_messages(user_id)

for message in messages:
    print(f"メッセージ: {message['text']}, タイムスタンプ: {message['timestamp']}")

と変更し

python get_message.py

を実行

/Users/snowpool/aw10s/linebot/get_message.py:15: UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.
  query = messages_ref.where("user_id", "==", user_id).where("timestamp", ">=", datetime.datetime.now() - datetime.timedelta(days=1))
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1731190387.989119 3864599 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache
メッセージ: 動作していますか, タイムスタンプ: 2024-11-09 21:39:35.038000+00:00
メッセージ: 今度はiCloud Driveに保存しますか, タイムスタンプ: 2024-11-09 21:45:40.490000+00:00

となった

つまり送信した時のuser_id がlinebotのものになるようにしないとダメ

user_idの値を実際のLINE botユーザーのIDに設定する

user_id の値を user_id_example ではなく linebotのユーザの値にしたい

FireStore の
user_idの値を実際のLINE botユーザーのIDに設定するには、
main2.pyのコードで、ユーザーIDを動的に取得し、Firestoreに保存する際に正しいIDを設定する必要がある

GPTによれば

修正方法
1. main2.pyのuser_idを設定する部分で、LINE botからユーザーIDを取得できるようにします。
2. 例えば、index.jsでLINEから送られてきたユーザーIDを取得し、Pythonスクリプトに渡す仕組みを構築するか、必要に応じて固定のIDを用いる方法も考えられます。

とのこと

INE botユーザーのIDを正確に反映するために、user_idをmain2.pyに渡すか、
スクリプトで直接設定する

import sounddevice as sd
from module.module_whisper import FasterWhisperModel
from module.module_recorder import Recorder
import time
from line_notify import LineNotify  # 作成したLineNotifyモジュールをインポート
from ollama_text_correction import OllamaTextCorrector  # Ollamaによる修正モジュールをインポート
from line_bot_sender import LineBotSender  # LINE bot送信用のモジュールをインポート
import firebase_admin
from firebase_admin import credentials, firestore

# Firebase Admin SDKの初期化
if not firebase_admin._apps:
    cred = credentials.Certificate("path/to/serviceAccountKey.json")  # 認証ファイルのパスに置き換え
    firebase_admin.initialize_app(cred)

# Firestoreのクライアントを取得
db = firestore.client()

# LINE botユーザーIDを設定(実際のLINEユーザーIDに置き換え)
LINE_USER_ID = "実際のLINEユーザーのID"  # この部分を実際のユーザーIDに変更

def save_to_firestore(user_id, text):
    # Firestoreにメッセージを保存する関数
    try:
        message_data = {
            "user_id": user_id,
            "text": text,
            "timestamp": firestore.SERVER_TIMESTAMP,
            "read": False
        }
        db.collection("messages").add(message_data)
        print("Firestoreに保存されました:", text)
    except Exception as e:
        print(f"Firestoreに保存中にエラーが発生しました: {e}")

def main():
    recorder = Recorder()
    fasterWhispermodel = FasterWhisperModel()

    # 入力された音声テキストを格納するリスト
    recognized_texts = []

    # LINE Notifyのモジュールを初期化(config.jsonからトークンを読み込む)
    line_notify = LineNotify("config.json")
    
    # Ollamaのテキスト修正モジュールを初期化
    text_corrector = OllamaTextCorrector("config.json")
    
    # LINE bot送信用のモジュールを初期化
    line_bot_sender = LineBotSender("config.json")

    while True:
        start_time = time.time()  # 処理開始時刻を記録
        audio_data = recorder.speech2audio()

        # 処理が10秒間行われなかった場合はループを抜ける
        if time.time() - start_time >= 10:
            print("10秒間音声が入力されなかったため、ループを終了します。")
            break

        if audio_data is None:
            print("無音状態が続いたため、ループを終了します。")
            break  # 無音でループを抜ける
        
        # 音声をテキストに変換
        text = fasterWhispermodel.audio2text(audio_data)
        
        # Ollamaでテキストを構成
        corrected_text = text_corrector.correct_text(text)
        
        if corrected_text:  # Noneが返された場合はスキップ
            recognized_texts.append(corrected_text)
            print("認識されたテキスト:", corrected_text)

            # Firestoreに保存を開始
            print("Firestoreに保存を開始します:", corrected_text)
            save_to_firestore(LINE_USER_ID, corrected_text)  # LINEのユーザーIDを指定
            print("Firestoreへの保存処理が完了しました")

    # ループ終了後に、入力した音声テキストを改行付きで一覧表示
    if recognized_texts:
        message = "\n".join(recognized_texts)
        print("\n入力された音声テキスト一覧:")
        print(message)

        # LINE Notifyでメッセージを送信
        line_notify.send(f"入力された音声テキスト一覧:\n{message}")
        
        # LINE botで修正されたテキストを送信
        line_bot_sender.send_message(f"修正された音声テキスト:\n{message}")
    else:
        print("入力メッセージはありませんでした")

if __name__ == "__main__":
    main()

となるようにコード変更する

コードを新しいmain.4.pyを作成して実行する

touch main4.py

でコードを貼り付け

なおjsonファイルパスとユーザIDは修正しておく

import sounddevice as sd
from module.module_whisper import FasterWhisperModel
from module.module_recorder import Recorder
import time
from line_notify import LineNotify  # 作成したLineNotifyモジュールをインポート
from ollama_text_correction import OllamaTextCorrector  # Ollamaによる修正モジュールをインポート
from line_bot_sender import LineBotSender  # LINE bot送信用のモジュールをインポート
import firebase_admin
from firebase_admin import credentials, firestore

# Firebase Admin SDKの初期化
if not firebase_admin._apps:
    cred = credentials.Certificate("serviceAccountKey.json")  # 認証ファイルのパスに置き換え
    firebase_admin.initialize_app(cred)

# Firestoreのクライアントを取得
db = firestore.client()

# LINE botユーザーIDを設定(実際のLINEユーザーIDに置き換え)
LINE_USER_ID = ""  # この部分を実際のユーザーIDに変更

def save_to_firestore(user_id, text):
    # Firestoreにメッセージを保存する関数
    try:
        message_data = {
            "user_id": user_id,
            "text": text,
            "timestamp": firestore.SERVER_TIMESTAMP,
            "read": False
        }
        db.collection("messages").add(message_data)
        print("Firestoreに保存されました:", text)
    except Exception as e:
        print(f"Firestoreに保存中にエラーが発生しました: {e}")

def main():
    recorder = Recorder()
    fasterWhispermodel = FasterWhisperModel()

    # 入力された音声テキストを格納するリスト
    recognized_texts = []

    # LINE Notifyのモジュールを初期化(config.jsonからトークンを読み込む)
    line_notify = LineNotify("config.json")
    
    # Ollamaのテキスト修正モジュールを初期化
    text_corrector = OllamaTextCorrector("config.json")
    
    # LINE bot送信用のモジュールを初期化
    line_bot_sender = LineBotSender("config.json")

    while True:
        start_time = time.time()  # 処理開始時刻を記録
        audio_data = recorder.speech2audio()

        # 処理が10秒間行われなかった場合はループを抜ける
        if time.time() - start_time >= 10:
            print("10秒間音声が入力されなかったため、ループを終了します。")
            break

        if audio_data is None:
            print("無音状態が続いたため、ループを終了します。")
            break  # 無音でループを抜ける
        
        # 音声をテキストに変換
        text = fasterWhispermodel.audio2text(audio_data)
        
        # Ollamaでテキストを構成
        corrected_text = text_corrector.correct_text(text)
        
        if corrected_text:  # Noneが返された場合はスキップ
            recognized_texts.append(corrected_text)
            print("認識されたテキスト:", corrected_text)

            # Firestoreに保存を開始
            print("Firestoreに保存を開始します:", corrected_text)
            save_to_firestore(LINE_USER_ID, corrected_text)  # LINEのユーザーIDを指定
            print("Firestoreへの保存処理が完了しました")

    # ループ終了後に、入力した音声テキストを改行付きで一覧表示
    if recognized_texts:
        message = "\n".join(recognized_texts)
        print("\n入力された音声テキスト一覧:")
        print(message)

        # LINE Notifyでメッセージを送信
        line_notify.send(f"入力された音声テキスト一覧:\n{message}")
        
        # LINE botで修正されたテキストを送信
        line_bot_sender.send_message(f"修正された音声テキスト:\n{message}")
    else:
        print("入力メッセージはありませんでした")

if __name__ == "__main__":
    main()

これで

python main4.py

を実行

[2024-11-11 05:19:46.714] [ctranslate2] [thread 3933364] [warning] The compute type inferred from the saved model is float16, but the target device or backend do not support efficient float16 computation. The model weights have been automatically converted to use the float32 compute type instead.
stand by ready OK
recording...
finished
認識されたテキスト: 少々重量過剰かもしれない
Firestoreに保存を開始します: 少々重量過剰かもしれない
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1731270012.069859 3933364 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache
Firestoreに保存されました: 少々重量過剰かもしれない
Firestoreへの保存処理が完了しました
stand by ready OK
recording...
finished
10秒間音声が入力されなかったため、ループを終了します。

入力された音声テキスト一覧:
少々重量過剰かもしれない
[・shell]
となってユーザIDが反映されて保存される

python get_message.py

を実行すると

/Users/snowpool/aw10s/linebot/get_message.py:15: UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.
  query = messages_ref.where("user_id", "==", user_id).where("timestamp", ">=", datetime.datetime.now() - datetime.timedelta(days=1))
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1731270127.577985 3935695 config.cc:230] gRPC experiments enabled: call_status_override_on_cancellation, event_engine_dns, event_engine_listener, http2_stats_fix, monitoring_experiment, pick_first_new, trace_record_callops, work_serializer_clears_time_cache
メッセージ: 少々重量過剰かもしれない, タイムスタンプ: 2024-11-10 20:20:12.657000+00:00

でメッセージ取得も問題ない

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です