mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-29 16:44:13 +08:00
some
This commit is contained in:
parent
cc23a18318
commit
c055b23acd
2
.github/scripts/build_lunatranslator.py
vendored
2
.github/scripts/build_lunatranslator.py
vendored
@ -281,8 +281,8 @@ if __name__ == "__main__":
|
|||||||
downloadBrotli()
|
downloadBrotli()
|
||||||
downloadLocaleEmulator()
|
downloadLocaleEmulator()
|
||||||
downloadNtlea()
|
downloadNtlea()
|
||||||
downloadCurl()
|
|
||||||
if arch != "xp":
|
if arch != "xp":
|
||||||
|
downloadCurl()
|
||||||
downloadOCRModel()
|
downloadOCRModel()
|
||||||
downloadcommon()
|
downloadcommon()
|
||||||
downloadbass()
|
downloadbass()
|
||||||
|
2
.github/scripts/collectall_xp.py
vendored
2
.github/scripts/collectall_xp.py
vendored
@ -14,8 +14,6 @@ def copycheck(src, tgt):
|
|||||||
print(src, tgt, os.path.exists(src))
|
print(src, tgt, os.path.exists(src))
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
return
|
return
|
||||||
if src.lower().endswith("_ssl.pyd"):
|
|
||||||
return
|
|
||||||
if not os.path.exists(tgt):
|
if not os.path.exists(tgt):
|
||||||
os.makedirs(tgt, exist_ok=True)
|
os.makedirs(tgt, exist_ok=True)
|
||||||
if os.path.isdir(src):
|
if os.path.isdir(src):
|
||||||
|
@ -10,14 +10,29 @@ class ArgsEmptyExc(Exception):
|
|||||||
super().__init__(" , ".join(valuelist) + getlanguagespace() + _TR("不能为空"))
|
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):
|
class proxysession(requests.Session):
|
||||||
def __init__(self, _key1, _key2):
|
def __init__(self, _key1, _key2):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.proxyconf = _key1, _key2
|
self.proxyconf = _key1, _key2
|
||||||
|
|
||||||
def request(self, *args, **kwargs):
|
def request(self, *args, **kwargs) -> maybejson:
|
||||||
kwargs["proxies"] = getproxy(self.proxyconf)
|
kwargs["proxies"] = getproxy(self.proxyconf)
|
||||||
return super().request(*args, **kwargs)
|
|
||||||
|
return maybejson(super().request(*args, **kwargs))
|
||||||
|
|
||||||
|
|
||||||
class commonbase:
|
class commonbase:
|
||||||
|
@ -20,7 +20,7 @@ class OCR(baseocr):
|
|||||||
try:
|
try:
|
||||||
token = res.json()["tenant_access_token"]
|
token = res.json()["tenant_access_token"]
|
||||||
except:
|
except:
|
||||||
raise Exception(res.maybejson)
|
raise Exception(res)
|
||||||
self.tokens[(app_id, app_secret)] = token
|
self.tokens[(app_id, app_secret)] = token
|
||||||
return self.tokens[(app_id, app_secret)]
|
return self.tokens[(app_id, app_secret)]
|
||||||
|
|
||||||
@ -40,4 +40,4 @@ class OCR(baseocr):
|
|||||||
try:
|
try:
|
||||||
return res.json()["data"]["text_list"]
|
return res.json()["data"]["text_list"]
|
||||||
except:
|
except:
|
||||||
raise Exception(res.maybejson)
|
raise Exception(res)
|
||||||
|
@ -43,7 +43,7 @@ class OCR(baseocr):
|
|||||||
]
|
]
|
||||||
return {"box": box, "text": text, "isocrtranslate": True}
|
return {"box": box, "text": text, "isocrtranslate": True}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def ocr_ts2(self, imagebinary):
|
def ocr_ts2(self, imagebinary):
|
||||||
self.checkempty(["app_id", "app_key"])
|
self.checkempty(["app_id", "app_key"])
|
||||||
@ -103,7 +103,7 @@ class OCR(baseocr):
|
|||||||
]
|
]
|
||||||
return {"box": box, "text": text, "isocrtranslate": True}
|
return {"box": box, "text": text, "isocrtranslate": True}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def srclangx(self):
|
def srclangx(self):
|
||||||
@ -160,7 +160,7 @@ class OCR(baseocr):
|
|||||||
try:
|
try:
|
||||||
return resp.json()["access_token"]
|
return resp.json()["access_token"]
|
||||||
except:
|
except:
|
||||||
raise Exception(resp.maybejson)
|
raise Exception(resp)
|
||||||
|
|
||||||
def getaccess(self):
|
def getaccess(self):
|
||||||
self.checkempty(["API Key", "Secret Key"])
|
self.checkempty(["API Key", "Secret Key"])
|
||||||
@ -237,4 +237,4 @@ class OCR(baseocr):
|
|||||||
]
|
]
|
||||||
return {"box": boxs, "text": texts}
|
return {"box": boxs, "text": texts}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -2,6 +2,7 @@ from ocrengines.baseocrclass import baseocr
|
|||||||
import base64, requests
|
import base64, requests
|
||||||
from myutils.utils import createurl, createenglishlangmap, urlpathjoin
|
from myutils.utils import createurl, createenglishlangmap, urlpathjoin
|
||||||
from myutils.proxy import getproxy
|
from myutils.proxy import getproxy
|
||||||
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
def list_models(typename, regist):
|
def list_models(typename, regist):
|
||||||
@ -18,7 +19,7 @@ def list_models(typename, regist):
|
|||||||
try:
|
try:
|
||||||
return sorted([_["id"] for _ in resp.json()["data"]])
|
return sorted([_["id"] for _ in resp.json()["data"]])
|
||||||
except:
|
except:
|
||||||
raise Exception(resp.maybejson)
|
raise Exception(maybejson(resp))
|
||||||
|
|
||||||
|
|
||||||
class OCR(baseocr):
|
class OCR(baseocr):
|
||||||
@ -82,7 +83,7 @@ class OCR(baseocr):
|
|||||||
)
|
)
|
||||||
return message
|
return message
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def createurl(self):
|
def createurl(self):
|
||||||
return createurl(self.config["apiurl"])
|
return createurl(self.config["apiurl"])
|
||||||
|
@ -34,4 +34,4 @@ class OCR(baseocr):
|
|||||||
try:
|
try:
|
||||||
return response.json()["data"]
|
return response.json()["data"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -3,6 +3,7 @@ import requests
|
|||||||
from ocrengines.baseocrclass import baseocr
|
from ocrengines.baseocrclass import baseocr
|
||||||
from myutils.utils import createenglishlangmap, urlpathjoin
|
from myutils.utils import createenglishlangmap, urlpathjoin
|
||||||
from myutils.proxy import getproxy
|
from myutils.proxy import getproxy
|
||||||
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
def list_models(typename, regist):
|
def list_models(typename, regist):
|
||||||
@ -14,7 +15,7 @@ def list_models(typename, regist):
|
|||||||
try:
|
try:
|
||||||
models = resp.json()["models"]
|
models = resp.json()["models"]
|
||||||
except:
|
except:
|
||||||
raise Exception(resp.maybejson)
|
raise Exception(maybejson(resp))
|
||||||
mm = []
|
mm = []
|
||||||
for m in models:
|
for m in models:
|
||||||
name: str = m["name"]
|
name: str = m["name"]
|
||||||
@ -72,6 +73,6 @@ class OCR(baseocr):
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return response.json()["candidates"][0]["content"]["parts"][0]["text"]
|
return response.json()["candidates"][0]["content"]["parts"][0]["text"]
|
||||||
else:
|
else:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(response.maybejson) from e
|
raise Exception(response) from e
|
||||||
|
@ -42,4 +42,4 @@ class OCR(baseocr):
|
|||||||
)
|
)
|
||||||
return {"box": boxs, "text": texts}
|
return {"box": boxs, "text": texts}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -15,9 +15,9 @@ class OCR(baseocr):
|
|||||||
absolute_img_path = os.path.abspath(fname)
|
absolute_img_path = os.path.abspath(fname)
|
||||||
params = {"image_path": absolute_img_path}
|
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)
|
os.remove(absolute_img_path)
|
||||||
try:
|
try:
|
||||||
return response.json()["text"]
|
return response.json()["text"]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(response.maybejson) from e
|
raise Exception(response) from e
|
||||||
|
@ -72,4 +72,4 @@ class OCR(baseocr):
|
|||||||
]
|
]
|
||||||
return {"box": boxs, "text": texts}
|
return {"box": boxs, "text": texts}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -144,7 +144,7 @@ class OCR(baseocr):
|
|||||||
]
|
]
|
||||||
return {"box": boxs, "text": texts, "isocrtranslate": True}
|
return {"box": boxs, "text": texts, "isocrtranslate": True}
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(r)
|
||||||
|
|
||||||
def langmap(self):
|
def langmap(self):
|
||||||
# https://cloud.tencent.com/document/product/866/33526
|
# https://cloud.tencent.com/document/product/866/33526
|
||||||
@ -206,7 +206,7 @@ class OCR(baseocr):
|
|||||||
texts = [_["DetectedText"] for _ in r.json()["Response"]["TextDetections"]]
|
texts = [_["DetectedText"] for _ in r.json()["Response"]["TextDetections"]]
|
||||||
return {"box": boxs, "text": texts}
|
return {"box": boxs, "text": texts}
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(r)
|
||||||
|
|
||||||
def ocr(self, imagebinary):
|
def ocr(self, imagebinary):
|
||||||
interfacetype = self.config["interface"]
|
interfacetype = self.config["interface"]
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import requests
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@ -438,7 +436,7 @@ class Service(object):
|
|||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
return resp.text
|
return resp.text
|
||||||
else:
|
else:
|
||||||
raise Exception(resp.maybejson)
|
raise Exception(maybejson(resp))
|
||||||
|
|
||||||
def prepare_request(self, api_info, params, doseq=0):
|
def prepare_request(self, api_info, params, doseq=0):
|
||||||
for key in params:
|
for key in params:
|
||||||
|
@ -128,7 +128,7 @@ class OCR(baseocr):
|
|||||||
renew_text = response.json()["payload"]["ocr_output_text"]["text"]
|
renew_text = response.json()["payload"]["ocr_output_text"]["text"]
|
||||||
finalResult = json.loads(str(base64.b64decode(renew_text), "utf-8"))
|
finalResult = json.loads(str(base64.b64decode(renew_text), "utf-8"))
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
try:
|
try:
|
||||||
res = finalResult["pages"][0]
|
res = finalResult["pages"][0]
|
||||||
if "lines" not in res:
|
if "lines" not in res:
|
||||||
|
@ -47,7 +47,7 @@ class OCR(baseocr):
|
|||||||
"text": [l["words"] for l in response.json()["lines"]],
|
"text": [l["words"] for l in response.json()["lines"]],
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def ocrapi(self, imagebinary):
|
def ocrapi(self, imagebinary):
|
||||||
def truncate(q):
|
def truncate(q):
|
||||||
@ -93,7 +93,7 @@ class OCR(baseocr):
|
|||||||
"text": [l["text"] for l in _],
|
"text": [l["text"] for l in _],
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def freetest_ts(self, imagebinary):
|
def freetest_ts(self, imagebinary):
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ class OCR(baseocr):
|
|||||||
"isocrtranslate": True,
|
"isocrtranslate": True,
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def ocrapi_ts(self, imagebinary):
|
def ocrapi_ts(self, imagebinary):
|
||||||
|
|
||||||
@ -237,7 +237,7 @@ class OCR(baseocr):
|
|||||||
]
|
]
|
||||||
return {"box": box, "text": text, "isocrtranslate": True}
|
return {"box": box, "text": text, "isocrtranslate": True}
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def ocr(self, imagebinary):
|
def ocr(self, imagebinary):
|
||||||
interfacetype = self.config["interface"]
|
interfacetype = self.config["interface"]
|
||||||
|
@ -111,13 +111,6 @@ class ResponseBase:
|
|||||||
charset = m.group(1) if m else "utf-8"
|
charset = m.group(1) if m else "utf-8"
|
||||||
return charset
|
return charset
|
||||||
|
|
||||||
@property
|
|
||||||
def maybejson(self):
|
|
||||||
try:
|
|
||||||
return self.json()
|
|
||||||
except:
|
|
||||||
return self.text
|
|
||||||
|
|
||||||
def json(self):
|
def json(self):
|
||||||
return json.loads(self.text)
|
return json.loads(self.text)
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
message = response.json()["result"].replace("\n\n", "\n").strip()
|
message = response.json()["result"].replace("\n\n", "\n").strip()
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
yield message
|
yield message
|
||||||
self.context.append({"role": "user", "content": query})
|
self.context.append({"role": "user", "content": query})
|
||||||
self.context.append({"role": "assistant", "content": message})
|
self.context.append({"role": "assistant", "content": message})
|
||||||
|
@ -22,7 +22,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
token = res.json()["tenant_access_token"]
|
token = res.json()["tenant_access_token"]
|
||||||
except:
|
except:
|
||||||
raise Exception(res.maybejson)
|
raise Exception(res)
|
||||||
self.tokens[(app_id, app_secret)] = token
|
self.tokens[(app_id, app_secret)] = token
|
||||||
return self.tokens[(app_id, app_secret)]
|
return self.tokens[(app_id, app_secret)]
|
||||||
|
|
||||||
@ -45,4 +45,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return res.json()["data"]["text"]
|
return res.json()["data"]["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(res.maybejson)
|
raise Exception(res)
|
||||||
|
@ -146,7 +146,7 @@ class TS(basetrans):
|
|||||||
"Content"
|
"Content"
|
||||||
]
|
]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
yield message
|
yield message
|
||||||
self.context.append({"Role": "user", "Content": query})
|
self.context.append({"Role": "user", "Content": query})
|
||||||
self.context.append({"Role": "assistant", "Content": message})
|
self.context.append({"Role": "assistant", "Content": message})
|
||||||
|
@ -79,5 +79,5 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
trans = data["data"]["translateText"]
|
trans = data["data"]["translateText"]
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(r)
|
||||||
return html.unescape(trans)
|
return html.unescape(trans)
|
||||||
|
@ -58,4 +58,4 @@ class TS(basetrans):
|
|||||||
response = request.json()
|
response = request.json()
|
||||||
return response["Data"]["Translated"]
|
return response["Data"]["Translated"]
|
||||||
except:
|
except:
|
||||||
raise Exception(request.maybejson)
|
raise Exception(request)
|
||||||
|
@ -38,4 +38,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response[0]["translations"][0]["text"]
|
return response[0]["translations"][0]["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(request.maybejson)
|
raise Exception(request)
|
||||||
|
@ -2,6 +2,7 @@ import requests
|
|||||||
import re
|
import re
|
||||||
from translator.basetranslator import basetrans
|
from translator.basetranslator import basetrans
|
||||||
import time, urllib
|
import time, urllib
|
||||||
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
class Tse:
|
class Tse:
|
||||||
@ -122,7 +123,7 @@ class BaiduV1(Tse):
|
|||||||
try:
|
try:
|
||||||
return "\n".join([item["dst"] for item in r.json()["data"]])
|
return "\n".join([item["dst"] for item in r.json()["data"]])
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(maybejson(r))
|
||||||
|
|
||||||
|
|
||||||
class TS(basetrans):
|
class TS(basetrans):
|
||||||
|
@ -60,7 +60,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["lan"]
|
return response.json()["lan"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def translate(self, query):
|
def translate(self, query):
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return "\n".join([_["dst"] for _ in r.json()["result"]["trans_result"]])
|
return "\n".join([_["dst"] for _ in r.json()["result"]["trans_result"]])
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(r)
|
||||||
|
|
||||||
def translate_fy(self, q):
|
def translate_fy(self, q):
|
||||||
self.checkempty(["APP ID", "密钥"])
|
self.checkempty(["APP ID", "密钥"])
|
||||||
@ -94,4 +94,4 @@ class TS(basetrans):
|
|||||||
return "\n".join([_["dst"] for _ in res.json()["trans_result"]])
|
return "\n".join([_["dst"] for _ in res.json()["trans_result"]])
|
||||||
|
|
||||||
except:
|
except:
|
||||||
raise Exception(res.maybejson)
|
raise Exception(res)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
import requests, time, urllib
|
import requests, time, urllib
|
||||||
from translator.basetranslator import basetrans
|
from translator.basetranslator import basetrans
|
||||||
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
class Tse:
|
class Tse:
|
||||||
@ -150,7 +151,7 @@ class Bing(Tse):
|
|||||||
"to": to_language,
|
"to": to_language,
|
||||||
"tryFetchingGenderDebiasedTranslations": "true",
|
"tryFetchingGenderDebiasedTranslations": "true",
|
||||||
}
|
}
|
||||||
form_data = {**form_data, **self.tk}
|
form_data.update(self.tk)
|
||||||
api_url_param = "?isVertical=1&&IG={}&IID={}".format(
|
api_url_param = "?isVertical=1&&IG={}&IID={}".format(
|
||||||
self.ig_iid["ig"], self.ig_iid["iid"]
|
self.ig_iid["ig"], self.ig_iid["iid"]
|
||||||
)
|
)
|
||||||
@ -169,7 +170,7 @@ class Bing(Tse):
|
|||||||
try:
|
try:
|
||||||
return data[0] if is_detail_result else data[0]["translations"][0]["text"]
|
return data[0] if is_detail_result else data[0]["translations"][0]["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(maybejson(r))
|
||||||
|
|
||||||
|
|
||||||
class TS(basetrans):
|
class TS(basetrans):
|
||||||
|
@ -121,4 +121,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return decrypt(response.json()["target"])
|
return decrypt(response.json()["target"])
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -28,4 +28,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["target"]
|
return response.json()["target"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -85,7 +85,7 @@ class TS(basetrans):
|
|||||||
response.json()["content"][0]["text"].replace("\n\n", "\n").strip()
|
response.json()["content"][0]["text"].replace("\n\n", "\n").strip()
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
yield message
|
yield message
|
||||||
self.context.append({"role": "user", "content": query})
|
self.context.append({"role": "user", "content": query})
|
||||||
self.context.append({"role": "assistant", "content": message})
|
self.context.append({"role": "assistant", "content": message})
|
||||||
|
@ -116,7 +116,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
message = response.json()["text"]
|
message = response.json()["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
yield message
|
yield message
|
||||||
self.context.append({"role": "USER", "message": query})
|
self.context.append({"role": "USER", "message": query})
|
||||||
self.context.append({"role": "CHATBOT", "message": message})
|
self.context.append({"role": "CHATBOT", "message": message})
|
||||||
|
@ -87,7 +87,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["data"]
|
return response.json()["data"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def translate_deeplx_internal(self, translateText):
|
def translate_deeplx_internal(self, translateText):
|
||||||
# Preparing the request data for the DeepL API
|
# Preparing the request data for the DeepL API
|
||||||
|
@ -41,4 +41,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["translations"][0]["text"]
|
return response.json()["translations"][0]["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -59,7 +59,11 @@ class TS(basetrans):
|
|||||||
message.append({"role": "model", "parts": [{"text": prefill}]})
|
message.append({"role": "model", "parts": [{"text": prefill}]})
|
||||||
contents = dict(contents=message)
|
contents = dict(contents=message)
|
||||||
usingstream = self.config["usingstream"]
|
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(
|
res = self.proxysession.post(
|
||||||
urlpathjoin(
|
urlpathjoin(
|
||||||
self.config["BASE_URL"].strip(),
|
self.config["BASE_URL"].strip(),
|
||||||
@ -85,7 +89,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
line = res.json()["candidates"][0]["content"]["parts"][0]["text"]
|
line = res.json()["candidates"][0]["content"]["parts"][0]["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(res.maybejson)
|
raise Exception(res)
|
||||||
yield line
|
yield line
|
||||||
self.context.append({"role": "user", "parts": [{"text": query}]})
|
self.context.append({"role": "user", "parts": [{"text": query}]})
|
||||||
self.context.append({"role": "model", "parts": [{"text": line}]})
|
self.context.append({"role": "model", "parts": [{"text": line}]})
|
||||||
|
@ -23,4 +23,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["data"]["translations"][0]["translatedText"]
|
return response.json()["data"]["translations"][0]["translatedText"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -3,6 +3,7 @@ import json, requests, time, hmac, hashlib
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from myutils.utils import createurl, createenglishlangmap, urlpathjoin
|
from myutils.utils import createurl, createenglishlangmap, urlpathjoin
|
||||||
from myutils.proxy import getproxy
|
from myutils.proxy import getproxy
|
||||||
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
def list_models(typename, regist):
|
def list_models(typename, regist):
|
||||||
@ -19,7 +20,7 @@ def list_models(typename, regist):
|
|||||||
try:
|
try:
|
||||||
return sorted([_["id"] for _ in resp.json()["data"]])
|
return sorted([_["id"] for _ in resp.json()["data"]])
|
||||||
except:
|
except:
|
||||||
raise Exception(resp.maybejson)
|
raise Exception(maybejson(resp))
|
||||||
|
|
||||||
|
|
||||||
class qianfanIAM:
|
class qianfanIAM:
|
||||||
@ -116,7 +117,7 @@ class gptcommon(basetrans):
|
|||||||
) and (response.status_code != 200):
|
) and (response.status_code != 200):
|
||||||
# application/json
|
# application/json
|
||||||
# text/html
|
# text/html
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
for chunk in response.iter_lines():
|
for chunk in response.iter_lines():
|
||||||
response_data: str = chunk.decode("utf-8").strip()
|
response_data: str = chunk.decode("utf-8").strip()
|
||||||
if not response_data.startswith("data: "):
|
if not response_data.startswith("data: "):
|
||||||
@ -148,7 +149,7 @@ class gptcommon(basetrans):
|
|||||||
.strip()
|
.strip()
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
yield message
|
yield message
|
||||||
self.context.append({"role": "user", "content": query})
|
self.context.append({"role": "user", "content": query})
|
||||||
self.context.append({"role": "assistant", "content": message})
|
self.context.append({"role": "assistant", "content": message})
|
||||||
|
@ -37,4 +37,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["translation"]
|
return response.json()["translation"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -332,7 +332,7 @@ class Service(object):
|
|||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
return json.dumps(resp.json())
|
return json.dumps(resp.json())
|
||||||
else:
|
else:
|
||||||
raise Exception(resp.maybejson)
|
raise Exception(resp)
|
||||||
|
|
||||||
def prepare_request(self, api_info, params, doseq=0):
|
def prepare_request(self, api_info, params, doseq=0):
|
||||||
for key in params:
|
for key in params:
|
||||||
|
@ -314,7 +314,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
project_id = response.json()["projects"][0]["id"]
|
project_id = response.json()["projects"][0]["id"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
self.cacheproject[(end, ak, sk)] = project_id
|
self.cacheproject[(end, ak, sk)] = project_id
|
||||||
project_id = self.cacheproject.get((end, ak, sk))
|
project_id = self.cacheproject.get((end, ak, sk))
|
||||||
url = "https://{}/v1/{}/machine-translation/text-translation".format(
|
url = "https://{}/v1/{}/machine-translation/text-translation".format(
|
||||||
|
@ -22,4 +22,4 @@ class TS(basetrans):
|
|||||||
translation = result["translations"][0]["translation"]
|
translation = result["translations"][0]["translation"]
|
||||||
return translation
|
return translation
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -2,6 +2,7 @@ import requests
|
|||||||
import re
|
import re
|
||||||
import urllib
|
import urllib
|
||||||
import time
|
import time
|
||||||
|
from myutils.commonbase import maybejson
|
||||||
|
|
||||||
|
|
||||||
class Tse:
|
class Tse:
|
||||||
@ -124,7 +125,7 @@ class Itranslate(Tse):
|
|||||||
try:
|
try:
|
||||||
return r.json()["target"]["text"]
|
return r.json()["target"]["text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(maybejson(r))
|
||||||
|
|
||||||
|
|
||||||
from traceback import print_exc
|
from traceback import print_exc
|
||||||
|
@ -159,7 +159,7 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
yield output.json()
|
yield output.json()
|
||||||
except:
|
except:
|
||||||
raise Exception(output.maybejson)
|
raise Exception(output)
|
||||||
|
|
||||||
def send_request_stream(self, messages, is_test=False, **kwargs):
|
def send_request_stream(self, messages, is_test=False, **kwargs):
|
||||||
extra_query = {
|
extra_query = {
|
||||||
@ -195,7 +195,7 @@ class TS(basetrans):
|
|||||||
if (not output.headers["Content-Type"].startswith("text/event-stream")) and (
|
if (not output.headers["Content-Type"].startswith("text/event-stream")) and (
|
||||||
output.status_code != 200
|
output.status_code != 200
|
||||||
):
|
):
|
||||||
raise Exception(output.maybejson)
|
raise Exception(output)
|
||||||
for chunk in output.iter_lines():
|
for chunk in output.iter_lines():
|
||||||
response_data: str = chunk.decode("utf-8").strip()
|
response_data: str = chunk.decode("utf-8").strip()
|
||||||
if not response_data.startswith("data: "):
|
if not response_data.startswith("data: "):
|
||||||
|
@ -13,4 +13,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()
|
return response.json()
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -75,4 +75,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return ret.json()["Response"]["TargetText"]
|
return ret.json()["Response"]["TargetText"]
|
||||||
except:
|
except:
|
||||||
raise Exception(ret.maybejson)
|
raise Exception(ret)
|
||||||
|
@ -38,4 +38,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["tgt_text"]
|
return response.json()["tgt_text"]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -8,12 +8,26 @@ class TS(basetrans):
|
|||||||
headers = {
|
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"
|
"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 = {
|
params = {
|
||||||
"lang": "{}-{}".format(self.srclang, self.tgtlang),
|
"lang": "{}-{}".format(lang, self.tgtlang),
|
||||||
"text": content,
|
"text": content,
|
||||||
|
"srv": "browser_video_translation",
|
||||||
}
|
}
|
||||||
url = "https://browser.translate.yandex.net/api/v1/tr.json/translate"
|
url = "https://browser.translate.yandex.net/api/v1/tr.json/translate"
|
||||||
params = {**{"srv": "browser_video_translation"}, **params}
|
|
||||||
response = self.proxysession.post(
|
response = self.proxysession.post(
|
||||||
url=url,
|
url=url,
|
||||||
params=params,
|
params=params,
|
||||||
@ -24,4 +38,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["text"][0]
|
return response.json()["text"][0]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -21,4 +21,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return response.json()["text"][0]
|
return response.json()["text"][0]
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -105,4 +105,4 @@ class TS(basetrans):
|
|||||||
|
|
||||||
return res
|
return res
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
@ -45,4 +45,4 @@ class TS(basetrans):
|
|||||||
try:
|
try:
|
||||||
return r.json()["translation"][0]
|
return r.json()["translation"][0]
|
||||||
except:
|
except:
|
||||||
raise Exception(r.maybejson)
|
raise Exception(r)
|
||||||
|
@ -85,7 +85,7 @@ class TS(basetrans):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
raise Exception(response.maybejson)
|
raise Exception(response)
|
||||||
|
|
||||||
def translate(self, content):
|
def translate(self, content):
|
||||||
return self.translate10_9_8(content)
|
return self.translate10_9_8(content)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user