ow
This commit is contained in:
1 parent
a8789b2e42
commit
6cb3c6118f
5 files changed
+180
-1
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
*/
|
||||
Reference in new issue
Block a user