Skip to content

Commit

Permalink
some
Browse files Browse the repository at this point in the history
  • Loading branch information
HIllya51 committed Nov 15, 2024
1 parent cc23a18 commit c055b23
Show file tree
Hide file tree
Showing 49 changed files with 109 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/build_lunatranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ def downloadbass():
downloadBrotli()
downloadLocaleEmulator()
downloadNtlea()
downloadCurl()
if arch != "xp":
downloadCurl()
downloadOCRModel()
downloadcommon()
downloadbass()
Expand Down
2 changes: 0 additions & 2 deletions .github/scripts/collectall_xp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def copycheck(src, tgt):
print(src, tgt, os.path.exists(src))
if not os.path.exists(src):
return
if src.lower().endswith("_ssl.pyd"):
return
if not os.path.exists(tgt):
os.makedirs(tgt, exist_ok=True)
if os.path.isdir(src):
Expand Down
19 changes: 17 additions & 2 deletions py/LunaTranslator/myutils/commonbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ def __init__(self, valuelist) -> None:
super().__init__(" , ".join(valuelist) + getlanguagespace() + _TR("不能为空"))


class maybejson:
def __init__(self, _) -> None:
self._ = _

def __str__(self):
try:
return str(self._.json())
except:
return self._.text

def __getattr__(self, t):
return getattr(self._, t)


class proxysession(requests.Session):
def __init__(self, _key1, _key2):
super().__init__()
self.proxyconf = _key1, _key2

def request(self, *args, **kwargs):
def request(self, *args, **kwargs) -> maybejson:
kwargs["proxies"] = getproxy(self.proxyconf)
return super().request(*args, **kwargs)

return maybejson(super().request(*args, **kwargs))


class commonbase:
Expand Down
4 changes: 2 additions & 2 deletions py/LunaTranslator/ocrengines/_feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def check(self):
try:
token = res.json()["tenant_access_token"]
except:
raise Exception(res.maybejson)
raise Exception(res)
self.tokens[(app_id, app_secret)] = token
return self.tokens[(app_id, app_secret)]

Expand All @@ -40,4 +40,4 @@ def ocr(self, imagebinary):
try:
return res.json()["data"]["text_list"]
except:
raise Exception(res.maybejson)
raise Exception(res)
8 changes: 4 additions & 4 deletions py/LunaTranslator/ocrengines/baiduocr_X.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def ocr_ts1(self, imagebinary):
]
return {"box": box, "text": text, "isocrtranslate": True}
except:
raise Exception(response.maybejson)
raise Exception(response)

def ocr_ts2(self, imagebinary):
self.checkempty(["app_id", "app_key"])
Expand Down Expand Up @@ -103,7 +103,7 @@ def get_md5(string, encoding="utf-8"):
]
return {"box": box, "text": text, "isocrtranslate": True}
except:
raise Exception(response.maybejson)
raise Exception(response)

@property
def srclangx(self):
Expand Down Expand Up @@ -160,7 +160,7 @@ def get_access_token(self, API_KEY, SECRET_KEY):
try:
return resp.json()["access_token"]
except:
raise Exception(resp.maybejson)
raise Exception(resp)

def getaccess(self):
self.checkempty(["API Key", "Secret Key"])
Expand Down Expand Up @@ -237,4 +237,4 @@ def ocr_x(self, imagebinary):
]
return {"box": boxs, "text": texts}
except:
raise Exception(response.maybejson)
raise Exception(response)
5 changes: 3 additions & 2 deletions py/LunaTranslator/ocrengines/chatgptlike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import base64, requests
from myutils.utils import createurl, createenglishlangmap, urlpathjoin
from myutils.proxy import getproxy
from myutils.commonbase import maybejson


def list_models(typename, regist):
Expand All @@ -18,7 +19,7 @@ def list_models(typename, regist):
try:
return sorted([_["id"] for _ in resp.json()["data"]])
except:
raise Exception(resp.maybejson)
raise Exception(maybejson(resp))


class OCR(baseocr):
Expand Down Expand Up @@ -82,7 +83,7 @@ def ocr(self, imagebinary):
)
return message
except:
raise Exception(response.maybejson)
raise Exception(response)

def createurl(self):
return createurl(self.config["apiurl"])
2 changes: 1 addition & 1 deletion py/LunaTranslator/ocrengines/docsumo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def ocr(self, imagebinary):
try:
return response.json()["data"]
except:
raise Exception(response.maybejson)
raise Exception(response)
7 changes: 4 additions & 3 deletions py/LunaTranslator/ocrengines/geminiocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ocrengines.baseocrclass import baseocr
from myutils.utils import createenglishlangmap, urlpathjoin
from myutils.proxy import getproxy
from myutils.commonbase import maybejson


