File size: 734 Bytes
5ea1a43
 
 
 
 
 
a862211
 
5ea1a43
 
aa9955a
5ea1a43
 
aa9955a
8b525a1
5ea1a43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse
from gtts import gTTS
import os
import uuid



app = FastAPI()

UPLOAD_DIR = "/tmp/audio"
os.makedirs(UPLOAD_DIR, exist_ok=True)


@app.post("/echo")
async def echo(request: Request):
    data = await request.json()
    text = data.get("text", "")

    if not text:
        return {"error": "No 'text' provided in request."}

    # Generate unique filename
    filename = f"{uuid.uuid4().hex}.mp3"
    filepath = os.path.join(UPLOAD_DIR, filename)

    # Generate and save speech
    tts = gTTS(text, lang='en')
    tts.save(filepath)

    # Return the audio file as response
    return FileResponse(filepath, media_type="audio/mpeg", filename=filename)