积极的屁孩 commited on
Commit
39c1e1e
·
1 Parent(s): 677c37b

fix langsegment

Browse files
Files changed (1) hide show
  1. app.py +79 -27
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
  import sys
 
 
3
  import json
4
  import torch
5
  import gradio as gr
@@ -9,37 +11,87 @@ from huggingface_hub import snapshot_download, hf_hub_download
9
  import subprocess
10
  import re
11
 
12
- # 修复LangSegment包导入问题
13
- def fix_langsegment_imports():
14
  try:
15
- import LangSegment
16
- langsegment_init_path = os.path.join(os.path.dirname(LangSegment.__file__), '__init__.py')
17
-
18
- if os.path.exists(langsegment_init_path):
19
- with open(langsegment_init_path, 'r') as f:
20
- content = f.read()
21
-
22
- # 检查是否存在已废弃的导入
23
- if 'setLangfilters' in content or 'getLangfilters' in content:
24
- print("正在修复LangSegment包中的导入问题...")
25
- # 替换导入语句,删除setLangfilters和getLangfilters
26
- fixed_content = re.sub(
27
- r'from \.LangSegment import LangSegment,([^,]*),setLangfilters,getLangfilters',
28
- r'from .LangSegment import LangSegment,\1',
29
- content
30
- )
31
-
32
- # 写回文件
33
- with open(langsegment_init_path, 'w') as f:
34
- f.write(fixed_content)
35
- print("LangSegment包导入修复完成")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  else:
37
- print("LangSegment包无需修复")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  except Exception as e:
39
- print(f"修复LangSegment包时出错: {e}")
40
 
41
- # 在导入其他模块之前执行修复
42
- fix_langsegment_imports()
43
 
44
  # 克隆Amphion仓库
45
  if not os.path.exists("Amphion"):
 
1
  import os
2
  import sys
3
+ import importlib.util
4
+ import site
5
  import json
6
  import torch
7
  import gradio as gr
 
11
  import subprocess
12
  import re
13
 
14
+ def patch_langsegment_init():
 
15
  try:
16
+ # 尝试找到 LangSegment 包的位置
17
+ spec = importlib.util.find_spec("LangSegment")
18
+ if spec is None or spec.origin is None:
19
+ print("无法定位 LangSegment 包。")
20
+ return
21
+
22
+ # 构建 __init__.py 的路径
23
+ init_path = os.path.join(os.path.dirname(spec.origin), '__init__.py')
24
+
25
+ if not os.path.exists(init_path):
26
+ print(f"未找到 LangSegment 的 __init__.py 文件于: {init_path}")
27
+ # 尝试在 site-packages 中查找,适用于某些环境
28
+ for site_pkg_path in site.getsitepackages():
29
+ potential_path = os.path.join(site_pkg_path, 'LangSegment', '__init__.py')
30
+ if os.path.exists(potential_path):
31
+ init_path = potential_path
32
+ print(f"在 site-packages 中找到 __init__.py: {init_path}")
33
+ break
34
+ else: # 如果循环正常结束(没有 break)
35
+ print(f"在 site-packages 中也未找到 __init__.py")
36
+ return
37
+
38
+
39
+ print(f"尝试读取 LangSegment __init__.py: {init_path}")
40
+ with open(init_path, 'r') as f:
41
+ lines = f.readlines()
42
+
43
+ modified = False
44
+ new_lines = []
45
+ target_line_prefix = "from .LangSegment import"
46
+
47
+ for line in lines:
48
+ stripped_line = line.strip()
49
+ if stripped_line.startswith(target_line_prefix):
50
+ if 'setLangfilters' in stripped_line or 'getLangfilters' in stripped_line:
51
+ print(f"发现需要修改的行: {stripped_line}")
52
+ # 移除 setLangfilters 和 getLangfilters
53
+ modified_line = stripped_line.replace(',setLangfilters', '')
54
+ modified_line = modified_line.replace(',getLangfilters', '')
55
+ # 确保逗号处理正确 (例如,如果它们是末尾的项)
56
+ modified_line = modified_line.replace('setLangfilters,', '')
57
+ modified_line = modified_line.replace('getLangfilters,', '')
58
+ # 如果它们是唯一的额外导入,移除可能多余的逗号
59
+ modified_line = modified_line.rstrip(',')
60
+ new_lines.append(modified_line + '\n')
61
+ modified = True
62
+ print(f"修改后的行: {modified_line.strip()}")
63
+ else:
64
+ new_lines.append(line) # 行没问题,保留原样
65
  else:
66
+ new_lines.append(line) # 非目标行,保留原样
67
+
68
+ if modified:
69
+ print(f"尝试写回已修改的 LangSegment __init__.py 到: {init_path}")
70
+ try:
71
+ with open(init_path, 'w') as f:
72
+ f.writelines(new_lines)
73
+ print("LangSegment __init__.py 修改成功。")
74
+ # 尝试重新加载模块以使更改生效(可能无效,取决于导入链)
75
+ try:
76
+ import LangSegment
77
+ importlib.reload(LangSegment)
78
+ print("LangSegment 模块已尝试重新加载。")
79
+ except Exception as reload_e:
80
+ print(f"重新加载 LangSegment 时出错(可能无影响): {reload_e}")
81
+ except PermissionError:
82
+ print(f"错误:权限不足,无法修改 {init_path}。请考虑修改 requirements.txt。")
83
+ except Exception as write_e:
84
+ print(f"写入 LangSegment __init__.py 时发生其他错误: {write_e}")
85
+ else:
86
+ print("LangSegment __init__.py 无需修改。")
87
+
88
+ except ImportError:
89
+ print("未找到 LangSegment 包,无法进行修复。")
90
  except Exception as e:
91
+ print(f"修复 LangSegment 包时发生意外错误: {e}")
92
 
93
+ # 在所有其他导入(尤其是可能触发 LangSegment 导入的 Amphion)之前执行修复
94
+ patch_langsegment_init()
95
 
96
  # 克隆Amphion仓库
97
  if not os.path.exists("Amphion"):