This commit is contained in:
恍兮惚兮 2024-11-15 10:36:57 +08:00
parent cc23a18318
commit c055b23acd
49 changed files with 109 additions and 81 deletions

View File

@ -281,8 +281,8 @@ if __name__ == "__main__":
downloadBrotli()
downloadLocaleEmulator()
downloadNtlea()
downloadCurl()
if arch != "xp":
downloadCurl()
downloadOCRModel()
downloadcommon()
downloadbass()

View File

@ -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):

View File

@ -10,14 +10,29 @@ class ArgsEmptyExc(Exception):
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:

View File

@ -20,7 +20,7 @@ class OCR(baseocr):
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)]
@ -40,4 +40,4 @@ class OCR(baseocr):
try:
return res.json()["data"]["text_list"]
except:
raise Exception(res.maybejson)
raise Exception(res)

View File

@ -43,7 +43,7 @@ class OCR(baseocr):
]
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"])
@ -103,7 +103,7 @@ class OCR(baseocr):
]
return {"box": box, "text": text, "isocrtranslate": True}
except:
raise Exception(response.maybejson)
raise Exception(response)
@property
def srclangx(self):
@ -160,7 +160,7 @@ class OCR(baseocr):
try:
return resp.json()["access_token"]
except:
raise Exception(resp.maybejson)
raise Exception(resp)
def getaccess(self):
self.checkempty(["API Key", "Secret Key"])
@ -237,4 +237,4 @@ class OCR(baseocr):
]
return {"box": boxs, "text": texts}
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -2,6 +2,7 @@ from ocrengines.baseocrclass import baseocr
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):
@ -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):
@ -82,7 +83,7 @@ class OCR(baseocr):
)
return message
except:
raise Exception(response.maybejson)
raise Exception(response)
def createurl(self):
return createurl(self.config["apiurl"])

View File

@ -34,4 +34,4 @@ class OCR(baseocr):
try:
return response.json()["data"]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -3,6 +3,7 @@ import requests
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):
@ -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"]
@ -72,6 +73,6 @@ class OCR(baseocr):
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

View File

@ -42,4 +42,4 @@ class OCR(baseocr):
)
return {"box": boxs, "text": texts}
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -15,9 +15,9 @@ class OCR(baseocr):
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

View File

@ -72,4 +72,4 @@ class OCR(baseocr):
]
return {"box": boxs, "text": texts}
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -144,7 +144,7 @@ class OCR(baseocr):
]
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
@ -206,7 +206,7 @@ class OCR(baseocr):
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"]

View File

@ -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
@ -438,7 +436,7 @@ class Service(object):
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:

View File

@ -128,7 +128,7 @@ class OCR(baseocr):
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:

View File

@ -47,7 +47,7 @@ class OCR(baseocr):
"text": [l["words"] for l in response.json()["lines"]],
}
except:
raise Exception(response.maybejson)
raise Exception(response)
def ocrapi(self, imagebinary):
def truncate(q):
@ -93,7 +93,7 @@ class OCR(baseocr):
"text": [l["text"] for l in _],
}
except:
raise Exception(response.maybejson)
raise Exception(response)
def freetest_ts(self, imagebinary):
@ -129,7 +129,7 @@ class OCR(baseocr):
"isocrtranslate": True,
}
except:
raise Exception(response.maybejson)
raise Exception(response)
def ocrapi_ts(self, imagebinary):
@ -237,7 +237,7 @@ class OCR(baseocr):
]
return {"box": box, "text": text, "isocrtranslate": True}
except:
raise Exception(response.maybejson)
raise Exception(response)
def ocr(self, imagebinary):
interfacetype = self.config["interface"]

View File

@ -111,13 +111,6 @@ class ResponseBase:
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)

View File

@ -53,7 +53,7 @@ class TS(basetrans):
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})

View File

@ -22,7 +22,7 @@ class TS(basetrans):
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)]
@ -45,4 +45,4 @@ class TS(basetrans):
try:
return res.json()["data"]["text"]
except:
raise Exception(res.maybejson)
raise Exception(res)

View File

@ -146,7 +146,7 @@ class TS(basetrans):
"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})

