Skip to content

Commit

Permalink
fix: bugs and update
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed May 9, 2024
1 parent 1f7b6f2 commit b13d7f4
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 399 deletions.
48 changes: 0 additions & 48 deletions test2.py

This file was deleted.

343 changes: 88 additions & 255 deletions testcuda.py

Large diffs are not rendered by default.

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="v1.70"
VERSION_NUM=110700
VERSION="v1.72"
VERSION_NUM=110702
3 changes: 3 additions & 0 deletions videotrans/box/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ def run(self):
# 开始翻译,从目标文件夹读取原始字幕
set_process_box(text=f'start translate',type='logs')
config.box_trans = "ing"
if not self.files:
set_process_box(text="no srt file", type="error", func_name=self.func_name)
return
target = os.path.join(os.path.dirname(self.files[0]), '_translate')
os.makedirs(target, exist_ok=True)
for f in self.files:
Expand Down
2 changes: 1 addition & 1 deletion videotrans/set.ini
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ loop_backaudio=false
;Subtitle Recognition - GPU Improvement Reduced Performance Related
;从视频中识别字幕时的cuda数据类型,int8=消耗资源少,速度快,精度低,float32=消耗资源多,速度慢,精度高,int8_float16=设备自选
; cuda data type when recognizing subtitles from video, int8=consumes fewer resources, faster, lower precision, float32=consumes more resources, slower, higher precision, int8_float16=device of choice
cuda_com_type=float16
cuda_com_type=float32

;中文语言的视频时,用于识别的提示词,可解决简体识别为繁体问题。但注意,有可能直接会将提示词作为识别结果返回
;The prompt words used to recognize videos in Chinese language can solve the problem of recognizing simplified Chinese as traditional Chinese. But note that there is a possibility that the prompt word will be returned directly as the result of the recognition.
Expand Down
33 changes: 25 additions & 8 deletions videotrans/translator/azure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import os
import re
import time
import httpx
Expand All @@ -7,6 +8,26 @@
from videotrans.util import tools


shound_del=False
def update_proxy(type='set'):
global shound_del
if type=='del' and shound_del:
del os.environ['http_proxy']
del os.environ['https_proxy']
del os.environ['all_proxy']
shound_del=False
elif type=='set':
raw_proxy=os.environ.get('http_proxy')
print(f'当前代理:{raw_proxy=}')
if not raw_proxy:
proxy=tools.set_proxy()
if proxy:
print(f'设置代理:{proxy=}')
shound_del=True
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['all_proxy'] = proxy

