Spaces:
Runtime error
Runtime error
Calvin commited on
Commit ·
1b5d43e
1
Parent(s): 4ad1109
return audio
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
from gtts import gTTS
|
| 4 |
import aiofiles
|
| 5 |
import uvicorn
|
|
@@ -8,16 +7,7 @@ import time
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
# Pydantic model for JSON body
|
| 12 |
-
class TTSRequest(BaseModel):
|
| 13 |
-
text: str
|
| 14 |
-
lang: str = "id"
|
| 15 |
-
slow: bool = False
|
| 16 |
-
|
| 17 |
def generate_timestamps(script: str, wpm: int = 150):
|
| 18 |
-
"""
|
| 19 |
-
Generate naive timestamps per sentence based on words per minute (WPM).
|
| 20 |
-
"""
|
| 21 |
sentences = [s.strip() for s in script.replace("\n", " ").split(".") if s.strip()]
|
| 22 |
timestamps = []
|
| 23 |
current_time = 0.0
|
|
@@ -42,25 +32,38 @@ async def root():
|
|
| 42 |
return {"message": "API is running!"}
|
| 43 |
|
| 44 |
@app.post("/tts")
|
| 45 |
-
async def text_to_speech(
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
file_path = "output.mp3"
|
| 48 |
-
tts = gTTS(
|
| 49 |
tts.save(file_path)
|
| 50 |
|
| 51 |
-
# Read audio
|
| 52 |
async with aiofiles.open(file_path, mode="rb") as f:
|
| 53 |
audio_data = await f.read()
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
timestamps = generate_timestamps(request.text)
|
| 57 |
-
|
| 58 |
return {
|
| 59 |
-
"script":
|
| 60 |
-
"timestamps":
|
| 61 |
-
"
|
| 62 |
"size": len(audio_data)
|
| 63 |
}
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from gtts import gTTS
|
| 3 |
import aiofiles
|
| 4 |
import uvicorn
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def generate_timestamps(script: str, wpm: int = 150):
|
|
|
|
|
|
|
|
|
|
| 11 |
sentences = [s.strip() for s in script.replace("\n", " ").split(".") if s.strip()]
|
| 12 |
timestamps = []
|
| 13 |
current_time = 0.0
|
|
|
|
| 32 |
return {"message": "API is running!"}
|
| 33 |
|
| 34 |
@app.post("/tts")
|
| 35 |
+
async def text_to_speech(payload: dict):
|
| 36 |
+
text = payload.get("text", "")
|
| 37 |
+
lang = payload.get("lang", "en")
|
| 38 |
+
slow = payload.get("slow", False)
|
| 39 |
+
|
| 40 |
+
if not text:
|
| 41 |
+
return {"error": "Text is required"}
|
| 42 |
+
|
| 43 |
+
# Save audio file
|
| 44 |
file_path = "output.mp3"
|
| 45 |
+
tts = gTTS(text=text, lang=lang, slow=slow)
|
| 46 |
tts.save(file_path)
|
| 47 |
|
| 48 |
+
# Read audio for size
|
| 49 |
async with aiofiles.open(file_path, mode="rb") as f:
|
| 50 |
audio_data = await f.read()
|
| 51 |
|
| 52 |
+
# Build JSON response with download link
|
|
|
|
|
|
|
| 53 |
return {
|
| 54 |
+
"script": text,
|
| 55 |
+
"timestamps": generate_timestamps(text),
|
| 56 |
+
"file_url": f"/download/{os.path.basename(file_path)}",
|
| 57 |
"size": len(audio_data)
|
| 58 |
}
|
| 59 |
|
| 60 |
+
@app.get("/download/{filename}")
|
| 61 |
+
async def download_file(filename: str):
|
| 62 |
+
file_path = os.path.join(os.getcwd(), filename)
|
| 63 |
+
if os.path.exists(file_path):
|
| 64 |
+
from fastapi.responses import FileResponse
|
| 65 |
+
return FileResponse(file_path, media_type="audio/mpeg", filename=filename)
|
| 66 |
+
return {"error": "File not found"}
|
| 67 |
+
|
| 68 |
if __name__ == "__main__":
|
| 69 |
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|