Skip to content

Commit

Permalink
Fix: #596
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed Oct 29, 2024
1 parent fe4b08f commit 86fd06f
Show file tree
Hide file tree
Showing 28 changed files with 310 additions and 134 deletions.
6 changes: 4 additions & 2 deletions sp.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def run(self):
with open('./videotrans/styles/style.qss', 'r', encoding='utf-8') as f:
app.setStyleSheet(f.read())
try:

from videotrans.configure import config
from videotrans.mainwin._main_win import MainWindow
sets=QSettings("pyvideotrans", "settings")
w,h=int(self.width*0.85), int(self.height*0.85)
Expand All @@ -62,8 +64,8 @@ def run(self):
h=size.height()
except:
pass
mw=MainWindow(width=w, height=h)
mw.move(QPoint(int((self.width - w) / 2), int((self.height - h) / 2)))
config.MAINWIN=MainWindow(width=w, height=h)
config.MAINWIN.move(QPoint(int((self.width - w) / 2), int((self.height - h) / 2)))
except Exception as e:
import traceback
from PySide6.QtWidgets import QMessageBox
Expand Down
4 changes: 2 additions & 2 deletions videotrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

VERSION = "v2.94"
VERSION_NUM = 120094
VERSION = "v2.95"
VERSION_NUM = 120095
2 changes: 1 addition & 1 deletion videotrans/configure/_except.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def __init__(self, proxy=None,msg='',name=""):
self.name=name
def __str__(self):
if self.proxy and (self.proxy.startswith('http') or self.proxy.startswith('sock')):
return f'[{self.name}]: {self.msg} 当前代理地址 {self.proxy} 无法连接,请更换或尝试关闭代理' if config.defaulelang=='zh' else f'{self.msg} Current proxy address {self.proxy} cannot be connected, please replace or try to turn off the proxy'
return f'[{self.name}]: {self.msg}. 当前代理地址:{self.proxy}, 请更换代理' if config.defaulelang=='zh' else f'{self.msg} Current proxy address {self.proxy} cannot be connected, please replace or try to turn off the proxy'
return f'[{self.name}]: {self.msg} 当前IP受限或无法连接,请使用代理' if config.defaulelang=='zh' else f'{self.msg} Current IP is restricted or cannot be connected, please use proxy'

1 change: 1 addition & 0 deletions videotrans/configure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path
from queue import Queue

MAINWIN=None

