qopscribe/speech_service/main.py

60 lines
2.1 KiB
Python

import asyncio
import logging
from config import load_config
from models import AudioTask
from redis_client import RedisClient
from transcriber import WhisperTranscriber
async def process_audio_task(redis_client: RedisClient, transcriber: WhisperTranscriber, task_data: dict):
try:
task = AudioTask(**task_data)
except Exception as e:
logging.error(f"Error creating AudioTask from data: {e}")
return
logging.info(f"Processing task {task.uuid} ...")
loop = asyncio.get_running_loop()
text = await loop.run_in_executor(None, transcriber.transcribe, task.file_path)
logging.info(f"Transcription completed for task {task.uuid}, text length: {len(text)}")
summarize_task = {
"chat_id": task.chat_id,
"user_id": task.user_id,
"message_id": task.message_id,
"text": text
}
await redis_client.send_to_summarize(summarize_task)
logging.info(f"Sent text to summarize service for task {task.uuid}")
async def main():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
config = load_config()
logging.info(f"Loaded config: REDIS_HOST={config['REDIS_HOST']}, REDIS_PORT={config['REDIS_PORT']}")
redis_client = RedisClient(
host=config["REDIS_HOST"],
port=config["REDIS_PORT"],
task_channel=config["AUDIO_TASK_CHANNEL"],
result_channel=config["TEXT_RESULT_CHANNEL"],
text_task_channel="text_task_channel"
)
transcriber = WhisperTranscriber(config["WHISPER_MODEL"], config["DEVICE"])
logging.info(f"Initialized transcriber with model {config['WHISPER_MODEL']} on {config['DEVICE']}")
logging.info(f"Waiting for audio tasks in channel {config['AUDIO_TASK_CHANNEL']}...")
while True:
task_data = await redis_client.get_task(timeout=1)
if task_data:
logging.info(f"Received task: {task_data.get('uuid', 'unknown')}")
asyncio.create_task(process_audio_task(redis_client, transcriber, task_data))
await asyncio.sleep(0.1)
if __name__ == "__main__":
asyncio.run(main())