-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
212 lines (171 loc) · 8.25 KB
/
app.py
File metadata and controls
212 lines (171 loc) · 8.25 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import tkinter as tk
from tkinter import filedialog
import DataProcessing as dp
# -*- coding: utf-8 -*-
# 预定义的字幕句子和文稿句子数组
subtitle_sentences=[]
manuscript_sentences=[]
subtitle_time_info=[]
# 打开待处理字幕文件并读取内容
def open_file_1():
file_path = filedialog.askopenfilename() # 弹出文件选择对话框
if file_path:
with open(file_path, 'r', encoding='utf-8') as file: # 指定utf-8编码读取文件
content = file.read()
text_box1.delete(1.0, tk.END) # 清空文本框内容
global subtitle_sentences,subtitle_time_info
# 提取字幕内容部分
subtitle_sentences = [subtitle[1] for subtitle in dp.extract_subtitles(content)]
# 提取字幕时间信息部分
subtitle_time_info = [subtitle[0] for subtitle in dp.extract_subtitles(content)]
#print(subtitle_time_info)
# 插入每个字幕句子到文本框
text_box1.insert(tk.END, "\n".join(subtitle_sentences) + "\n")
else:
return
# 打开原始稿件文本并读取内容 处理并展示分割后的文稿句子
def open_file_2():
file_path = filedialog.askopenfilename() # 弹出文件选择对话框
if file_path:
with open(file_path, 'r', encoding='utf-8') as file: # 指定utf-8编码读取文件
content = file.read()
text_box2.delete(1.0, tk.END) # 清空文本框内容
text_box2.insert(tk.END, content) # 将文件内容插入到文本框中
dp.Manuscript = content
global manuscript_sentences
if chk_var.get()==True:
#使用RWKV分句
manuscript_sentences = dp.chat_with_model(dp.messages, dp.chat_completions_api_url) #分割后的文稿句子数组
else:
#使用第三方模型分句
manuscript_sentences = dp.third_party_split(content,36,1)
text_box2.delete(1.0, tk.END) # 清空文本框内容
text_box2.insert(tk.END, "\n".join(manuscript_sentences or []) + "\n")
#print(manuscript_sentences) #打印文稿句子数组
if not file_path:
return
# 清空待处理字幕数组文本框
def clear_text():
text_box1.delete(1.0, tk.END)
app = tk.Tk() # 创建主窗口
app.title("Substitute 一款基于RWKV AI00Server的字幕AI替换工具") # 设置窗口标题
frame = tk.Frame(app) # 创建一个框架,用于放置按钮
frame.pack(pady=20, side=tk.RIGHT) # 将框架放置在右侧,并添加垂直间距
# 创建“选择文件1”按钮,点击时调用 open_file_1 函数
btn_file_1 = tk.Button(frame, text="选择.ass字幕文件", command=open_file_1)
btn_file_1.pack(pady=5) # 设置按钮的垂直间距
# 创建“选择文件2”按钮,点击时调用 open_file_2 函数
btn_file_2 = tk.Button(frame, text="选择已有文稿", command=open_file_2)
btn_file_2.pack(pady=5)
# 创建“清空待处理字幕数组”按钮,点击时调用 clear_text 函数
btn_clear = tk.Button(frame, text="Clear Text", command=clear_text)
btn_clear.pack(pady=5)
# 创建“退出”按钮,点击时退出程序
btn_exit = tk.Button(frame, text="Exit", command=app.quit)
btn_exit.pack(pady=5)
# 创建“RWKV分句”候选框,点击时勾选
chk_var = tk.BooleanVar()
chk_rwkv = tk.Checkbutton(frame, text="RWKV分句", variable=chk_var)
chk_rwkv.pack(pady=5)
# 创建 "合并重复字幕" 候选框,点击时勾选
chk_merge_duplicates = tk.BooleanVar()
chk_merge = tk.Checkbutton(frame, text="合并重复字幕", variable=chk_merge_duplicates)
chk_merge.pack(pady=5)
# 创建文本框,用于显示待处理字幕数组
text_box1 = tk.Text(app, wrap='word', width=50, height=15)
text_box1.pack(pady=20, side=tk.LEFT) # 将文本框放置在左侧,并添加垂直间距
# 创建文本框,用于显示原始稿件文本
text_box2 = tk.Text(app, wrap='word', width=50, height=15)
text_box2.pack(pady=20, side=tk.LEFT) # 将文本框放置在左侧,并添加垂直间距
def get_subtitles_embeddings():
subtitles_embeddings = []
for sentence in subtitle_sentences:
embedding = dp.get_embeds(sentence)
subtitles_embeddings.append((sentence, embedding)) # 向列表中创建并添加元组[(句子,嵌入式向量), ...]
return subtitles_embeddings
def get_manuscripts_embeddings():
manuscripts_embeddings = []
if manuscript_sentences is None:
return []
for sentence in manuscript_sentences:
embedding = dp.get_embeds(sentence)
manuscripts_embeddings.append((sentence, embedding))
#print(manuscripts_embeddings)
return manuscripts_embeddings
def get_similarity():
# 获取字幕和文稿的嵌入向量
subtitles_embeddings = get_subtitles_embeddings() # 返回 [(字幕句子, 嵌入向量), ...]
manuscripts_embeddings = get_manuscripts_embeddings() # 返回 [(文稿句子, 嵌入向量), ...]
# 存储结果:字幕句子、最相似的文稿句子、相似度
similar_pairs = []
# 遍历每个字幕句子及其嵌入向量
for subtitle_sentence, subtitle_embedding in subtitles_embeddings:
max_similarity = -1 # 初始化最大相似度
most_similar_sentence = None # 保存最相似的文稿句子
# 遍历每个文稿句子及其嵌入向量,计算相似度
for manuscript_sentence, manuscript_embedding in manuscripts_embeddings:
try:
# 计算余弦相似度
similarity = dp.cosine_similarity(subtitle_embedding, manuscript_embedding)
# 如果相似度大于最大相似度,则更新
if similarity > max_similarity:
max_similarity = similarity
most_similar_sentence = manuscript_sentence
except Exception as e:
print(f"Error calculating similarity: {e}")
# 将当前字幕句子及其最相似文稿句子和相似度添加到结果列表
similar_pairs.append((subtitle_sentence, most_similar_sentence, max_similarity))
return similar_pairs
##
# 创建新列表替换原字幕内容[(其他信息,新字幕),...]
##
def replace_subtitles():
# 用于存储替换后的字幕
replaced_subtitles = []
# 获取相似度计算结果
similar_pairs = get_similarity()
# 遍历每个字幕句子
for i, time_info in enumerate(subtitle_time_info):
subtitle, most_similar_sentence, similarity = similar_pairs[i]
# 如果最相似的文稿句子不为空
if most_similar_sentence is not None:
# 将字幕句子替换为最相似的文稿句子
replaced_subtitles.append((time_info, most_similar_sentence))
else:
# 如果最相似的文稿句子为空,则保留原字幕句子
replaced_subtitles.append((time_info, subtitle))
# 如果勾选了“合并重复字幕”
if chk_merge_duplicates.get():
replaced_subtitles = dp.merge_repeated_subtitles(replaced_subtitles)
return replaced_subtitles
##
# 保存替换后的字幕
##
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".ass", filetypes=[("ASS files", "*.ass"), ("All files", "*.*")])
if file_path:
replaced_subtitles = replace_subtitles()
with open(file_path, 'w', encoding='utf-8') as file:
# 写入subtitle_other_info作为文件开头
file.write(dp.other_info + "\n")
print(dp.other_info)
# 写入替换后的字幕内容
for time_info, new_subtitle in replaced_subtitles:
new_subtitle = new_subtitle.replace('\n', '') # 删除换行符
file.write(f"{time_info}{new_subtitle}\n")
def test():
#test1=get_manuscripts_embeddings()
test1=save_file()
#print(test1)
with open("test_output.txt", "w", encoding="utf-8") as file:
file.write(f"{test1}\n")
print(f"test end")
return
# 创建“保存文件”按钮,点击时调用 save_file 函数
#btn_save = tk.Button(frame, text="保存修改后的字幕文件", command=save_file)
#btn_save.pack(pady=5)
# 创建“测试”按钮,点击时执行测试程序
btn_test = tk.Button(frame, text="Test", command=test)
btn_test.pack(pady=5)
# 启动应用程序
app.mainloop()