| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import asyncio
- import json
- from typing import Optional, Any
- from sqlalchemy import select
- from app.config.config import settings
- from app.models.db import get_async_session
- from app.models.asr import AsrRecordStatus, AsrRecordTypes, AsrRecord, AsrRecordModel
- from app.errors.base import ParamsError
- from app.utils.common import uuid_generate
- from app.utils.httpbase import HttpBase
- class AsrRecordService:
- @staticmethod
- async def get_asr_record(record_id: int) -> Optional[AsrRecord]:
- async with get_async_session() as session:
- result = await session.execute(
- select(AsrRecord).where(AsrRecord.id == record_id)
- )
- return result.scalar_one_or_none()
- @staticmethod
- async def list_asr_records(page: int, page_size: int) -> Optional[AsrRecord]:
- async with get_async_session() as session:
- result = await session.execute(
- select(AsrRecord).offset((page - 1) * page_size).limit(page_size)
- )
- return result.scalars().all()
- @staticmethod
- async def create_offline_asr_record(asr_record: AsrRecordModel) -> Optional[AsrRecord]:
- async with get_async_session() as session:
- record = AsrRecord(
- id=await uuid_generate(),
- task_id=asr_record.task_id,
- meeting_id=asr_record.meeting_id,
- file_url=asr_record.file_url,
- type=AsrRecordTypes.OFFLINE,
- status=AsrRecordStatus.PROCESSING,
- )
- session.add(record)
- await session.commit()
- return record
- @staticmethod
- async def create_realtime_asr_record(asr_record: AsrRecordModel) -> Optional[AsrRecord]:
- async with get_async_session() as session:
- record = AsrRecord(
- task_id=asr_record.task_id,
- file_url=asr_record.file_url,
- type=AsrRecordTypes.REALTIME,
- status=AsrRecordStatus.PROCESSING,
- )
- session.add(record)
- await session.commit()
- return record
- @staticmethod
- async def update_asr_record(asr_record: AsrRecordModel) -> Optional[AsrRecord]:
- async with get_async_session() as session:
- if asr_record.id:
- result = await session.execute(
- select(AsrRecord).where(AsrRecord.id == asr_record.id)
- )
- record = result.scalar_one()
- elif asr_record.task_id:
- result = await session.execute(
- select(AsrRecord).where(AsrRecord.task_id == asr_record.task_id)
- )
- record = result.scalar_one_or_none()
- else:
- raise ParamsError(message="Either 'id' or 'task_id' must be provided to update the record.")
- if not record:
- raise ParamsError(message="Either 'id' or 'task_id' must be provided to update the record.")
- if asr_record.file_url:
- record.file_url = asr_record.file_url
- if asr_record.type:
- record.type = asr_record.type
- if asr_record.status:
- record.status = asr_record.status
- if asr_record.result_url:
- record.result_url = asr_record.result_url
- await session.commit()
- return record
- @staticmethod
- async def create_asr_task_async(asr_record: AsrRecordModel) -> Optional[Any]:
- url = f"http://{settings.meeting_file_base_url}/api/process_audio_async"
- try:
- res = await HttpBase.http_post(url, {"audio_url": asr_record.file_url, "hotwords": asr_record.hotwords}, {})
- print(res.status_code)
- print(res.text)
- if res and res.status_code == 200:
- return res.json()
- except Exception as e:
- print(e)
- return {}
- if __name__ =="__main__":
- async def a():
- res = await AsrRecordService.create_asr_task_async(AsrRecordModel(file_url="http://192.168.20.119:9000/aimeeting/e0df3b8befc342539257d5fa35d5b578.wav"))
- print(res)
- # print(res.status_code)
- # print(res.text)
- asyncio.run(a())
|