View File

@ -79,5 +79,5 @@ class TS(basetrans):
try:
trans = data["data"]["translateText"]
except:
raise Exception(r.maybejson)
raise Exception(r)
return html.unescape(trans)

View File

@ -58,4 +58,4 @@ class TS(basetrans):
response = request.json()
return response["Data"]["Translated"]
except:
raise Exception(request.maybejson)
raise Exception(request)

View File

@ -38,4 +38,4 @@ class TS(basetrans):
try:
return response[0]["translations"][0]["text"]
except:
raise Exception(request.maybejson)
raise Exception(request)

View File

@ -2,6 +2,7 @@ import requests
import re
from translator.basetranslator import basetrans
import time, urllib
from myutils.commonbase import maybejson
class Tse:
@ -122,7 +123,7 @@ class BaiduV1(Tse):
try:
return "\n".join([item["dst"] for item in r.json()["data"]])
except:
raise Exception(r.maybejson)
raise Exception(maybejson(r))
class TS(basetrans):

View File

@ -60,7 +60,7 @@ class TS(basetrans):
try:
return response.json()["lan"]
except:
raise Exception(response.maybejson)
raise Exception(response)
def translate(self, query):

View File

@ -69,7 +69,7 @@ class TS(basetrans):
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", "密钥"])
@ -94,4 +94,4 @@ class TS(basetrans):
return "\n".join([_["dst"] for _ in res.json()["trans_result"]])
except:
raise Exception(res.maybejson)
raise Exception(res)

View File

@ -1,6 +1,7 @@
import re
import requests, time, urllib
from translator.basetranslator import basetrans
from myutils.commonbase import maybejson
class Tse:
@ -150,7 +151,7 @@ class Bing(Tse):
"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"]
)
@ -169,7 +170,7 @@ class Bing(Tse):
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):

View File

@ -121,4 +121,4 @@ class TS(basetrans):
try:
return decrypt(response.json()["target"])
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -28,4 +28,4 @@ class TS(basetrans):
try:
return response.json()["target"]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -85,7 +85,7 @@ class TS(basetrans):
response.json()["content"][0]["text"].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})

View File

@ -116,7 +116,7 @@ class TS(basetrans):
try:
message = response.json()["text"]
except:
raise Exception(response.maybejson)
raise Exception(response)
yield message
self.context.append({"role": "USER", "message": query})
self.context.append({"role": "CHATBOT", "message": message})

View File

@ -87,7 +87,7 @@ class TS(basetrans):
try:
return response.json()["data"]
except:
raise Exception(response.maybejson)
raise Exception(response)
def translate_deeplx_internal(self, translateText):
# Preparing the request data for the DeepL API

View File

@ -41,4 +41,4 @@ class TS(basetrans):
try:
return response.json()["translations"][0]["text"]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -59,7 +59,11 @@ class TS(basetrans):
message.append({"role": "model", "parts": [{"text": prefill}]})
contents = dict(contents=message)
usingstream = self.config["usingstream"]
payload = {**contents, **safety, **sys_message, **gen_config}
payload = {}
payload.update(contents)
payload.update(safety)
payload.update(sys_message)
payload.update(gen_config)
res = self.proxysession.post(
urlpathjoin(
self.config["BASE_URL"].strip(),
@ -85,7 +89,7 @@ class TS(basetrans):
try:
line = res.json()["candidates"][0]["content"]["parts"][0]["text"]
except:
raise Exception(res.maybejson)
raise Exception(res)
yield line
self.context.append({"role": "user", "parts": [{"text": query}]})
self.context.append({"role": "model", "parts": [{"text": line}]})

View File

@ -23,4 +23,4 @@ class TS(basetrans):
try:
return response.json()["data"]["translations"][0]["translatedText"]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -3,6 +3,7 @@ import json, requests, time, hmac, hashlib
from datetime import datetime, timezone
from myutils.utils import createurl, createenglishlangmap, urlpathjoin
from myutils.proxy import getproxy
from myutils.commonbase import maybejson
def list_models(typename, regist):
@ -19,7 +20,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 qianfanIAM:
@ -116,7 +117,7 @@ class gptcommon(basetrans):
) and (response.status_code != 200):
# application/json
# text/html
raise Exception(response.maybejson)
raise Exception(response)
for chunk in response.iter_lines():
response_data: str = chunk.decode("utf-8").strip()
if not response_data.startswith("data: "):
@ -148,7 +149,7 @@ class gptcommon(basetrans):
.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})