def list_models(typename, regist):
Expand All @@ -14,7 +15,7 @@ def list_models(typename, regist):
try:
models = resp.json()["models"]
except:
raise Exception(resp.maybejson)
raise Exception(maybejson(resp))
mm = []
for m in models:
name: str = m["name"]
Expand Down Expand Up @@ -72,6 +73,6 @@ def ocr(self, imagebinary):
if response.status_code == 200:
return response.json()["candidates"][0]["content"]["parts"][0]["text"]
else:
raise Exception(response.maybejson)
raise Exception(response)
except Exception as e:
raise Exception(response.maybejson) from e
raise Exception(response) from e
2 changes: 1 addition & 1 deletion py/LunaTranslator/ocrengines/googlecloudvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def ocr(self, imagebinary):
)
return {"box": boxs, "text": texts}
except:
raise Exception(response.maybejson)
raise Exception(response)
4 changes: 2 additions & 2 deletions py/LunaTranslator/ocrengines/mangaocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def ocr(self, imagebinary):
absolute_img_path = os.path.abspath(fname)
params = {"image_path": absolute_img_path}

response = requests.get("http://127.0.0.1:{}/image".format(self.port), params=params)
response = self.proxysession.get("http://127.0.0.1:{}/image".format(self.port), params=params)
os.remove(absolute_img_path)
try:
return response.json()["text"]
except Exception as e:
raise Exception(response.maybejson) from e
raise Exception(response) from e
2 changes: 1 addition & 1 deletion py/LunaTranslator/ocrengines/ocrspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ def ocr(self, imagebinary):
]
return {"box": boxs, "text": texts}
except:
raise Exception(response.maybejson)
raise Exception(response)
4 changes: 2 additions & 2 deletions py/LunaTranslator/ocrengines/txocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def getHash(message):
]
return {"box": boxs, "text": texts, "isocrtranslate": True}
except:
raise Exception(r.maybejson)
raise Exception(r)

def langmap(self):
# https://cloud.tencent.com/document/product/866/33526
Expand Down Expand Up @@ -206,7 +206,7 @@ def ocr_ocr(self, imagebinary):
texts = [_["DetectedText"] for _ in r.json()["Response"]["TextDetections"]]
return {"box": boxs, "text": texts}
except:
raise Exception(r.maybejson)
raise Exception(r)

def ocr(self, imagebinary):
interfacetype = self.config["interface"]
Expand Down
6 changes: 2 additions & 4 deletions py/LunaTranslator/ocrengines/volcengine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from collections import OrderedDict
import requests


from myutils.commonbase import maybejson
import requests
from urllib.parse import urlencode
from functools import reduce
Expand Down Expand Up @@ -438,7 +436,7 @@ def post(self, api, params, form, proxy):
if resp.status_code == 200:
return resp.text
else:
raise Exception(resp.maybejson)
raise Exception(maybejson(resp))

def prepare_request(self, api_info, params, doseq=0):
for key in params:
Expand Down
2 changes: 1 addition & 1 deletion py/LunaTranslator/ocrengines/xunfei.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def assemble_ws_auth_url(requset_url, method="POST", api_key="", api_secret=""):
renew_text = response.json()["payload"]["ocr_output_text"]["text"]
finalResult = json.loads(str(base64.b64decode(renew_text), "utf-8"))
except:
raise Exception(response.maybejson)
raise Exception(response)
try:
res = finalResult["pages"][0]
if "lines" not in res:
Expand Down
8 changes: 4 additions & 4 deletions py/LunaTranslator/ocrengines/youdaocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def freetest(self, imagebinary):
"text": [l["words"] for l in response.json()["lines"]],
}
except:
raise Exception(response.maybejson)
raise Exception(response)

def ocrapi(self, imagebinary):
def truncate(q):
Expand Down Expand Up @@ -93,7 +93,7 @@ def encrypt(signStr):
"text": [l["text"] for l in _],
}
except:
raise Exception(response.maybejson)
raise Exception(response)

def freetest_ts(self, imagebinary):

Expand Down Expand Up @@ -129,7 +129,7 @@ def freetest_ts(self, imagebinary):
"isocrtranslate": True,
}
except:
raise Exception(response.maybejson)
raise Exception(response)

def ocrapi_ts(self, imagebinary):

Expand Down Expand Up @@ -237,7 +237,7 @@ def readFileAsBase64(imagebinary):
]
return {"box": box, "text": text, "isocrtranslate": True}
except:
raise Exception(response.maybejson)
raise Exception(response)

