-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonhandler.cpp
44 lines (36 loc) · 1.11 KB
/
pythonhandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "pythonhandler.h"
void PythonHandler::initPython()
{
Py_Initialize();
if(!Py_IsInitialized())
{
qDebug() << QT_DEBUG_OUTPUT << "Fail to initiallize python.";
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyRun_SimpleString("sys.path.append('./python/llm')");
PyRun_SimpleString("sys.path.append('./python/tts')");
}
PyObject* PythonHandler::getPyFunction(PyObject* pyFile, QString functionName)
{
PyObject* retObj = PyObject_GetAttrString(pyFile, functionName.toStdString().c_str());
if(!retObj)
{
qDebug() << QT_DEBUG_OUTPUT << "Fail to load function: " << functionName;
}
return retObj;
}
void PythonHandler::importMoudle(QString moudleName)
{
QString pySentence = "import " + moudleName;
PyRun_SimpleString(pySentence.toStdString().c_str());
}
void PythonHandler::importNecessaryPyMoudle()
{
importMoudle("os");
importMoudle("requests");
importMoudle("json");
PyRun_SimpleString("from requests.exceptions import JSONDecodeError");
importMoudle("edge_tts");
importMoudle("asyncio");
}