This commit is contained in:
owenchen 2026-06-17 15:16:36 +08:00
parent a8789b2e42
commit 6cb3c6118f
5 changed files with 180 additions and 1 deletions

View File

@ -2,7 +2,7 @@
"profiles": {
"CloudBuilder.Topshelf": {
"commandName": "Project",
"commandLineArgs": "task:TtsTask book_id:B000010 from_chapter_id:181 to_chapter_id:200 speed:1 voice:Sherpa2 actor:韩立 actor_voice:Sherpa1 save_path:D:\\\\Net8\\\\FileServer\\\\Backup\\\\DmsFile\\\\voice"
"commandLineArgs": "task:PythonRunTask book_id:B000010 from_chapter_id:181 to_chapter_id:200 speed:1 voice:Sherpa2 actor:韩立 actor_voice:Sherpa1 save_path:D:\\\\Net8\\\\FileServer\\\\Backup\\\\DmsFile\\\\voice"
}
}
}

90
Python/Python39Command.cs Normal file
View File

@ -0,0 +1,90 @@
using CloudBuilder.Core.DatabaseAccessor.Entity;
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudBuilder.Topshelf.Python
{
public class Python39Command
{
private bool _disposed = false; // 释放标记
public dynamic PythonCreateScope { get; set; }
public IApplicationService ApplicationService;
public Python39Command(IApplicationService applicationService)
{
ApplicationService = applicationService;
Runtime.PythonDLL = applicationService.Configuration["Python39:PythonCommand"];
PythonEngine.PythonHome = applicationService.Configuration["Python39:PythonHome"];
PythonEngine.Initialize();
PythonCreateScope = Py.CreateScope();
}
public void Exec(string arg)
{
using (Py.GIL())
{
PythonCreateScope.Exec(arg);//"from pyhanlp import HanLP"
}
}
public string GetValueExec(string arg)
{
using (Py.GIL())
{
PythonCreateScope.Exec(string.Format("s_value={0}", arg));
var value = PythonCreateScope.Eval("s_value");
return value.ToString();
}
}
public string[] GetValuesExec(string arg)
{
using (Py.GIL())
{
PythonCreateScope.Exec(string.Format("s_list={0}", arg));
var value = PythonCreateScope.Eval("s_list");
string[] st = value.As<string[]>();
return st;
}
}
// 核心:释放 Python.NET 资源
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); // 阻止析构函数重复执行
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
// 1. 释放 Python 作用域
if (PythonCreateScope != null)
{
PythonCreateScope.Dispose();
PythonCreateScope = null;
}
// 2. 关闭 Python 运行时(关键!)
if (PythonEngine.IsInitialized)
{
AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", true);
PythonEngine.Shutdown(); // 销毁 Python 解释器
}
_disposed = true;
}
}
}

68
Python/SoVITS.cs Normal file
View File

@ -0,0 +1,68 @@
using Python.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudBuilder.Topshelf.Python
{
public class SoVITS
{
public readonly Python39Command python;
public SoVITS(Python39Command python)
{
this.python = python;
string file = python.ApplicationService.Configuration["Python39:PythonFile"]!;
if (!File.Exists(file)) return;
string init = File.ReadAllText(file, Encoding.UTF8);
using (Py.GIL())
{
python.Exec(init);
}
}
public void Generate(string text)
{
try
{
//必须定义方法的方式使用
using (Py.GIL())
{
string exec = $"api.generate(ref_wav_path=r\"{Path.Combine("F:\\GPT-SoVITS\\Template\\男青年\\douyihang", "douyihang-像一个被清空的文件夹,所有的喧嚣都暂时删除,只剩下空白的背景和呼吸的节奏.wav")}\",prompt_text=\"{",,"}\",text=\"{text}\",output_path=r\"{Path.Combine("F:\\GPT-SoVITS\\GPT-SoVITS-v2pro-20250604", "1404206091.mp3")}\")";
python.Exec(exec);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
/*
import os
import sys
import random
# Change to project directory這個是針對C#
os.chdir(r"F:\GPT-SoVITS\GPT-SoVITS-v2pro-20250604")
sys.path.insert(0, os.getcwd())
now_dir = os.getcwd()
sys.path.append(now_dir)
sys.path.append("%s/GPT_SoVITS" % (now_dir))
print(f"Current directory: {now_dir}")
print(f"Python path: {sys.executable}")
from gpt_sovits_api import GPTSoVITSAPI
print("\nInitializing GPT-SoVITS API...")
api = GPTSoVITSAPI(version="v2ProPlus")
*/

View File

@ -17,6 +17,22 @@ namespace CloudBuilder.Topshelf.Task
public void Run(Dictionary<string, string> bodyDict)
{
Python39Command python39Command = new Python39Command(service);
try
{
SoVITS soVITS = new SoVITS(python39Command);
soVITS.Generate("三更时分,青龙城毫无征兆地被血色浓雾笼罩,雾气带着诡异的幻术气息,城中低阶修士尽数陷入昏睡.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
python39Command.Dispose();
}
return;
PythonCommand python = new PythonCommand(service);
try
{

View File

@ -31,6 +31,11 @@
"salutations_path": "D:/Python38/hanlp/dictionary/salutations.txt",
"interlocutors_path": "D:/Python38/hanlp/dictionary/interlocutors.txt"
},
"Python39": {
"PythonCommand": "F:\\GPT-SoVITS\\GPT-SoVITS-v2pro-20250604\\runtime\\python39.dll",
"PythonHome": "F:\\GPT-SoVITS\\GPT-SoVITS-v2pro-20250604",
"PythonFile": "F:\\GPT-SoVITS\\GPT-SoVITS-v2pro-20250604\\GPT_SoVITS\\sovits.py"
},
"IApplicationService": {
"UserId": "000003" //Service
},