File size: 660 Bytes
e60c650
 
 
 
01b39cb
e60c650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01b39cb
 
 
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
from fastapi import FastAPI
import edge_tts
import asyncio
from fastapi.responses import FileResponse
import uvicorn

app = FastAPI()

@app.get("/")
def home():
    return {"message": "EdgeTTS FastAPI is running!"}

@app.get("/tts")
async def tts(text: str, voice: str = "en-US-AriaNeural"):
    output_file = "output.mp3"

    # Generate speech
    communicate = edge_tts.Communicate(text, voice)
    await communicate.save(output_file)

    return FileResponse(output_file, media_type="audio/mpeg", filename="speech.mp3")

# Ensure the app starts when running in Hugging Face Spaces
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)