Calvin commited on
Commit
679d5e9
·
1 Parent(s): b25b259

change importexit

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -1,19 +1,17 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import FileResponse
3
- from transformers import AutoProcessor, VitsModel
4
- import torch
5
- import soundfile as sf
6
  import aiofiles
7
- import uvicorn
8
  import os
 
 
9
 
10
  app = FastAPI()
11
 
12
- MODEL_NAME = "wibowo-id/vits-id"
13
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
14
 
15
- processor = AutoProcessor.from_pretrained(MODEL_NAME)
16
- model = VitsModel.from_pretrained(MODEL_NAME).to(device)
17
 
18
  @app.post("/tts")
19
  async def tts_api(payload: dict):
@@ -21,21 +19,16 @@ async def tts_api(payload: dict):
21
  if not text:
22
  return {"error": "Text is required"}
23
 
24
- inputs = processor(text, return_tensors="pt").to(device)
25
- with torch.no_grad():
26
- outputs = model(**inputs)
27
- audio = outputs.waveform.squeeze().cpu().numpy()
28
-
29
- file_path = "output.wav"
30
- sf.write(file_path, audio, samplerate=model.config.sampling_rate)
31
 
32
- async with aiofiles.open(file_path, "rb") as f:
33
  audio_data = await f.read()
34
 
35
  return {
36
  "text": text,
37
- "file_url": f"/download/{os.path.basename(file_path)}",
38
- "size": len(audio_data)
39
  }
40
 
41
  @app.get("/download/{filename}")
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import FileResponse
 
 
 
3
  import aiofiles
 
4
  import os
5
+ from TTS.api import TTS # install via: pip install TTS
6
+ import uvicorn
7
 
8
  app = FastAPI()
9
 
10
+ # Load the Tacotron2 + HiFi-GAN Indonesian model (from Coqui TTS hub)
11
+ # Model ID on HF: "tts_models/id/id_tts_tacotron2"
12
+ tts = TTS(model_name="tts_models/id/id_tts_tacotron2")
13
 
14
+ OUTPUT_FILE = "output.wav"
 
15
 
16
  @app.post("/tts")
17
  async def tts_api(payload: dict):
 
19
  if not text:
20
  return {"error": "Text is required"}
21
 
22
+ # Generate speech and save to OUTPUT_FILE
23
+ tts.tts_to_file(text=text, file_path=OUTPUT_FILE)
 
 
 
 
 
24
 
25
+ async with aiofiles.open(OUTPUT_FILE, "rb") as f:
26
  audio_data = await f.read()
27
 
28
  return {
29
  "text": text,
30
+ "file_url": f"/download/{os.path.basename(OUTPUT_FILE)}",
31
+ "size": len(audio_data),
32
  }
33
 
34
  @app.get("/download/{filename}")