qopscribe/speech_service/main.py

60 lines
2.1 KiB
Python
Raw Normal View History

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