def ocr(self, imagebinary):
interfacetype = self.config["interface"]
Expand Down
7 changes: 0 additions & 7 deletions py/LunaTranslator/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ def charset(self):
charset = m.group(1) if m else "utf-8"
return charset

@property
def maybejson(self):
try:
return self.json()
except:
return self.text

def json(self):
return json.loads(self.text)

Expand Down
2 changes: 1 addition & 1 deletion py/LunaTranslator/translator/_baiduqianfan.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def commonparseresponse(self, query, response: requests.ResponseBase, usingstrea
try:
message = response.json()["result"].replace("\n\n", "\n").strip()
except:
raise Exception(response.maybejson)
raise Exception(response)
yield message
self.context.append({"role": "user", "content": query})
self.context.append({"role": "assistant", "content": message})
Expand Down
4 changes: 2 additions & 2 deletions py/LunaTranslator/translator/_feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def check(self):
try:
token = res.json()["tenant_access_token"]
except:
raise Exception(res.maybejson)
raise Exception(res)
self.tokens[(app_id, app_secret)] = token
return self.tokens[(app_id, app_secret)]

Expand All @@ -45,4 +45,4 @@ def translate(self, query):
try:
return res.json()["data"]["text"]
except:
raise Exception(res.maybejson)
raise Exception(res)
2 changes: 1 addition & 1 deletion py/LunaTranslator/translator/_txhunyuan.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def translate(self, query):
"Content"
]
except:
raise Exception(response.maybejson)
raise Exception(response)
yield message
self.context.append({"Role": "user", "Content": query})
self.context.append({"Role": "assistant", "Content": message})
2 changes: 1 addition & 1 deletion py/LunaTranslator/translator/ali.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ def translate(self, content):
try:
trans = data["data"]["translateText"]
except:
raise Exception(r.maybejson)
raise Exception(r)
return html.unescape(trans)
2 changes: 1 addition & 1 deletion py/LunaTranslator/translator/aliyunapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def translate(self, query):
response = request.json()
return response["Data"]["Translated"]
except:
raise Exception(request.maybejson)
raise Exception(request)
2 changes: 1 addition & 1 deletion py/LunaTranslator/translator/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def translate(self, query):
try:
return response[0]["translations"][0]["text"]
except:
raise Exception(request.maybejson)
raise Exception(request)
3 changes: 2 additions & 1 deletion py/LunaTranslator/translator/baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
from translator.basetranslator import basetrans
import time, urllib
from myutils.commonbase import maybejson


class Tse:
Expand Down Expand Up @@ -122,7 +123,7 @@ def baidu_api(
try:
return "\n".join([item["dst"] for item in r.json()["data"]])
except:
raise Exception(r.maybejson)
raise Exception(maybejson(r))


class TS(basetrans):
Expand Down
2 changes: 1 addition & 1 deletion py/LunaTranslator/translator/baidu_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def detectlang(self, query):
try:
return response.json()["lan"]
except:
raise Exception(response.maybejson)
raise Exception(response)

def translate(self, query):

Expand Down
4 changes: 2 additions & 2 deletions py/LunaTranslator/translator/baiduapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def translate_bce(self, q):
try:
return "\n".join([_["dst"] for _ in r.json()["result"]["trans_result"]])
except:
raise Exception(r.maybejson)
raise Exception(r)

def translate_fy(self, q):
self.checkempty(["APP ID", "密钥"])
Expand All @@ -94,4 +94,4 @@ def translate_fy(self, q):
return "\n".join([_["dst"] for _ in res.json()["trans_result"]])

except:
raise Exception(res.maybejson)
raise Exception(res)
5 changes: 3 additions & 2 deletions py/LunaTranslator/translator/bing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import requests, time, urllib
from translator.basetranslator import basetrans
from myutils.commonbase import maybejson


class Tse:
Expand Down Expand Up @@ -150,7 +151,7 @@ def bing_api(
"to": to_language,
"tryFetchingGenderDebiasedTranslations": "true",
}
form_data = {**form_data, **self.tk}
form_data.update(self.tk)
api_url_param = "?isVertical=1&&IG={}&IID={}".format(
self.ig_iid["ig"], self.ig_iid["iid"]
)
Expand All @@ -169,7 +170,7 @@ def bing_api(
try:
return data[0] if is_detail_result else data[0]["translations"][0]["text"]
except:
raise Exception(r.maybejson)
raise Exception(maybejson(r))


class TS(basetrans):
Expand Down
Loading

0 comments on commit c055b23

Please sign in to comment.