52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
import asyncio
|
|
import json
|
|
from config import load_config
|
|
from models import AudioTask
|
|
from redis_client import RedisClient
|
|
from transcriber import WhisperTranscriber
|
|
import httpx
|
|
|
|
async def process_audio_task(redis_client: RedisClient, transcriber: WhisperTranscriber, task_data: dict):
|
|
try:
|
|
task = AudioTask(**task_data)
|
|
except Exception as e:
|
|
print(f"Error creating AudioTask from data: {e}")
|
|
return
|
|
|
|
print(f"Processing task {task.uuid} ...")
|
|
loop = asyncio.get_running_loop()
|
|
text = await loop.run_in_executor(None, transcriber.transcribe, task.file_path)
|
|
|
|
# Отправляем текст в сервис суммаризации
|
|
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)
|
|
print(f"Sent text to summarize service for task {task.uuid}")
|
|
|
|
|
|
async def main():
|
|
config = load_config()
|
|
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"])
|
|
|
|
print("Waiting for audio tasks...")
|
|
|
|
while True:
|
|
task_data = await redis_client.get_task(timeout=1)
|
|
if task_data:
|
|
asyncio.create_task(process_audio_task(redis_client, transcriber, task_data))
|
|
await asyncio.sleep(0.1)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|