Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception handling #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions melo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ def audio_numpy_concat(segment_data_list, sr, speed=1.):
@staticmethod
def split_sentences_into_pieces(text, language, quiet=False):
texts = split_sentence(text, language_str=language)
if len(texts) == 0:
return ""

if not quiet:
print(" > Text split to sentences.")
print('\n'.join(texts))
print(" > ===========================")
print(text)
return texts

def tts_to_file(self, text, speaker_id, output_path=None, sdp_ratio=0.2, noise_scale=0.6, noise_scale_w=0.8, speed=1.0, pbar=None, format=None, position=None, quiet=False,):
Expand Down Expand Up @@ -138,6 +142,8 @@ def synthesize(self, text, speaker_id, output_path=None, sdp_ratio=0.2, noise_sc
texts = self.split_sentences_into_pieces(text, language, quiet)
audio_list = []
for t in texts:
if t is None:
continue
if language in ['EN', 'ZH_MIX_EN']:
t = re.sub(r'([a-z])([A-Z])', r'\1 \2', t)
device = self.device
Expand Down
63 changes: 63 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from melo.api import TTS
from fastapi import FastAPI
import uvicorn
import numpy as np
from pydantic import BaseModel
from base64 import b64encode, b64decode
from contextlib import asynccontextmanager
import torch
import torchaudio.transforms as T
from typing import Optional
import time
from utils import write_bytesIO

TTS_Server = None
speaker_ids = None
@asynccontextmanager
async def lifespan(app: FastAPI):
global TTS_Server
global speaker_ids
# Load the ML model
TTS_Server = TTS('EN')
speaker_ids = TTS_Server.hps.data.spk2id
yield
# Clean up the ML models and release the resources
TTS_Server.close()

class TTSResponse(BaseModel):
voice_id:str
text:str
sr:int
sdp_ratio:Optional[float] = 0.2
noise_scale:Optional[float] = 0.6
noise_scale_w:Optional[float] = 0.8
speed:Optional[float] = 1.0

app = FastAPI(lifespan=lifespan)



@app.post("/connection")
def tts_process(response:TTSResponse):
__t = time.time()
try:
audio, sr = TTS_Server.synthesize(response.text,speaker_id=speaker_ids[response.voice_id], sdp_ratio=response.sdp_ratio, noise_scale=response.noise_scale, noise_scale_w=response.noise_scale_w, speed=response.speed)
audio = torch.from_numpy(audio)
resampler = T.Resample(sr, response.sr, dtype=audio.dtype)
audio = resampler(audio)
audio = audio.detach().numpy()
files = write_bytesIO(response.sr,audio)
return {'audio': b64encode(files.read()).decode(),'sr':response.sr,"time":time.time() - __t}
except Exception as e:
print(f"Something went wrong {e}")
with open('empty.wav', 'rb') as f:
audio = f.read()
resampler = T.Resample(44100, response.sr, dtype=np.int16)
audio = audio.detach().numpy()
files = write_bytesIO(response.sr,audio)
return {'audio': b64encode(files.read()).decode(),'sr':response.sr,"time":time.time() - __t}



if __name__ == "__main__":
uvicorn.run("server:app",host='0.0.0.0',port=8000,reload=True)