reoapae / src /api /api.py
puzan789's picture
Updated
604255a
raw
history blame
973 Bytes
from fastapi import FastAPI
import asyncio
from src.pipeline.pipeline import Pipeline
from fastapi.routing import APIRouter
from fastapi.responses import StreamingResponse
from fastapi.exceptions import HTTPException
import io
conversation=APIRouter()
pipe=Pipeline()
@conversation.get("/record")
async def record_and_transcribe():
transcribed_text=pipe.speech_to_text_()
return {"transcription": transcribed_text}
@conversation.get("/text_response")
async def get_text_response():
response=await pipe.text_to_text_()
return {"response": response}
@conversation.get("/speech_response")
async def get_speech_response():
try:
response_=await pipe.text_to_speech_()
if not response_:
raise HTTPException(status_code=400, detail="Failed to generate audio.")
return StreamingResponse(io.BytesIO(response_), media_type="audio/wav")
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))