aistudio_TTS / server.py
Qi28's picture
Update server.py
b62bfca verified
# /home/aistudio/work
import os
import subprocess
import sys
import json
import threading
import time
import psutil
import socket
env = os.environ.copy()
port_and_commands_json = json.dumps([
{
'port': 501,#九月
'command': '-s /home/aistudio/work/tts-mode/九月/9yue_e8_s80.pth -g /home/aistudio/work/tts-mode/九月/9yue-e25.ckpt -dr /home/aistudio/work/tts-mode/九月/九月-古代的簪花之人,不只有富人小姐,还有翩翩少女。.wav -dt 古代的簪花之人,不只有富人小姐,还有翩翩少女。 -dl zh'
}
,
{
'port': 502,#贝拉
'command': '-s /home/aistudio/work/tts-mode/贝拉/贝拉-Bella_e51_s1632.pth -g /home/aistudio/work/tts-mode/贝拉/贝拉-Bella-e21.ckpt -dr /home/aistudio/work/tts-mode/贝拉/贝拉-哎就,很狼狈,然后我就,跟你们讲,你说这件事情呢有什么意义呢?也没有什么意义。.wav -dt 哎就,很狼狈,然后我就,跟你们讲,你说这件事情呢有什么意义呢?也没有什么意义。 -dl zh'
},
{
'port': 503,#蔡徐坤
'command': '-s /home/aistudio/work/tts-mode/蔡徐坤/蔡徐坤-Kun_e12_s180.pth -g /home/aistudio/work/tts-mode/蔡徐坤/蔡徐坤-Kun-e12.ckpt -dr /home/aistudio/work/tts-mode/蔡徐坤/蔡徐坤-全民制作人们大家好,我是练习时长两年半的个人练习生蔡徐坤。.wav -dt 全民制作人们大家好,我是练习时长两年半的个人练习生蔡徐坤。 -dl zh'
},
{
'port': 504,#陈泽
'command': '-s /home/aistudio/work/tts-mode/陈泽/陈泽-Ze_e60_s1500.pth -g /home/aistudio/work/tts-mode/陈泽/陈泽-Ze-e21.ckpt -dr /home/aistudio/work/tts-mode/陈泽/陈泽-材料二表达的内容就是,我骗我爸说在我妈那住骗我妈说在我爸那住利用了一个时间差玩儿了一手,时间蒙太奇。.wav -dt 材料二表达的内容就是,我骗我爸说在我妈那住骗我妈说在我爸那住利用了一个时间差玩儿了一手,时间蒙太奇。 -dl zh'
}
,
{
'port': 505,#电棍
'command': '-s /home/aistudio/work/tts-mode/电棍/电棍-otto_e39_s1638.pth -g /home/aistudio/work/tts-mode/电棍/电棍-otto-e10.ckpt -dr /home/aistudio/work/tts-mode/电棍/电棍-那不得累死啊,倒啥样我怎么开啊,八十八点六千瓦,通电了。.wav -dt 那不得累死啊,倒啥样我怎么开啊,八十八点六千瓦,通电了。 -dl zh'
}
,
{
'port': 506,#丁真
'command': '-s /home/aistudio/work/tts-mode/丁真/丁真-dingzhen_e29_s348.pth -g /home/aistudio/work/tts-mode/丁真/丁真-dingzhen-e15.ckpt -dr /home/aistudio/work/tts-mode/丁真/丁真-过去的一年里,我,去了很多地方,但我最主要的任务就是学习。.wav -dt 过去的一年里,我,去了很多地方,但我最主要的任务就是学习。 -dl zh'
}
,
{
'port': 507,#东雪莲
'command': '-s /home/aistudio/work/tts-mode/东雪莲/东雪莲-Azuma_e35_s1435.pth -g /home/aistudio/work/tts-mode/东雪莲/东雪莲-Azuma-e10.ckpt -dr /home/aistudio/work/tts-mode/东雪莲/东雪莲-几个月前,真的,非常突然他开始冷淡,包括但不限于已读不回消息看到我扭头就走我说什么他都不理我。.wav -dt 几个月前,真的,非常突然他开始冷淡,包括但不限于已读不回消息看到我扭头就走我说什么他都不理我。 -dl zh'
}
,
{
'port': 508,#珈乐
'command': '-s /home/aistudio/work/tts-mode/珈乐/珈乐-Carol_e40_s2160.pth -g /home/aistudio/work/tts-mode/珈乐/珈乐-Carol-e15.ckpt -dr /home/aistudio/work/tts-mode/珈乐/珈乐-我以前,在做那种,测试,很多很多年前做那种网上测试。.wav -dt 我以前,在做那种,测试,很多很多年前做那种网上测试。 -dl zh'
}
,
{
'port': 509,#嘉然
'command': '-s /home/aistudio/work/tts-mode/嘉然/嘉然-Diana_e52_s2444.pth -g /home/aistudio/work/tts-mode/嘉然/嘉然-Diana-e24.ckpt -dr /home/aistudio/work/tts-mode/嘉然/嘉然-想知道一下,哎呀,最近嘉心糖们平时就是喜欢干些什么...日常的娱乐项目啊或者是。.wav -dt 想知道一下,哎呀,最近嘉心糖们平时就是喜欢干些什么...日常的娱乐项目啊或者是。 -dl zh'
}
,
{
'port': 510,#米诺
'command': '-s /home/aistudio/work/tts-mode/米诺/米诺-Mino_e55_s3025.pth -g /home/aistudio/work/tts-mode/米诺/米诺-Mino-e28.ckpt -dr /home/aistudio/work/tts-mode/米诺/米诺-这么多,然后就是说你今天练!你练就是我们买菜回完家回到家就希望你看到你整个地上就最好练完了嘛。.wav -dt 这么多,然后就是说你今天练!你练就是我们买菜回完家回到家就希望你看到你整个地上就最好练完了嘛。 -dl zh'
}
])
# 转发端口
gradio_data = {
"gradio": {
"501": 501,
"502": 502,
"503": 503,
"504": 504,
"505": 505,
"506": 506,
"507": 507,
"508": 508,
"509": 509,
"510": 510,
}
}
os.makedirs(".webide", exist_ok=True)
file_path = os.path.join(".webide", "proxy_config.json")
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(gradio_data, file, ensure_ascii=False, indent=4)
print(f"数据已成功写入 {file_path}")
# 安装环境
subprocess.run([sys.executable, "-m", "pip", "install", "-U", "openi", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple"])
import openi
# os.system('y | conda install ffmpeg cmake')
# pip install -U gradio
# subprocess.run([sys.executable, "-m", "pip", "install", "-U", "LangSegment"])
#pip install LangSegment
# def AA():
# while True:
# time.sleep(10) # 每次迭代暂停1秒
# threading.Thread(target=AA).start()
if os.path.exists('/root/nltk_data') and os.path.isdir(folder_path):
print(f"文件夹 '/root/nltk_data' 存在。")
else:
print(f"文件夹 '/root/nltk_data' 不存在。")
os.system("rm -r /home/aistudio/nltk_data.zip")
openi.download_file(repo_id="qilan2/SD-QL",file="nltk_data.zip",cluster="NPU",save_path="/home/aistudio/",force=False)
os.system("unzip /home/aistudio/nltk_data.zip -d /root/")
os.system("rm -r /home/aistudio/nltk_data.zip")
def download_and_extract_gpt_sovits():
if not os.path.exists('/home/aistudio/work/GPT-SoVITS/api.py'):
print('GPT-SoVITS 文件夹异常')
# subprocess.run(['openi', 'dataset', 'download', 'qilan2/SD-QL', 'GPT-SoVITS2.zip', '--cluster', 'NPU', '--save_path', '/home/aistudio'])
# subprocess.run(['unzip', '/home/aistudio/GPT-SoVITS2.zip', '-d', '/home/aistudio/work/'])
subprocess.run(['unzip', '/home/aistudio/data/data308901/GPT-SoVITS2.zip', '-d', '/home/aistudio/work/'])
# os.remove('/home/aistudio/GPT-SoVITS2.zip')
print('GPT-SoVITS 文件夹已创建并解压完成.')
subprocess.run(['rm', '/home/aistudio/work/GPT-SoVITS/api.py'])
subprocess.run(['wget','-O','/home/aistudio/work/GPT-SoVITS/api.py', 'https://hf-mirror.com/datasets/Qi28/aistudio_TTS/resolve/main/api.py'])
if not os.path.exists('/home/aistudio/work/tts-mode/乃琳/乃琳Eileen-e20.ckpt'):
print('tts-mode 文件夹异常')
subprocess.run(['mkdir', '/home/aistudio/work/tts-mode'])
subprocess.run(['unzip', '/home/aistudio/data/data308901/tts-mode.zip', '-d', '/home/aistudio/work/tts-mode/'])
print('TTS-mode 文件夹已创建并解压完成.')
else:
print('文件夹已存在,无需下载和解压.')
def execute_command(port, command):
# 构造完整的命令字符串
full_command = f'python /home/aistudio/work/GPT-SoVITS/api.py -p {port} {command}'
# 执行命令并将其放入后台
print(f'Executing command on port {port}: {full_command}')
# 使用 os.system 启动进程
os.system(full_command ) # 在后台运行命令 + ' &'
def execute_commands(port_and_commands_json):
try:
# 解析 JSON 字符串
port_and_commands_list = json.loads(port_and_commands_json)
threads = [] # 用于存储线程对象
for item in port_and_commands_list:
port = item['port']
command = item['command']
# 创建并启动线程来执行命令
thread = threading.Thread(target=execute_command, args=(port, command))
thread.start()
threads.append(thread) # 将线程对象添加到列表
time.sleep(30) # 可选:控制启动新线程的频率
# # 等待所有线程完成(可选)
for thread in threads:
thread.join()
except json.JSONDecodeError as e:
print(f"Failed to decode JSON: {e}")
except Exception as e:
print(f"An error occurred: {e}")
def is_port_open(port):
"""检查指定端口是否开放。"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('127.0.0.1', port)) == 0
def run_api_script():
api_script = "/home/aistudio/work/GPT-SoVITS/api.py"
print("启动 api.py ...")
subprocess.Popen(['python', api_script]) # 使用 Popen 启动,不阻塞主线程
# 等待3分钟并检查端口是否开放
time.sleep(60) # 等待180秒(3分钟)
if is_port_open(9880): # 检查端口 9880 是否在监听
print("api.py 正在运行,不需要安装依赖。")
# # 停止这个正在运行的进程
# for proc in psutil.process_iter(attrs=['pid', 'name']):
# if "api.py" in proc.info['name']:
# stop_process(proc.info['pid'])
# break
else:
print("api.py 未能成功启动,需要安装依赖。")
install_dependencies()
def stop_process(pid):
"""停止指定 PID 的进程。"""
proc = psutil.Process(pid)
print(f"正在停止进程: {proc.name()} (PID: {proc.pid})")
proc.terminate() # 尝试正常终止
proc.wait(timeout=5) # 等待进程终止
print(f"进程 {proc.name()} 成功停止。")
def install_dependencies():
os.chdir('/home/aistudio/work/GPT-SoVITS/')
# os.system("echo 'y' | sh install.sh")
subprocess.run(['pip', 'install', '-r','requirements.txt'])
subprocess.run(['npm', 'install', '-g', 'localtunnel'])
subprocess.run(['pip', 'install', '-r','requirements.txt'])
def run_subprocesses_in_thread(port_and_commands):
thread = threading.Thread(target=run_subprocesses, args=(port_and_commands,), daemon=True)
thread.start()
download_and_extract_gpt_sovits()
subprocess.run(['rm','/home/aistudio/activate_port.py','/home/aistudio/work/GPT-SoVITS/api.py'])
subprocess.run(['wget','-O','/home/aistudio/activate_port.py', 'https://hf-mirror.com/datasets/Qi28/aistudio_TTS/resolve/main/activate_port.py'])
subprocess.run(['wget','-O','/home/aistudio/work/GPT-SoVITS/api.py', 'https://hf-mirror.com/datasets/Qi28/aistudio_TTS/resolve/main/api.py'])
os.chdir('/home/aistudio/work/GPT-SoVITS')
run_api_script() # 检测环境
execute_commands(port_and_commands_json)
subprocess.run(['python', '/home/aistudio/activate_port.py', str(7860)], env=env, capture_output=True, text=True)
# os.system('python /home/aistudio/work/GPT-SoVITS/api.py -s "/home/aistudio/work/tts-mode/ks/ks_e8_s72.pth" -g "/home/aistudio/work/tts-mode/ks/ks-e15.ckpt" -dr "/home/aistudio/work/tts-mode/ks/ks-又下雨又打雷,还下冰雹,我这边还下冰雹呢。.wav" -dt "又下雨又打雷,还下冰雹,我这边还下冰雹呢。" -dl "zh" -p "7860"')
# os.system('python /home/aistudio/work/GPT-SoVITS/api.py -s /home/aistudio/work/tts-mode/米诺/米诺-Mino_e55_s3025.pth -g /home/aistudio/work/tts-mode/米诺/米诺-Mino-e28.ckpt -dr /home/aistudio/work/tts-mode/米诺/米诺-这么多,然后就是说你今天练!你练就是我们买菜回完家回到家就希望你看到你整个地上就最好练完了嘛。.wav -dt 这么多,然后就是说你今天练!你练就是我们买菜回完家回到家就希望你看到你整个地上就最好练完了嘛。 -dl zh -p 510')
# subprocess.run('python /home/aistudio/activate_port.py 7863', shell=True)