# 获取程序执行目录
def _get_executable_path():
Expand Down
2 changes: 2 additions & 0 deletions videotrans/recognition/_overall.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def _exec(self):
jianfan=config.settings.get('zh_hant_s')
if not config.settings['rephrase'] or self.detect_language[:2]!='zh':
for i in list(raws):
if len(i['words'])<1:
continue
tmp={
'text':zhconv.convert(i['text'], 'zh-hans') if jianfan and self.detect_language[:2]=='zh' else i['text'],
'start_time':int(i['words'][0]['start']*1000),
Expand Down
7 changes: 5 additions & 2 deletions videotrans/task/_translate_srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ def trans(self):

def _check_target_sub(self,source_srt_list,target_srt_list):
for i,it in enumerate(source_srt_list):
if target_srt_list[i]['time']!=it['time']:
if i>=len(target_srt_list) or target_srt_list[i]['time']!=it['time']:
# 在 target_srt_list 的 索引 i 位置插入一个dict
tmp=copy.deepcopy(it)
tmp['text']=' '
target_srt_list.insert(i,tmp)
if i>=len(target_srt_list):
target_srt_list.append(tmp)
else:
target_srt_list.insert(i,tmp)
else:
target_srt_list[i]['line']=it['line']
return target_srt_list
Expand Down
7 changes: 5 additions & 2 deletions videotrans/task/trans_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,14 @@ def trans(self) -> None:

def _check_target_sub(self, source_srt_list, target_srt_list):
for i, it in enumerate(source_srt_list):
if target_srt_list[i]['time'] != it['time']:
if i>=len(target_srt_list) or target_srt_list[i]['time'] != it['time']:
# 在 target_srt_list 的 索引 i 位置插入一个dict
tmp = copy.deepcopy(it)
tmp['text'] = ' '
target_srt_list.insert(i, tmp)
if i>=len(target_srt_list):
target_srt_list.append(tmp)
else:
target_srt_list.insert(i, tmp)
else:
target_srt_list[i]['line'] = it['line']
self._save_srt_target(target_srt_list, self.cfg['target_sub'])
Expand Down
5 changes: 5 additions & 0 deletions videotrans/ui/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def setupUi(self, geminiform):
self.set_gemini = QtWidgets.QPushButton(geminiform)
self.set_gemini.setMinimumSize(QtCore.QSize(0, 35))
self.set_gemini.setObjectName("set_gemini")

self.test = QtWidgets.QPushButton(geminiform)
self.test.setObjectName("test")

help_btn = QtWidgets.QPushButton()
help_btn.setMinimumSize(QtCore.QSize(0, 35))
Expand All @@ -76,6 +79,7 @@ def setupUi(self, geminiform):
help_btn.clicked.connect(lambda: tools.open_url(url='https://pyvideotrans.com/gemini'))

h3.addWidget(self.set_gemini)
h3.addWidget(self.test)
h3.addWidget(help_btn)
v1.addLayout(h3)

Expand All @@ -90,6 +94,7 @@ def retranslateUi(self, geminiform):
self.label_4.setText(
"{lang}代表目标语言名称,不要删除。" if config.defaulelang == 'zh' else "{lang} represents the target language name, do not delete it.")
self.set_gemini.setText('保存' if config.defaulelang == 'zh' else "Save")
self.test.setText('测试' if config.defaulelang == 'zh' else "Test")
self.gemini_key.setPlaceholderText("secret key")
self.label_2.setText("Gemini Key ")
self.label_3.setText('选择模型' if config.defaulelang == 'zh' else "Select model")
26 changes: 26 additions & 0 deletions videotrans/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,3 +1745,29 @@ def clean_srt(srt):
# 行号前添加换行符
srt=re.sub(r'\s?(\d+)\s+?(\d+:\d+:\d+)',r"\n\n\1\n\2",srt)
return srt.strip().replace('&#39;', '"').replace('&quot;', "'")


def check_local_api(api):
from PySide6 import QtWidgets,QtGui
# 创建消息框
msg_box = QtWidgets.QMessageBox()
msg_box.setIcon(QtWidgets.QMessageBox.Critical)
msg_box.setText("API url error:")
msg_box.setWindowTitle(config.transobj['anerror'])


# 设置窗口图标
icon = QtGui.QIcon(f"{config.ROOT_DIR}/videotrans/styles/icon.ico") # 替换为你的图标路径
msg_box.setWindowIcon(icon)

# 显示消息框

if not api:
msg_box.setInformativeText('必须填写http地址' if config.defaulelang == 'zh' else 'Must fill in the http address')
msg_box.exec()
return False
if api.find('0.0.0.0:')>-1:
msg_box.setInformativeText('请将 0.0.0.0 改为 127.0.0.1 ' if config.defaulelang == 'zh' else 'Please change 0.0.0.0 to 127.0.0.1. ')
msg_box.exec()
return False
return True
25 changes: 15 additions & 10 deletions videotrans/winform/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

from videotrans import translator
from videotrans.configure import config


# set chatgpt
from videotrans.util import tools



def openwin():
class TestChatgpt(QThread):
uito = Signal(str)
Expand Down Expand Up @@ -40,35 +38,42 @@ def feed(d):

def test():
key = winobj.chatgpt_key.text()
api = winobj.chatgpt_api.text().strip()
api = api if api else 'https://api.openai.com/v1'
url = winobj.chatgpt_api.text().strip()
url = url if url else 'https://api.openai.com/v1'
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
model = winobj.chatgpt_model.currentText()
template = winobj.chatgpt_template.toPlainText()

os.environ['OPENAI_API_KEY'] = key
config.params["chatgpt_key"] = key
config.params["chatgpt_api"] = api
config.params["chatgpt_api"] = url
config.params["chatgpt_model"] = model
config.params["chatgpt_template"] = template

task = TestChatgpt(parent=winobj)
winobj.test_chatgpt.setText('测试中请稍等...' if config.defaulelang == 'zh' else 'Testing...')
task.uito.connect(feed)
task.start()
winobj.test_chatgpt.setText('测试中请稍等...' if config.defaulelang == 'zh' else 'Testing...')

def save_chatgpt():
key = winobj.chatgpt_key.text()
api = winobj.chatgpt_api.text().strip()
api = api if api else 'https://api.openai.com/v1'
url = winobj.chatgpt_api.text().strip()
url = url if url else 'https://api.openai.com/v1'
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
model = winobj.chatgpt_model.currentText()
template = winobj.chatgpt_template.toPlainText()
with Path(tools.get_prompt_file('chatgpt')).open('w', encoding='utf-8') as f:
f.write(template)
f.flush()
os.environ['OPENAI_API_KEY'] = key
config.params["chatgpt_key"] = key
config.params["chatgpt_api"] = api
config.params["chatgpt_api"] = url
config.params["chatgpt_model"] = model
config.params["chatgpt_template"] = template
config.getset_params(config.params)
Expand Down
30 changes: 14 additions & 16 deletions videotrans/winform/chattts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

from videotrans import tts
from videotrans.configure import config


# 使用内置的 open 函数
from videotrans.util import tools



def openwin():
class TestTTS(QThread):
uito = Signal(str)
Expand Down Expand Up @@ -40,15 +38,13 @@ def feed(d):
winobj.test.setText('测试' if config.defaulelang == 'zh' else 'Test')

def test():
if not winobj.chattts_address.text().strip():
QtWidgets.QMessageBox.critical(winobj, config.transobj['anerror'], '必须填写http地址')

url = winobj.chattts_address.text().strip()
if tools.check_local_api(url) is not True:
return
apiurl = winobj.chattts_address.text().strip()
if not apiurl:
return QtWidgets.QMessageBox.critical(winobj, config.transobj['anerror'],
'必须填写api地址' if config.defaulelang == 'zh' else 'Please input ChatTTS API url')

config.params['chattts_api'] = apiurl
if not url.startswith('http'):
url = 'http://' + url
config.params['chattts_api'] = url
task = TestTTS(parent=winobj,
text="你好啊我的朋友"
)
Expand All @@ -57,12 +53,14 @@ def test():
task.start()

def save():
key = winobj.chattts_address.text().strip()
url = winobj.chattts_address.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
url = url.rstrip('/').replace('/tts', '')
voice = winobj.chattts_voice.text().strip()
if key:
key = key.rstrip('/')
key = 'http://' + key.replace('http://', '').replace('/tts', '')
config.params["chattts_api"] = key
config.params["chattts_api"] = url
config.getset_params(config.params)
config.settings['chattts_voice'] = voice
with open(config.ROOT_DIR + "/videotrans/cfg.json", 'w', encoding='utf-8') as f:
Expand Down
21 changes: 12 additions & 9 deletions videotrans/winform/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ def feed(d):
winobj.test.setText('测试' if config.defaulelang == 'zh' else 'Test')

def test():
if not winobj.clone_address.text().strip():
QtWidgets.QMessageBox.critical(winobj, config.transobj['anerror'], '必须填写http地址')
url = winobj.clone_address.text().strip()
if tools.check_local_api(url) is not True:
return

config.params['clone_api'] = winobj.clone_address.text().strip()
if not url.startswith('http'):
url = 'http://' + url
config.params['clone_api'] = url
task = TestTTS(parent=winobj,
text="你好啊我的朋友" if config.defaulelang == 'zh' else 'hello,my friend'
, language="zh-cn" if config.defaulelang == 'zh' else 'en')
Expand All @@ -53,11 +54,13 @@ def test():
task.start()

def save():
key = winobj.clone_address.text().strip()
if key:
key = key.rstrip('/')
key = 'http://' + key.replace('http://', '')
config.params["clone_api"] = key
url = winobj.clone_address.text().strip()
if tools.check_local_api(url) is not True:
return
url = url.rstrip('/')
if not url.startswith('http'):
url = 'http://' + url
config.params["clone_api"] = url
config.getset_params(config.params)
tools.set_process(text='clone', type="refreshtts")
winobj.close()
Expand Down
13 changes: 10 additions & 3 deletions videotrans/winform/cosyvoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def feed(d):
winobj.test.setText('测试api')

def test():
url = winobj.api_url.text()
url = winobj.api_url.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
config.params["cosyvoice_url"] = url
task = TestTTS(parent=winobj,
text="你好啊我的朋友",
Expand All @@ -46,8 +50,11 @@ def test():
task.start()

def save():
url = winobj.api_url.text()

url = winobj.api_url.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
role = winobj.role.toPlainText().strip()

config.params["cosyvoice_url"] = url
Expand Down
17 changes: 11 additions & 6 deletions videotrans/winform/deepLX.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from videotrans import translator
from videotrans.configure import config

from videotrans.util import tools

def openwin():
class TestTask(QThread):
Expand Down Expand Up @@ -32,11 +32,12 @@ def feed(d):
winobj.test.setText('测试' if config.defaulelang == 'zh' else 'Test')

def test():
url = winobj.deeplx_address.text()
url = winobj.deeplx_address.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
key = winobj.deeplx_key.text().strip()
if not url:
return QtWidgets.QMessageBox.critical(winobj, config.transobj['anerror'],
'必须填写 api 地址' if config.defaulelang == 'zh' else 'Please input api url')

config.params["deeplx_address"] = url
config.params["deeplx_key"] = key
Expand All @@ -48,7 +49,11 @@ def test():
task.start()

def save():
url = winobj.deeplx_address.text()
url = winobj.deeplx_address.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
key = winobj.deeplx_key.text().strip()
config.params["deeplx_address"] = url
config.params["deeplx_key"] = key
Expand Down
12 changes: 10 additions & 2 deletions videotrans/winform/f5tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def feed(d):
winobj.test.setText('测试api')

def test():
url = winobj.api_url.text()
url = winobj.api_url.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
model = winobj.model.currentText()
role = winobj.role.toPlainText().strip()
if not role:
Expand Down Expand Up @@ -84,7 +88,11 @@ def getrole():
return role

def save():
url = winobj.api_url.text()
url = winobj.api_url.text().strip()
if tools.check_local_api(url) is not True:
return
if not url.startswith('http'):
url = 'http://' + url
role = winobj.role.toPlainText().strip()
model = winobj.model.currentText()

Expand Down
Loading

0 comments on commit 86fd06f

Please sign in to comment.