From 28ccfbfc51068a663a80764e14074df5edf2b5ba Mon Sep 17 00:00:00 2001
From: kongdeqiang <kongdeqiang960204@163.com>
Date: 星期五, 13 三月 2026 17:41:41 +0800
Subject: [PATCH] 提交
---
funasr/utils/dynamic_import.py | 66 ++++++++++++++++++++++++++++----
1 files changed, 57 insertions(+), 9 deletions(-)
diff --git a/funasr/utils/dynamic_import.py b/funasr/utils/dynamic_import.py
index 2830cb2..e0a056b 100644
--- a/funasr/utils/dynamic_import.py
+++ b/funasr/utils/dynamic_import.py
@@ -1,13 +1,61 @@
-import importlib
+import importlib.util
+
+import importlib.util
+import inspect
+import os.path
+import sys
-def dynamic_import(import_path):
- """dynamic import module and class
-
- :param str import_path: syntax 'module_name:class_name'
- :return: imported class
+def load_module_from_path(file_path):
"""
+ 浠庣粰瀹氱殑鏂囦欢璺緞鍔ㄦ�佸姞杞芥ā鍧椼��
- module_name, objname = import_path.split(":")
- m = importlib.import_module(module_name)
- return getattr(m, objname)
+ :param file_path: 妯″潡鏂囦欢鐨勭粷瀵硅矾寰勩��
+ :return: 鍔犺浇鐨勬ā鍧�
+ """
+ module_name = file_path.split("/")[-1].replace(".py", "")
+ spec = importlib.util.spec_from_file_location(module_name, file_path)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+def import_module_from_path(file_path: str):
+
+ if file_path.startswith("http"):
+ from funasr.download.file import download_from_url
+
+ file_path = download_from_url(file_path)
+
+ file_dir = os.path.dirname(file_path)
+ # file_name = os.path.basename(file_path)
+ module_name = file_path.split("/")[-1].replace(".py", "")
+ if len(file_dir) < 1:
+ file_dir = "./"
+ sys.path.append(file_dir)
+ try:
+ importlib.import_module(module_name)
+ print(f"Loading remote code successfully: {file_path}")
+ except Exception as e:
+ print(f"Loading remote code failed: {file_path}, {e}")
+
+
+#
+# def load_module_from_path(module_name, file_path):
+# """
+# 浠庣粰瀹氱殑鏂囦欢璺緞鍔ㄦ�佸姞杞芥ā鍧椼��
+#
+# :param module_name: 鍔ㄦ�佸姞杞界殑妯″潡鐨勫悕绉般��
+# :param file_path: 妯″潡鏂囦欢鐨勭粷瀵硅矾寰勩��
+# :return: 鍔犺浇鐨勬ā鍧�
+# """
+# # 鍒涘缓鍔犺浇妯″潡鐨剆pec锛堣鏍硷級
+# spec = importlib.util.spec_from_file_location(module_name, file_path)
+#
+# # 鏍规嵁spec鍒涘缓妯″潡
+# module = importlib.util.module_from_spec(spec)
+#
+# # 鎵ц妯″潡鐨勪唬鐮佹潵瀹為檯鍔犺浇瀹�
+# spec.loader.exec_module(module)
+#
+# return module
--
Gitblit v1.9.1