Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from fastapi.responses import FileResponse
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
class AudioRequest(BaseModel):
|
| 9 |
+
request_id: str
|
| 10 |
+
text: str
|
| 11 |
+
language: str
|
| 12 |
+
|
| 13 |
+
app = FastAPI(
|
| 14 |
+
title="Stupid TTS API",
|
| 15 |
+
description="A simple API to generate TTS audio using Google's gTTS",
|
| 16 |
+
version="1.0.0"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
@app.get("/")
|
| 20 |
+
async def read_root():
|
| 21 |
+
return {"message": "Welcome to the Stupid TTS API! Use POST /generate_audio to create audio."}
|
| 22 |
+
|
| 23 |
+
@app.post("/generate_audio")
|
| 24 |
+
async def generate_audio(audio_request: AudioRequest):
|
| 25 |
+
if not audio_request.request_id or not audio_request.text or not audio_request.language:
|
| 26 |
+
raise HTTPException(status_code=400, detail="All fields (request_id, text, language) are required")
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
output_dir = "audio"
|
| 30 |
+
output_file = f"{output_dir}/{audio_request.request_id}.mp3"
|
| 31 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 32 |
+
tts = gTTS(text=audio_request.text, lang=audio_request.language, slow=False)
|
| 33 |
+
tts.save(output_file)
|
| 34 |
+
return FileResponse(output_file, media_type="audio/mpeg", filename=f"{audio_request.request_id}.mp3")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
raise HTTPException(status_code=500, detail=f"Error generating audio: {str(e)}")
|