def get_content(d,*,model=None,prompt=None):
message = [
{'role': 'system', 'content': "You are a professional, authentic translation engine, only returns translations."},
Expand Down Expand Up @@ -46,13 +67,7 @@ def trans(text_list, target_language="English", *, set_p=True,inst=None,stop=0,s
set_p:
是否实时输出日志,主界面中需要
"""
proxies = None
serv = tools.set_proxy()
if serv:
proxies = {
'http://': serv,
'https://': serv
}
update_proxy(type='set')

# 翻译后的文本
target_text = {"0":[]}
Expand Down Expand Up @@ -97,7 +112,7 @@ def trans(text_list, target_language="English", *, set_p=True,inst=None,stop=0,s
api_key=config.params["azure_key"],
api_version="2023-05-15",
azure_endpoint=config.params["azure_api"],
http_client=httpx.Client(proxies=proxies)
http_client=httpx.Client()
)


Expand Down Expand Up @@ -150,6 +165,8 @@ def trans(text_list, target_language="English", *, set_p=True,inst=None,stop=0,s
break


update_proxy(type='del')

if err:
config.logger.error(f'[AzureGPT]翻译请求失败:{err=}')
raise Exception(f'AzureGPT:{err}')
Expand Down
15 changes: 14 additions & 1 deletion videotrans/translator/baidu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import hashlib
import os
import time
import requests
from videotrans.configure import config
Expand All @@ -14,8 +15,13 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
set_p:
是否实时输出日志,主界面中需要
"""
proxy = os.environ.get('http_proxy')
if proxy:
del os.environ['http_proxy']
del os.environ['https_proxy']
del os.environ['all_proxy']

# 翻译后的文本
# 翻译后的文本
target_text = []
index = 0 # 当前循环需要开始的 i 数字,小于index的则跳过
iter_num = 0 # 当前循环次数,如果 大于 config.settings.retries 出错
Expand Down Expand Up @@ -107,6 +113,13 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
index=0 if i<=1 else i
else:
break

if proxy:
os.environ['http_proxy']=proxy
os.environ['https_proxy']=proxy
os.environ['all_proxy']=proxy


if err:
config.logger.error(f'[Baidu]翻译请求失败:{err=}')
raise Exception(f'百度翻译:{err}')
Expand Down
33 changes: 24 additions & 9 deletions videotrans/translator/chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import os
import re
import time
import httpx
Expand All @@ -19,21 +20,33 @@ def get_url(url=""):
return url+"/v1"
return url

shound_del=False
def update_proxy(type='set'):
global shound_del
if type=='del' and shound_del:
del os.environ['http_proxy']
del os.environ['https_proxy']
del os.environ['all_proxy']
shound_del=False
elif type=='set':
raw_proxy=os.environ.get('http_proxy')
print(f'当前代理:{raw_proxy=}')
if not raw_proxy:
proxy=tools.set_proxy()
if proxy:
print(f'设置代理:{proxy=}')
shound_del=True
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['all_proxy'] = proxy

def create_openai_client():
api_url = get_url(config.params['chatgpt_api'])
openai.base_url = api_url
config.logger.info(f'当前chatGPT:{api_url=}')
proxies=None
if not re.search(r'localhost',api_url) and not re.match(r'https?://(\d+\.){3}\d+',api_url):
serv = tools.set_proxy()
if serv:
proxies = {
'http://': serv,
'https://': serv
}
update_proxy(type='set')
try:
client = OpenAI(base_url=api_url,http_client=httpx.Client(proxies=proxies))
client = OpenAI(base_url=api_url,http_client=httpx.Client())
except Exception as e:
raise Exception(f'API={api_url},{str(e)}')
return client,api_url
Expand Down Expand Up @@ -175,6 +188,8 @@ def trans(text_list, target_language="English", *, set_p=True,inst=None,stop=0,s
else:
break

update_proxy(type='del')

if err:
config.logger.error(f'[ChatGPT]翻译请求失败:{err=}')
if err.lower().find("Connection error")>-1:
Expand Down
24 changes: 24 additions & 0 deletions videotrans/translator/deepl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
# -*- coding: utf-8 -*-
import os
import re
import time
import deepl

from videotrans.configure import config
from videotrans.util import tools

shound_del=False
def update_proxy(type='set'):
global shound_del
if type=='del' and shound_del:
del os.environ['http_proxy']
del os.environ['https_proxy']
del os.environ['all_proxy']
shound_del=False
elif type=='set':
raw_proxy=os.environ.get('http_proxy')
print(f'当前代理:{raw_proxy=}')
if not raw_proxy:
proxy=tools.set_proxy()
if proxy:
print(f'设置代理:{proxy=}')
shound_del=True
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['all_proxy'] = proxy

def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source_code=""):
"""
Expand All @@ -16,6 +36,7 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
set_p:
是否实时输出日志,主界面中需要
"""
update_proxy(type='set')
target_language='EN-US' if target_language=='EN' else target_language
# 翻译后的文本
target_text = []
Expand Down Expand Up @@ -83,6 +104,9 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
index=0 if i<=1 else i
else:
break

update_proxy(type='del')

if err:
config.logger.error(f'[DeepL]翻译请求失败:{err=}')
raise Exception(f'DeepL:{err}')
Expand Down
38 changes: 27 additions & 11 deletions videotrans/translator/deeplx.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
# -*- coding: utf-8 -*-
import os
import re
import time
import requests
from videotrans.configure import config
from videotrans.util import tools

shound_del=False
def update_proxy(type='set'):
global shound_del
if type=='del' and shound_del:
del os.environ['http_proxy']
del os.environ['https_proxy']
del os.environ['all_proxy']
shound_del=False
elif type=='set':
raw_proxy=os.environ.get('http_proxy')
print(f'当前代理:{raw_proxy=}')
if not raw_proxy:
proxy=tools.set_proxy()
if proxy:
print(f'设置代理:{proxy=}')
shound_del=True
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['all_proxy'] = proxy

def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source_code=""):
"""
Expand All @@ -18,15 +37,9 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
url=config.params['deeplx_address'].strip().rstrip('/').replace('/translate','')+'/translate'
if not url.startswith('http'):
url=f"http://{url}"
serv = tools.set_proxy()
proxies = None
if serv:
proxies = {
'http': serv,
'https': serv
}
if re.search(r'localhost',url) or re.match(r'https?://(\d+\.){3}\d+',url):
proxies={"http":"","https":""}

if not re.search(r'localhost',url) and not re.match(r'https?://(\d+\.){3}\d+',url):
update_proxy(type='set')
# 翻译后的文本
target_text = []
index = 0 # 当前循环需要开始的 i 数字,小于index的则跳过
Expand Down Expand Up @@ -70,7 +83,7 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
}
config.logger.info(f'[DeepLX]发送请求数据,{data=}')

response = requests.post(url=url, json=data, proxies=proxies)
response = requests.post(url=url, json=data)
config.logger.info(f'[DeepLX]返回响应,{response.text=}')
try:
result = response.json()
Expand Down Expand Up @@ -106,6 +119,9 @@ def trans(text_list, target_language="en", *, set_p=True,inst=None,stop=0,source
index=0 if i<=1 else i
else:
break

update_proxy(type='del')

if err:
config.logger.error(f'[DeepLX]翻译请求失败:{err=}')
raise Exception(f'DeepLX:{err}')
Expand Down
Loading

0 comments on commit b13d7f4

Please sign in to comment.