Skip to content

Commit

Permalink
refine TTS (infiniflow#2500)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
  • Loading branch information
KevinHuSh authored Sep 19, 2024
1 parent 27f6b5b commit d6e7c62
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions api/apps/conversation_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ def tts():

def stream_audio():
try:
for chunk in tts_mdl.tts(text):
yield chunk
for txt in re.split(r"[,。/《》?;:!\n\r:;]+", text):
for chunk in tts_mdl.tts(txt):
yield chunk
except Exception as e:
yield ("data:" + json.dumps({"retcode": 500, "retmsg": str(e),
"data": {"answer": "**ERROR**: " + str(e)}},
Expand Down
17 changes: 10 additions & 7 deletions api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,27 @@ def set_api_key():
if msg:
return get_data_error_result(retmsg=msg)

llm = {
llm_config = {
"api_key": req["api_key"],
"api_base": req.get("base_url", "")
}
for n in ["model_type", "llm_name"]:
if n in req:
llm[n] = req[n]
llm_config[n] = req[n]

if not TenantLLMService.filter_update(
[TenantLLM.tenant_id == current_user.id, TenantLLM.llm_factory == factory], llm):
for llm in LLMService.query(fid=factory):
for llm in LLMService.query(fid=factory):
if not TenantLLMService.filter_update(
[TenantLLM.tenant_id == current_user.id,
TenantLLM.llm_factory == factory,
TenantLLM.llm_name == llm.llm_name],
llm_config):
TenantLLMService.save(
tenant_id=current_user.id,
llm_factory=factory,
llm_name=llm.llm_name,
model_type=llm.model_type,
api_key=req["api_key"],
api_base=req.get("base_url", "")
api_key=llm_config["api_key"],
api_base=llm_config["api_base"]
)

return get_json_result(data=True)
Expand Down
3 changes: 2 additions & 1 deletion rag/llm/tts_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def on_event(self, result: SpeechSynthesisResult):

class OpenAITTS(Base):
def __init__(self, key, model_name="tts-1", base_url="https://api.openai.com/v1"):
if not base_url: base_url="https://api.openai.com/v1"
self.api_key = key
self.model_name = model_name
self.base_url = base_url
Expand All @@ -181,6 +182,6 @@ def tts(self, text, voice="alloy"):

if response.status_code != 200:
raise Exception(f"**Error**: {response.status_code}, {response.text}")
for chunk in response.iter_content(chunk_size=1024):
for chunk in response.iter_content():
if chunk:
yield chunk

0 comments on commit d6e7c62

Please sign in to comment.