Spaces:
Sleeping
Sleeping
File size: 1,747 Bytes
410b2f3 |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# main.py
from fastapi import FastAPI, HTTPException
from fastapi.responses import Response
from gtts import gTTS
from pydantic import BaseModel
import io
import os
app = FastAPI(title="Text-to-Speech API")
class TextToSpeechRequest(BaseModel):
text: str
lang: str = "en"
slow: bool = False
@app.get("/")
async def root():
return {"message": "Text-to-Speech API is running. Use POST /tts to convert text to speech."}
@app.post("/tts")
async def text_to_speech(request: TextToSpeechRequest):
try:
# Create a bytes buffer
audio_buffer = io.BytesIO()
# Generate TTS audio
tts = gTTS(text=request.text, lang=request.lang, slow=request.slow)
tts.write_to_fp(audio_buffer)
# Reset buffer pointer to the beginning
audio_buffer.seek(0)
# Return the audio file
return Response(
content=audio_buffer.getvalue(),
media_type="audio/mp3",
headers={"Content-Disposition": f"attachment; filename=speech.mp3"}
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"TTS generation failed: {str(e)}")
@app.get("/languages")
async def get_available_languages():
# List of commonly supported languages in gTTS
languages = {
"en": "English",
"fr": "French",
"es": "Spanish",
"de": "German",
"it": "Italian",
"ja": "Japanese",
"ko": "Korean",
"pt": "Portuguese",
"ru": "Russian",
"zh-CN": "Chinese (Simplified)"
}
return languages
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 8000))
uvicorn.run("main:app", host="0.0.0.0", port=port) |