View File

@ -37,4 +37,4 @@ class TS(basetrans):
try:
return response.json()["translation"]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -332,7 +332,7 @@ class Service(object):
if resp.status_code == 200:
return json.dumps(resp.json())
else:
raise Exception(resp.maybejson)
raise Exception(resp)
def prepare_request(self, api_info, params, doseq=0):
for key in params:

View File

@ -314,7 +314,7 @@ class TS(basetrans):
try:
project_id = response.json()["projects"][0]["id"]
except:
raise Exception(response.maybejson)
raise Exception(response)
self.cacheproject[(end, ak, sk)] = project_id
project_id = self.cacheproject.get((end, ak, sk))
url = "https://{}/v1/{}/machine-translation/text-translation".format(

View File

@ -22,4 +22,4 @@ class TS(basetrans):
translation = result["translations"][0]["translation"]
return translation
except Exception as e:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -2,6 +2,7 @@ import requests
import re
import urllib
import time
from myutils.commonbase import maybejson
class Tse:
@ -124,7 +125,7 @@ class Itranslate(Tse):
try:
return r.json()["target"]["text"]
except:
raise Exception(r.maybejson)
raise Exception(maybejson(r))
from traceback import print_exc

View File

@ -159,7 +159,7 @@ class TS(basetrans):
try:
yield output.json()
except:
raise Exception(output.maybejson)
raise Exception(output)
def send_request_stream(self, messages, is_test=False, **kwargs):
extra_query = {
@ -195,7 +195,7 @@ class TS(basetrans):
if (not output.headers["Content-Type"].startswith("text/event-stream")) and (
output.status_code != 200
):
raise Exception(output.maybejson)
raise Exception(output)
for chunk in output.iter_lines():
response_data: str = chunk.decode("utf-8").strip()
if not response_data.startswith("data: "):

View File

@ -13,4 +13,4 @@ class TS(basetrans):
try:
return response.json()
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -75,4 +75,4 @@ class TS(basetrans):
try:
return ret.json()["Response"]["TargetText"]
except:
raise Exception(ret.maybejson)
raise Exception(ret)

View File

@ -38,4 +38,4 @@ class TS(basetrans):
try:
return response.json()["tgt_text"]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -8,12 +8,26 @@ class TS(basetrans):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 YaBrowser/24.1.5.825 Yowser/2.5 Safari/537.36"
}
if self.srclang == "auto":
params = {
"srv": "browser_video_translation",
"text": content,
}
response = self.proxysession.get(
"https://translate.yandex.net/api/v1/tr.json/detect",
params=params,
headers=headers,
)
lang = response.json()["lang"]
else:
lang = self.srclang
params = {
"lang": "{}-{}".format(self.srclang, self.tgtlang),
"lang": "{}-{}".format(lang, self.tgtlang),
"text": content,
"srv": "browser_video_translation",
}
url = "https://browser.translate.yandex.net/api/v1/tr.json/translate"
params = {**{"srv": "browser_video_translation"}, **params}
response = self.proxysession.post(
url=url,
params=params,
@ -24,4 +38,4 @@ class TS(basetrans):
try:
return response.json()["text"][0]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -21,4 +21,4 @@ class TS(basetrans):
try:
return response.json()["text"][0]
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -105,4 +105,4 @@ class TS(basetrans):
return res
except:
raise Exception(response.maybejson)
raise Exception(response)

View File

@ -45,4 +45,4 @@ class TS(basetrans):
try:
return r.json()["translation"][0]
except:
raise Exception(r.maybejson)
raise Exception(r)

View File

@ -85,7 +85,7 @@ class TS(basetrans):
]
)
except:
raise Exception(response.maybejson)
raise Exception(response)
def translate(self, content):
return self.translate10_9_8(content)