Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
-
import
|
| 4 |
-
import pkg_resources
|
| 5 |
-
import importlib.metadata
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
def get_package_version(package_name):
|
| 23 |
-
"""获取已安装包的版本"""
|
| 24 |
-
try:
|
| 25 |
-
# 使用 importlib.metadata 获取版本 (Python 3.8+)
|
| 26 |
-
return importlib.metadata.version(package_name)
|
| 27 |
-
except importlib.metadata.PackageNotFoundError:
|
| 28 |
-
try:
|
| 29 |
-
# 备选方法
|
| 30 |
-
return pkg_resources.get_distribution(package_name).version
|
| 31 |
-
except pkg_resources.DistributionNotFound:
|
| 32 |
-
return None
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
"""
|
| 36 |
-
print("="*50)
|
| 37 |
-
print("开始诊断 HF 环境中的 pip 安装问题 (修改版)")
|
| 38 |
-
print("="*50)
|
| 39 |
-
|
| 40 |
-
# 显示 Python 版本
|
| 41 |
-
print(f"Python 版本: {sys.version}")
|
| 42 |
-
|
| 43 |
-
# 显示 pip 版本
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
print("尝试更新 setuptools...")
|
| 52 |
-
install_package("setuptools", ["--upgrade"])
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if install_package("pysrt", ["--use-pep517"]):
|
| 57 |
-
print("成功使用 --use-pep517 安装 pysrt!")
|
| 58 |
-
else:
|
| 59 |
-
# 方法 2: 使用 egg 安装
|
| 60 |
-
print("\n方法 2: 尝试使用 egg 安装")
|
| 61 |
-
if install_package("pysrt", ["--egg"]):
|
| 62 |
-
print("成功使用 egg 安装 pysrt!")
|
| 63 |
-
else:
|
| 64 |
-
# 方法 3: 直接从 PyPI 获取源代码并安装
|
| 65 |
-
print("\n方法 3: 直接从源代码安装")
|
| 66 |
-
try:
|
| 67 |
-
import tempfile
|
| 68 |
-
import shutil
|
| 69 |
-
from urllib.request import urlretrieve
|
| 70 |
-
import tarfile
|
| 71 |
-
|
| 72 |
-
# 创建临时目录
|
| 73 |
-
temp_dir = tempfile.mkdtemp()
|
| 74 |
-
try:
|
| 75 |
-
# 下载源代码
|
| 76 |
-
tar_path = os.path.join(temp_dir, "pysrt.tar.gz")
|
| 77 |
-
urlretrieve("https://files.pythonhosted.org/packages/86/97/5ec8ae1f3e93414fb9c143c3b55a6b7c67caef9c89052c45b3222f2c6a36/pysrt-1.1.2.tar.gz", tar_path)
|
| 78 |
-
|
| 79 |
-
# 解压
|
| 80 |
-
with tarfile.open(tar_path) as tar:
|
| 81 |
-
tar.extractall(temp_dir)
|
| 82 |
-
|
| 83 |
-
# 找到 setup.py 所在的目录
|
| 84 |
-
setup_dir = os.path.join(temp_dir, "pysrt-1.1.2")
|
| 85 |
-
|
| 86 |
-
# 直接运行 setup.py install
|
| 87 |
-
subprocess.check_call([sys.executable, "setup.py", "install"], cwd=setup_dir)
|
| 88 |
-
print("成功从源代码安装 pysrt!")
|
| 89 |
-
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print(f"从源代码安装失败: {e}")
|
| 92 |
-
finally:
|
| 93 |
-
# 清理临时目录
|
| 94 |
-
shutil.rmtree(temp_dir)
|
| 95 |
-
except Exception as e:
|
| 96 |
-
print(f"方法 3 失败: {e}")
|
| 97 |
-
|
| 98 |
-
# 方法 4: 使用 pip install -e
|
| 99 |
-
print("\n方法 4: 尝试使用 pip install -e")
|
| 100 |
-
if install_package("git+https://github.com/byroot/pysrt.git", ["-e"]):
|
| 101 |
-
print("成功使用 -e 从 GitHub 安装 pysrt!")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
print("成功导入 pysrt 模块")
|
| 112 |
-
|
| 113 |
-
# 创建一个简单的字幕对象以验证功能
|
| 114 |
-
try:
|
| 115 |
-
from pysrt import SubRipFile, SubRipItem, SubRipTime
|
| 116 |
-
subs = SubRipFile()
|
| 117 |
-
item = SubRipItem(index=1,
|
| 118 |
-
start=SubRipTime(0, 0, 0),
|
| 119 |
-
end=SubRipTime(0, 0, 10),
|
| 120 |
-
text="测试字幕")
|
| 121 |
-
subs.append(item)
|
| 122 |
-
print("成功创建字幕对象,pysrt 功能正常")
|
| 123 |
-
except Exception as e:
|
| 124 |
-
print(f"创建字幕对象失败: {e}")
|
| 125 |
-
except ImportError as e:
|
| 126 |
-
print(f"导入 pysrt 失败: {e}")
|
| 127 |
-
else:
|
| 128 |
-
print("\n无法获取 pysrt 版本,安装可能失败")
|
| 129 |
|
| 130 |
-
|
| 131 |
-
print("诊断完成")
|
| 132 |
-
print("="*50)
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
主程序入口点 - 用于 Hugging Face Spaces
|
| 3 |
+
"""
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
+
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 确保能找到 install_pysrt 包
|
| 9 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
+
if current_dir not in sys.path:
|
| 11 |
+
sys.path.append(current_dir)
|
| 12 |
+
|
| 13 |
+
# 尝试导入并安装 pysrt
|
| 14 |
+
try:
|
| 15 |
+
import install_pysrt
|
| 16 |
+
installed = install_pysrt.is_pysrt_installed()
|
| 17 |
+
version = install_pysrt.get_pysrt_version() if installed else "未安装"
|
| 18 |
|
| 19 |
+
if not installed:
|
| 20 |
+
print("尝试安装 pysrt...")
|
| 21 |
+
success = install_pysrt.installer.install_pysrt()
|
| 22 |
+
if success:
|
| 23 |
+
version = install_pysrt.get_pysrt_version()
|
| 24 |
+
print(f"pysrt {version} 已成功安装")
|
| 25 |
+
else:
|
| 26 |
+
print("安装 pysrt 失败")
|
| 27 |
+
else:
|
| 28 |
+
print(f"pysrt {version} 已安装")
|
| 29 |
+
|
| 30 |
+
import_success = True
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"导入或安装 install_pysrt 时出错: {e}")
|
| 33 |
+
import_success = False
|
| 34 |
+
version = "错误"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
def create_subtitle(start_time, end_time, text):
|
| 38 |
+
"""创建字幕并返回结果"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
+
import pysrt
|
| 41 |
+
from pysrt import SubRipFile, SubRipItem, SubRipTime
|
| 42 |
+
|
| 43 |
+
# 解析时间
|
| 44 |
+
start_parts = [int(x) for x in start_time.split(':')]
|
| 45 |
+
end_parts = [int(x) for x in end_time.split(':')]
|
| 46 |
+
|
| 47 |
+
if len(start_parts) == 3:
|
| 48 |
+
start = SubRipTime(hours=start_parts[0], minutes=start_parts[1], seconds=start_parts[2])
|
| 49 |
+
else:
|
| 50 |
+
start = SubRipTime(minutes=start_parts[0], seconds=start_parts[1])
|
| 51 |
+
|
| 52 |
+
if len(end_parts) == 3:
|
| 53 |
+
end = SubRipTime(hours=end_parts[0], minutes=end_parts[1], seconds=end_parts[2])
|
| 54 |
+
else:
|
| 55 |
+
end = SubRipTime(minutes=end_parts[0], seconds=end_parts[1])
|
| 56 |
+
|
| 57 |
+
# 创建字幕文件
|
| 58 |
+
subs = SubRipFile()
|
| 59 |
+
item = SubRipItem(index=1, start=start, end=end, text=text)
|
| 60 |
+
subs.append(item)
|
| 61 |
+
|
| 62 |
+
# 转换为字符串
|
| 63 |
+
return str(subs), f"字幕已成功创建! pysrt 版本: {install_pysrt.get_pysrt_version()}"
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return "", f"创建字幕时出错: {e}"
|
| 66 |
+
|
| 67 |
+
# 创建 UI
|
| 68 |
+
with gr.Blocks(title="pysrt 演示") as demo:
|
| 69 |
+
gr.Markdown(f"""
|
| 70 |
+
# pysrt 字幕演示程序
|
| 71 |
|
| 72 |
+
当前 pysrt 状态: {"已安装 ✅" if import_success else "未安装 ❌"}
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
版本: {version}
|
| 75 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
start_time = gr.Textbox(label="开始时间 (分:秒 或 时:分:秒)", value="00:00")
|
| 80 |
+
end_time = gr.Textbox(label="结束时间 (分:秒 或 时:分:秒)", value="00:10")
|
| 81 |
+
text = gr.Textbox(label="字幕文本", value="这是一个测试字幕")
|
| 82 |
+
create_btn = gr.Button("创建字幕")
|
| 83 |
|
| 84 |
+
with gr.Column():
|
| 85 |
+
result = gr.Textbox(label="生成的 SRT 内容", lines=10)
|
| 86 |
+
status = gr.Textbox(label="状态")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
create_btn.click(fn=create_subtitle, inputs=[start_time, end_time, text], outputs=[result, status])
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
+
# 显示 pysrt 安装状态
|
| 92 |
+
print("-" * 50)
|
| 93 |
+
print(f"pysrt 安装状态: {'成功' if import_success else '失败'}")
|
| 94 |
+
if import_success:
|
| 95 |
+
print(f"pysrt 版本: {version}")
|
| 96 |
+
print("-" * 50)
|
| 97 |
+
|
| 98 |
+
# 启动 UI
|
| 99 |
+
demo.launch()
|