s
File size: 908 Bytes
3076331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
from fastapi import FastAPI, UploadFile
from fastapi.responses import FileResponse
from whisper_utils import transcribe_audio
from gtts_utils import generate_speech
from llm_utils import get_llm_response
import os

app = FastAPI()

@app.post("/transcribe/")
async def transcribe(file: UploadFile):
    file_path = f"audio/{file.filename}"
    with open(file_path, "wb") as audio:
        audio.write(await file.read())

    text = transcribe_audio(file_path)
    os.remove(file_path)  # Cleanup audio file
    return {"transcription": text}

@app.post("/response/")
async def get_response(input_text: str):
    llm_response = get_llm_response(input_text)
    audio_path = generate_speech(llm_response)
    return {"response": llm_response, "audio_url": audio_path}

@app.get("/audio/{file_name}")
async def serve_audio(file_name: str):
    file_path = f"audio/{file_name}"
    return FileResponse(file_path)