File size: 973 Bytes
604255a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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))