Spaces:
Sleeping
Sleeping
| # 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 | |
| async def root(): | |
| return {"message": "Text-to-Speech API is running. Use POST /tts to convert text to speech."} | |
| 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)}") | |
| 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) |