Calvin commited on
Commit
4ad1109
·
1 Parent(s): f1a1694
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from fastapi import FastAPI
 
2
  from gtts import gTTS
3
  import aiofiles
4
  import uvicorn
@@ -7,6 +8,12 @@ import time
7
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
10
  def generate_timestamps(script: str, wpm: int = 150):
11
  """
12
  Generate naive timestamps per sentence based on words per minute (WPM).
@@ -35,10 +42,10 @@ async def root():
35
  return {"message": "API is running!"}
36
 
37
  @app.post("/tts")
38
- async def text_to_speech(text: str):
39
  # Save TTS audio
40
- tts = gTTS(text)
41
  file_path = "output.mp3"
 
42
  tts.save(file_path)
43
 
44
  # Read audio file
@@ -46,10 +53,10 @@ async def text_to_speech(text: str):
46
  audio_data = await f.read()
47
 
48
  # Generate timestamps
49
- timestamps = generate_timestamps(text)
50
 
51
  return {
52
- "script": text,
53
  "timestamps": timestamps,
54
  "file": file_path,
55
  "size": len(audio_data)
 
1
  from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
  from gtts import gTTS
4
  import aiofiles
5
  import uvicorn
 
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).
 
42
  return {"message": "API is running!"}
43
 
44
  @app.post("/tts")
45
+ async def text_to_speech(request: TTSRequest):
46
  # Save TTS audio
 
47
  file_path = "output.mp3"
48
+ tts = gTTS(request.text, lang=request.lang, slow=request.slow)
49
  tts.save(file_path)
50
 
51
  # Read audio file
 
53
  audio_data = await f.read()
54
 
55
  # Generate timestamps
56
+ timestamps = generate_timestamps(request.text)
57
 
58
  return {
59
+ "script": request.text,
60
  "timestamps": timestamps,
61
  "file": file_path,
62
  "size": len(audio_data)