Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
UPLOAD_DIR = "audio"
|
| 10 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
@app.post("/echo")
|
| 13 |
+
async def echo(request: Request):
|
| 14 |
+
data = await request.json()
|
| 15 |
+
text = data.get("text", "")
|
| 16 |
+
|
| 17 |
+
if not text:
|
| 18 |
+
return {"error": "No 'text' provided in request."}
|
| 19 |
+
|
| 20 |
+
# Generate unique filename
|
| 21 |
+
filename = f"{uuid.uuid4().hex}.mp3"
|
| 22 |
+
filepath = os.path.join(UPLOAD_DIR, filename)
|
| 23 |
+
|
| 24 |
+
# Generate and save speech
|
| 25 |
+
tts = gTTS(text, lang='en')
|
| 26 |
+
tts.save(filepath)
|
| 27 |
+
|
| 28 |
+
# Return the audio file as response
|
| 29 |
+
return FileResponse(filepath, media_type="audio/mpeg", filename=filename)
|