用Python代码调用GPT-SoVITS

GPT-SoVITS 项目地址:https://github.com/RVC-Boss/GPT-SoVITS

用户手册:https://www.yuque.com/baicaigongchang1145haoyuangong/ib3g1e

import os
import requests
from PySide6.QtCore import QThread
from PySide6.QtWidgets import QWidget, QApplication
from design import Ui_Form
import subprocess
import sounddevice as sd
import soundfile as sf
import psutil


class MyWindow(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.text_te.setFocus()

self.start_service_btn.clicked.connect(self.start_service_btn_clicked)
self.close_service_btn.clicked.connect(self.close_service_btn_clicked)
self.tts_btn.clicked.connect(self.tts_btn_clicked)

def start_service_btn_clicked(self):
thread = WorkerThread(self)
thread.start()

@staticmethod
def close_service_btn_clicked():
port = 9880
# 查找占用指定端口的进程
for conn in psutil.net_connections():
if conn.status == psutil.CONN_LISTEN and conn.laddr.port == port:
pid = conn.pid
try:
# 使用系统命令结束进程
subprocess.run(["taskkill", "/F", "/PID", str(pid)], check=True)
print(f"Process with PID {pid} using port {port} terminated.")
except subprocess.CalledProcessError as e:
print(f"Failed to terminate process with PID {pid}: {e}")
return
print(f"No process found using port {port}.")

def tts_btn_clicked(self):
params = {
"refer_wav_path": r"D:\BaiduNetdiskDownload\xl\哇,这么多人都在这里玩牌…他们的肚子会不会饿了呀?.wav",
"prompt_text": "哇,这么多人都在这里玩牌…他们的肚子会不会饿了呀?",
"prompt_language": "zh",
"text": self.text_te.toPlainText(),
"text_language": "zh"
}

response = requests.get('http://127.0.0.1:9880/', params=params)

file_path = 'temp.wav'
with open(file_path, 'wb') as f:
f.write(response.content)

data, samplerate = sf.read(file_path)
sd.play(data, samplerate)
sd.wait()


class WorkerThread(QThread):

def __init__(self, parent=None):
super().__init__(parent)

def run(self):
path = r"D:\BaiduNetdiskDownload\GPT-SoVITS-beta0706fix1\GPT-SoVITS-beta0706"
# 改变当前工作目录到.bat文件所在目录
os.chdir(path)
subprocess.run([r'runtime\python.exe', 'api.py', '-g', r"D:\BaiduNetdiskDownload\xl\xl-e10.ckpt", '-s', r"D:\BaiduNetdiskDownload\xl\xl_e15_s3195.pth"])


if __name__ == "__main__":
app = QApplication([])
app.setStyle("Fusion")
window = MyWindow()
window.show()
app.exec()