From 6cb3c6118fe7abeec3eeccaaeaf2bbeffddbb0eb Mon Sep 17 00:00:00 2001 From: owenchen Date: Wed, 17 Jun 2026 15:16:36 +0800 Subject: [PATCH] ow --- Properties/launchSettings.json | 2 +- Python/Python39Command.cs | 90 ++++++++++++++++++++++++++++++++++ Python/SoVITS.cs | 68 +++++++++++++++++++++++++ Task/Python/PythonRunTask.cs | 16 ++++++ appsettings.json | 5 ++ 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 Python/Python39Command.cs create mode 100644 Python/SoVITS.cs diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index c1cd06f..08fab86 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -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" } } } \ No newline at end of file diff --git a/Python/Python39Command.cs b/Python/Python39Command.cs new file mode 100644 index 0000000..9cbaf8a --- /dev/null +++ b/Python/Python39Command.cs @@ -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(); + + 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; + } + } +} diff --git a/Python/SoVITS.cs b/Python/SoVITS.cs new file mode 100644 index 0000000..0195d41 --- /dev/null +++ b/Python/SoVITS.cs @@ -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") + + */ \ No newline at end of file diff --git a/Task/Python/PythonRunTask.cs b/Task/Python/PythonRunTask.cs index 4a92a6a..93d7f6c 100644 --- a/Task/Python/PythonRunTask.cs +++ b/Task/Python/PythonRunTask.cs @@ -17,6 +17,22 @@ namespace CloudBuilder.Topshelf.Task public void Run(Dictionary 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 { diff --git a/appsettings.json b/appsettings.json index fd7f30a..4af1ed5 100644 --- a/appsettings.json +++ b/appsettings.json @@ -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是不需要用户权限而直接调用 },