This commit is contained in:
恍兮惚兮 2024-10-09 01:04:30 +08:00
parent ac15a09471
commit 5041de78b8
51 changed files with 181 additions and 209 deletions

View File

@ -917,11 +917,22 @@ def checkv1(api_url: str):
return api_url + "/v1" return api_url + "/v1"
def urlpathjoin(*argc):
urlx = []
for i, u in enumerate(argc):
if u.startswith("/") and i != 0:
u = u[1:]
if u.endswith("/") and i != len(argc) - 1:
u = u[:-1]
urlx.append(u)
return "/".join(urlx)
def createurl(url: str): def createurl(url: str):
if url.endswith("/chat/completions"): if url.endswith("/chat/completions"):
pass pass
else: else:
url = checkv1(url) + "/chat/completions" url = urlpathjoin(checkv1(url), "/chat/completions")
return url return url

View File

@ -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.text) raise Exception(response.maybejson)
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.text) raise Exception(response.maybejson)
@property @property
def srclangx(self): def srclangx(self):
@ -154,12 +154,12 @@ class OCR(baseocr):
"client_id": API_KEY, "client_id": API_KEY,
"client_secret": SECRET_KEY, "client_secret": SECRET_KEY,
} }
js = self.proxysession.post(url, params=params).json() resp = self.proxysession.post(url, params=params)
try: try:
return js["access_token"] return resp.json()["access_token"]
except: except:
raise Exception(js) raise Exception(resp.maybejson)
def getaccess(self): def getaccess(self):
self.checkempty(["API Key", "Secret Key"]) self.checkempty(["API Key", "Secret Key"])
@ -236,4 +236,4 @@ class OCR(baseocr):
] ]
return {"box": boxs, "text": texts} return {"box": boxs, "text": texts}
except: except:
raise Exception(response.text) raise Exception(response.maybejson)

View File

@ -1,21 +1,25 @@
from ocrengines.baseocrclass import baseocr from ocrengines.baseocrclass import baseocr
import base64, requests import base64, requests
from myutils.utils import createurl, createenglishlangmap from myutils.utils import createurl, createenglishlangmap, urlpathjoin
from myutils.proxy import getproxy from myutils.proxy import getproxy
def list_models(typename, regist): def list_models(typename, regist):
js = requests.get( resp = requests.get(
createurl(regist["apiurl"]().strip())[: -len("/chat/completions")] + "/models", urlpathjoin(
headers={"Authorization": "Bearer " + regist["SECRET_KEY"]().split("|")[0].strip()}, createurl(regist["apiurl"]().strip())[: -len("chat/completions")],
"models",
),
headers={
"Authorization": "Bearer " + regist["SECRET_KEY"]().split("|")[0].strip()
},
proxies=getproxy(("ocr", typename)), proxies=getproxy(("ocr", typename)),
timeout=10, timeout=10,
).json() )
try: try:
return sorted([_["id"] for _ in js["data"]]) return sorted([_["id"] for _ in resp.json()["data"]])
except: except:
raise Exception(js) raise Exception(resp.maybejson)
class OCR(baseocr): class OCR(baseocr):
@ -79,7 +83,7 @@ class OCR(baseocr):
) )
return message return message
except: except:
raise Exception(response.text) raise Exception(response.maybejson)
def createurl(self): def createurl(self):
return createurl(self.config["apiurl"]) return createurl(self.config["apiurl"])

View File

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

View File

@ -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.json()) raise Exception(res.maybejson)
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.text) raise Exception(res.maybejson)

View File

@ -1,21 +1,21 @@
import base64 import base64
import requests import requests
from ocrengines.baseocrclass import baseocr from ocrengines.baseocrclass import baseocr
from myutils.utils import createenglishlangmap from myutils.utils import createenglishlangmap, urlpathjoin
from myutils.proxy import getproxy from myutils.proxy import getproxy
def list_models(typename, regist): def list_models(typename, regist):
js = requests.get( resp = requests.get(
"https://generativelanguage.googleapis.com/v1beta/models", urlpathjoin(regist["BASE_URL"]().strip(), "v1beta/models"),
params={"key": regist["key"]().split("|")[0].strip()}, params={"key": regist["key"]().split("|")[0].strip()},
proxies=getproxy(("ocr", typename)), proxies=getproxy(("ocr", typename)),
timeout=10, timeout=10,
).json() )
try: try:
models = js["models"] models = resp.json()["models"]
except: except:
raise Exception(js) raise Exception(resp.maybejson)
mm = [] mm = []
for m in models: for m in models:
name: str = m["name"] name: str = m["name"]
@ -34,10 +34,9 @@ class OCR(baseocr):
def ocr(self, imagebinary): def ocr(self, imagebinary):
self.checkempty(["key"]) self.checkempty(["key"])
self.checkempty(["url"]) self.checkempty(["BASE_URL"])
self.checkempty(["model"]) self.checkempty(["model"])
api_key = self.config["key"] api_key = self.config["key"]
url = self.config["url"]
model = self.config["model"] model = self.config["model"]
image_data = base64.b64encode(imagebinary).decode("utf-8") image_data = base64.b64encode(imagebinary).decode("utf-8")
@ -59,15 +58,21 @@ class OCR(baseocr):
# Set up the request headers and URL # Set up the request headers and URL
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
# by default https://generativelanguage.googleapis.com/v1 # by default https://generativelanguage.googleapis.com/v1
url = f"{url}/models/{model}:generateContent?key={api_key}"
# Send the request # Send the request
response = requests.post(url, headers=headers, json=payload, proxies=self.proxy) response = requests.post(
urlpathjoin(
self.config["BASE_URL"],
f"v1beta/models/{model}:generateContent?key={api_key}",
),
headers=headers,
json=payload,
proxies=self.proxy,
)
try: try:
# Handle the response # Handle the response
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.text) raise Exception(response.maybejson)
except Exception as e: except Exception as e:
raise Exception(response.text) from e raise Exception(response.maybejson) from e

View File

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

View File

@ -20,4 +20,4 @@ class OCR(baseocr):
try: try:
return response.json()["text"] return response.json()["text"]
except Exception as e: except Exception as e:
raise Exception(response.text) from e raise Exception(response.maybejson) from e

View File

@ -51,7 +51,6 @@ class OCR(baseocr):
response = self.proxysession.post( response = self.proxysession.post(
"https://" + base + "/parse/image", headers=headers, data=data "https://" + base + "/parse/image", headers=headers, data=data
) )
# print(response.text)
try: try:
_ = response.json()["ParsedResults"][0]["ParsedText"] _ = response.json()["ParsedResults"][0]["ParsedText"]
boxs = [] boxs = []
@ -71,4 +70,4 @@ class OCR(baseocr):
] ]
return {"box": boxs, "text": texts} return {"box": boxs, "text": texts}
except: except:
raise Exception(response.text) raise Exception(response.maybejson)

View File

@ -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.json()) raise Exception(r.maybejson)
def langmap(self): def langmap(self):
# https://cloud.tencent.com/document/product/866/33526 # https://cloud.tencent.com/document/product/866/33526
@ -190,7 +190,6 @@ class OCR(baseocr):
r = self.proxysession.get( r = self.proxysession.get(
url="https://ocr.tencentcloudapi.com/", params=req_para, timeout=10 url="https://ocr.tencentcloudapi.com/", params=req_para, timeout=10
) )
# print(r.text)
try: try:
boxs = [ boxs = [
@ -209,7 +208,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.text) raise Exception(r.maybejson)
def ocr(self, imagebinary): def ocr(self, imagebinary):
interfacetype = self.config["interface"] interfacetype = self.config["interface"]

View File

@ -442,7 +442,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.text) raise Exception(resp.maybejson)
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:

View File

@ -124,14 +124,11 @@ class OCR(baseocr):
request_url, data=json.dumps(body), headers=headers request_url, data=json.dumps(body), headers=headers
) )
re = response.content.decode("utf8")
str_result = json.loads(re)
# print("\nresponse-content:", re)
try: try:
renew_text = str_result["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(str_result) raise Exception(response.maybejson)
try: try:
res = finalResult["pages"][0] res = finalResult["pages"][0]
if "lines" not in res: if "lines" not in res:

View File

@ -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.text) raise Exception(response.maybejson)
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.text) raise Exception(response.maybejson)
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.text) raise Exception(response.maybejson)
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.text) raise Exception(response.maybejson)
def ocr(self, imagebinary): def ocr(self, imagebinary):
interfacetype = self.config["interface"] interfacetype = self.config["interface"]

View File

@ -109,6 +109,13 @@ 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)

View File

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

View File

@ -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.text) raise Exception(request.maybejson)

View File

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

View File

@ -111,8 +111,6 @@ class BaiduV1(Tse):
timeout = kwargs.get("timeout", None) timeout = kwargs.get("timeout", None)
proxies = kwargs.get("proxies", None) proxies = kwargs.get("proxies", None)
sleep_seconds = kwargs.get("sleep_seconds", 0) sleep_seconds = kwargs.get("sleep_seconds", 0)
if_print_warning = kwargs.get("if_print_warning", True)
is_detail_result = kwargs.get("is_detail_result", False)
update_session_after_freq = kwargs.get( update_session_after_freq = kwargs.get(
"update_session_after_freq", self.default_session_freq "update_session_after_freq", self.default_session_freq
) )
@ -120,10 +118,6 @@ class BaiduV1(Tse):
"update_session_after_seconds", self.default_session_seconds "update_session_after_seconds", self.default_session_seconds
) )
not_update_cond_freq = 1 if self.query_count < update_session_after_freq else 0
not_update_cond_time = (
1 if time.time() - self.begin_time < update_session_after_seconds else 0
)
if not (self.session): if not (self.session):
self.session = requests.Session() self.session = requests.Session()
_ = self.session.get( _ = self.session.get(
@ -154,17 +148,12 @@ class BaiduV1(Tse):
proxies=proxies, proxies=proxies,
) )
r.raise_for_status() r.raise_for_status()
data = r.json()
time.sleep(sleep_seconds) time.sleep(sleep_seconds)
self.query_count += 1 self.query_count += 1
try: try:
return ( return "\n".join([item["dst"] for item in r.json()["data"]])
data
if is_detail_result
else "\n".join([item["dst"] for item in data["data"]])
)
except: except:
raise Exception(data) raise Exception(r.maybejson)
class TS(basetrans): class TS(basetrans):

View File

@ -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.text) raise Exception(r.maybejson)
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.text) raise Exception(res.maybejson)

View File

@ -52,7 +52,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.text) raise Exception(response.maybejson)
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})

View File

@ -190,7 +190,6 @@ class Bing(Tse):
) )
r.raise_for_status() r.raise_for_status()
data = r.json() data = r.json()
print(r.text)
time.sleep(sleep_seconds) time.sleep(sleep_seconds)
self.query_count += 1 self.query_count += 1
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"]

View File

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

View File

@ -26,6 +26,6 @@ class TS(basetrans):
"POST", url, data=json.dumps(payload), headers=headers "POST", url, data=json.dumps(payload), headers=headers
) )
try: try:
return json.loads(response.text)["target"] return response.json()["target"]
except: except:
raise Exception(response.text) raise Exception(response.maybejson)

View File

@ -1,7 +1,7 @@
from traceback import print_exc from traceback import print_exc
import json import json
from translator.basetranslator import basetrans from translator.basetranslator import basetrans
from myutils.utils import createenglishlangmap, checkv1 from myutils.utils import createenglishlangmap, checkv1, urlpathjoin
class TS(basetrans): class TS(basetrans):
@ -24,7 +24,9 @@ class TS(basetrans):
temperature = self.config["Temperature"] temperature = self.config["Temperature"]
message = [] message = []
self._gpt_common_parse_context(message, self.context, self.config["附带上下文个数"]) self._gpt_common_parse_context(
message, self.context, self.config["附带上下文个数"]
)
message.append({"role": "user", "content": query}) message.append({"role": "user", "content": query})
prefill = self._gptlike_create_prefill("prefill_use", "prefill") prefill = self._gptlike_create_prefill("prefill_use", "prefill")
if prefill: if prefill:
@ -46,7 +48,7 @@ class TS(basetrans):
stream=usingstream, stream=usingstream,
) )
response = self.proxysession.post( response = self.proxysession.post(
checkv1(self.config["BASE_URL"]) + "/messages", urlpathjoin(checkv1(self.config["BASE_URL"]) + "messages"),
headers=headers, headers=headers,
json=data, json=data,
stream=usingstream, stream=usingstream,
@ -83,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.text) raise Exception(response.maybejson)
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})

View File

@ -116,7 +116,7 @@ class TS(basetrans):
try: try:
message = response.json()["text"] message = response.json()["text"]
except: except:
raise Exception(response.text) raise Exception(response.maybejson)
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})

View File

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

View File

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

View File

@ -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.json()) raise Exception(res.maybejson)
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.text) raise Exception(res.maybejson)

View File

@ -1,5 +1,5 @@
from translator.basetranslator import basetrans from translator.basetranslator import basetrans
from myutils.utils import createenglishlangmap from myutils.utils import createenglishlangmap, urlpathjoin
import json, requests import json, requests
from myutils.proxy import getproxy from myutils.proxy import getproxy
@ -61,7 +61,10 @@ class TS(basetrans):
usingstream = self.config["usingstream"] usingstream = self.config["usingstream"]
payload = {**contents, **safety, **sys_message, **gen_config} payload = {**contents, **safety, **sys_message, **gen_config}
res = self.proxysession.post( res = self.proxysession.post(
f"https://generativelanguage.googleapis.com/v1beta/models/{model}:{['generateContent','streamGenerateContent'][usingstream]}", urlpathjoin(
self.config["BASE_URL"],
f"v1beta/models/{model}:{['generateContent','streamGenerateContent'][usingstream]}",
),
params={"key": self.multiapikeycurrent["SECRET_KEY"]}, params={"key": self.multiapikeycurrent["SECRET_KEY"]},
json=payload, json=payload,
stream=usingstream, stream=usingstream,
@ -80,7 +83,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.text) raise Exception(res.maybejson)
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}]})
@ -88,7 +91,7 @@ class TS(basetrans):
def list_models(typename, regist): def list_models(typename, regist):
js = requests.get( js = requests.get(
"https://generativelanguage.googleapis.com/v1beta/models", urlpathjoin(regist["BASE_URL"]().strip(), "v1beta/models"),
params={"key": regist["SECRET_KEY"]().split("|")[0].strip()}, params={"key": regist["SECRET_KEY"]().split("|")[0].strip()},
proxies=getproxy(("fanyi", typename)), proxies=getproxy(("fanyi", typename)),
timeout=10, timeout=10,

View File

@ -77,8 +77,6 @@ class TS(basetrans):
headers=headers, headers=headers,
data=freq, data=freq,
) )
# good=response.text.split('\n')[3]
# print(response.text)
json_data = json.loads(response.text[6:]) json_data = json.loads(response.text[6:])
data = json.loads(json_data[0][2]) data = json.loads(json_data[0][2])
return " ".join([x[0] for x in (data[1][0][0][5] or data[1][0])]) return " ".join([x[0] for x in (data[1][0][0][5] or data[1][0])])

View File

@ -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.text) raise Exception(response.maybejson)

View File

@ -1,25 +1,25 @@
from translator.basetranslator import basetrans from translator.basetranslator import basetrans
import json, requests import json, requests
from traceback import print_exc from myutils.utils import createurl, createenglishlangmap, urlpathjoin
from myutils.utils import createurl, createenglishlangmap
from myutils.proxy import getproxy from myutils.proxy import getproxy
def list_models(typename, regist): def list_models(typename, regist):
js = requests.get( resp = requests.get(
createurl(regist["API接口地址"]().strip())[: -len("/chat/completions")] urlpathjoin(
+ "/models", createurl(regist["API接口地址"]().strip())[: -len("chat/completions")],
"models",
),
headers={ headers={
"Authorization": "Bearer " + regist["SECRET_KEY"]().split("|")[0].strip() "Authorization": "Bearer " + regist["SECRET_KEY"]().split("|")[0].strip()
}, },
proxies=getproxy(("fanyi", typename)), proxies=getproxy(("fanyi", typename)),
timeout=10, timeout=10,
).json() )
try: try:
return sorted([_["id"] for _ in js["data"]]) return sorted([_["id"] for _ in resp.json()["data"]])
except: except:
raise Exception(js) raise Exception(resp.maybejson)
class gptcommon(basetrans): class gptcommon(basetrans):
@ -74,7 +74,7 @@ class gptcommon(basetrans):
if not response.headers["Content-Type"].startswith("text/event-stream"): if not response.headers["Content-Type"].startswith("text/event-stream"):
# application/json # application/json
# text/html # text/html
raise Exception(response.text) raise Exception(response.maybejson)
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: "):
@ -105,7 +105,7 @@ class gptcommon(basetrans):
.strip() .strip()
) )
except: except:
raise Exception(response.text) raise Exception(response.maybejson)
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,9 @@ class gptcommon(basetrans):
) )
sysprompt = self._gptlike_createsys("使用自定义promt", "自定义promt") sysprompt = self._gptlike_createsys("使用自定义promt", "自定义promt")
message = [{"role": "system", "content": sysprompt}] message = [{"role": "system", "content": sysprompt}]
self._gpt_common_parse_context(message, self.context, self.config["附带上下文个数"]) self._gpt_common_parse_context(
message, self.context, self.config["附带上下文个数"]
)
message.append({"role": "user", "content": query}) message.append({"role": "user", "content": query})
prefill = self._gptlike_create_prefill("prefill_use", "prefill") prefill = self._gptlike_create_prefill("prefill_use", "prefill")
if prefill: if prefill:

View File

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

View File

@ -336,7 +336,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.text) raise Exception(resp.maybejson)
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:

View File

@ -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.json()) raise Exception(response.maybejson)
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 = f"https://{end}/v1/{project_id}/machine-translation/text-translation" url = f"https://{end}/v1/{project_id}/machine-translation/text-translation"

View File

@ -20,4 +20,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.text) raise Exception(response.maybejson)

View File

@ -143,7 +143,7 @@ class Itranslate(Tse):
try: try:
return r.json()["target"]["text"] return r.json()["target"]["text"]
except: except:
raise Exception(r.text) raise Exception(r.maybejson)
from traceback import print_exc from traceback import print_exc

View File

@ -1,6 +1,7 @@
from translator.basetranslator import basetrans from translator.basetranslator import basetrans
import requests import requests
import json, zhconv import json, zhconv
from myutils.utils import urlpathjoin
# OpenAI # OpenAI
# from openai import OpenAI # from openai import OpenAI
@ -140,14 +141,16 @@ class TS(basetrans):
stream=False, stream=False,
) )
try: try:
output = self.session.post(self.api_url + "/chat/completions", json=data) output = self.session.post(
urlpathjoin(self.api_url, "chat/completions"), json=data
)
except requests.RequestException as e: except requests.RequestException as e:
raise ValueError(f"连接到Sakura API超时{self.api_url}") raise ValueError(f"连接到Sakura API超时{self.api_url}")
try: try:
yield output.json() yield output.json()
except: except:
raise Exception(output.text) raise Exception(output.maybejson)
def send_request_stream(self, messages, is_test=False, **kwargs): def send_request_stream(self, messages, is_test=False, **kwargs):
extra_query = { extra_query = {
@ -173,14 +176,14 @@ class TS(basetrans):
) )
try: try:
output = self.session.post( output = self.session.post(
self.api_url + "/chat/completions", urlpathjoin(self.api_url, "chat/completions"),
json=data, json=data,
stream=True, stream=True,
) )
except requests.RequestException: except requests.RequestException:
raise ValueError(f"连接到Sakura API超时{self.api_url}") raise ValueError(f"连接到Sakura API超时{self.api_url}")
if not output.headers["Content-Type"].startswith("text/event-stream"): if not output.headers["Content-Type"].startswith("text/event-stream"):
raise Exception(output.text) raise Exception(output.maybejson)
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: "):

View File

@ -1,6 +1,4 @@
import requests, os
from translator.basetranslator import basetrans from translator.basetranslator import basetrans
from myutils.subproc import subproc_w, autoproc
class TS(basetrans): class TS(basetrans):
@ -15,4 +13,4 @@ class TS(basetrans):
try: try:
return response.json() return response.json()
except: except:
raise Exception(response.text) raise Exception(response.maybejson)

View File

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

View File

@ -146,7 +146,7 @@ class TS(basetrans):
"Content" "Content"
] ]
except: except:
raise Exception(response.text) raise Exception(response.maybejson)
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})

View File

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

View File

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

View File

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

View File

@ -105,7 +105,4 @@ class TS(basetrans):
return res return res
except: except:
raise Exception(response.text) raise Exception(response.maybejson)
def show(self, res):
print("有道", "\033[0;33;47m", res, "\033[0m", flush=True)

View File

@ -58,4 +58,4 @@ class TS(basetrans):
return js return js
except: except:
raise Exception(response.text) raise Exception(response.maybejson)

View File

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

View File

@ -43,7 +43,7 @@ class TS(basetrans):
} }
mysticTime, sign = self.signx() mysticTime, sign = self.signx()
if v == 10:
param = { param = {
"keyfrom": "deskdict.main", "keyfrom": "deskdict.main",
"client": "deskdict", "client": "deskdict",
@ -71,53 +71,6 @@ class TS(basetrans):
"appVersion": "10.1.3.0", "appVersion": "10.1.3.0",
"product": "deskdict", "product": "deskdict",
} }
elif v == 9:
param = {
"keyfrom": "deskdict.main",
"client": "deskdict",
"from": self.srclang,
"to": self.tgtlang,
"keyid": "deskdict",
"mysticTime": mysticTime,
"pointParam": "client,product,mysticTime",
"sign": sign,
"domain": "0",
"useTerm": "false",
"noCheckPrivate": "false",
"id": "0a464aedddbc6e4b9",
"vendor": "unknown",
"in": "YoudaoDictSetup",
"appVer": "9.3.0.0",
"appZengqiang": "0",
"abTest": "0",
"model": "LENOVO",
"screen": "1920*1080",
"OsVersion": "10.0.19045",
"appVersion": "9.3.0.0",
"product": "deskdict",
}
elif v == 8:
param = {
"keyfrom": "deskdict.main",
"client": "deskdict",
"from": self.srclang,
"to": self.tgtlang,
"keyid": "deskdict",
"mysticTime": mysticTime,
"pointParam": "client,product,mysticTime",
"sign": sign,
"id": "0a464aedddbc6e4b9",
"vendor": "unknown",
"in": "YoudaoDictSetup",
"appVer": "8.10.8.0",
"appZengqiang": "0",
"abTest": "0",
"model": "LENOVO",
"screen": "1920*1080",
"OsVersion": "10.0.19045",
"appVersion": "8.10.8.0",
"product": "deskdict",
}
response = self.proxysession.post( response = self.proxysession.post(
"https://dict.youdao.com/dicttranslate", "https://dict.youdao.com/dicttranslate",
params=param, params=param,
@ -133,13 +86,7 @@ class TS(basetrans):
] ]
) )
except: except:
raise Exception(response.json()) raise Exception(response.maybejson)
def translate(self, content): def translate(self, content):
return self.translate10_9_8(content) return self.translate10_9_8(content)
if self.config["Version"] == 0: # 10
return self.translate10_9_8(content)
if self.config["Version"] == 1: # 9
return self.translate10_9_8(content, 9)
if self.config["Version"] == 2: # 8
return self.translate10_9_8(content, 8)

View File

@ -1,5 +1,5 @@
import requests import requests
from urllib.parse import urljoin from myutils.utils import urlpathjoin
from tts.basettsclass import TTSbase from tts.basettsclass import TTSbase
from urllib.parse import quote from urllib.parse import quote
@ -7,7 +7,7 @@ from urllib.parse import quote
class TTS(TTSbase): class TTS(TTSbase):
def getvoicelist(self): def getvoicelist(self):
responseVits = requests.get( responseVits = requests.get(
urljoin(self.config["URL"], self.config["voices"]) urlpathjoin(self.config["URL"], self.config["voices"])
).json() ).json()
voicelist = [] voicelist = []
internal = [] internal = []
@ -26,6 +26,6 @@ class TTS(TTSbase):
speak = self.config["speak"].format( speak = self.config["speak"].format(
model_lower=model.lower(), model=model, id=idx, text=encoded_content model_lower=model.lower(), model=model, id=idx, text=encoded_content
) )
response = requests.get(urljoin(self.config["URL"], speak)).content response = requests.get(urlpathjoin(self.config["URL"], speak)).content
return response return response

View File

@ -211,13 +211,20 @@
"geminiocr": { "geminiocr": {
"args": { "args": {
"key": "", "key": "",
"url": "https://generativelanguage.googleapis.com/v1", "BASE_URL": "https://generativelanguage.googleapis.com/",
"model": "gemini-1.5-flash", "model": "gemini-1.5-flash",
"use_custom_prompt": false, "use_custom_prompt": false,
"modellistcache": [], "modellistcache": [],
"custom_prompt": "" "custom_prompt": ""
}, },
"argstype": { "argstype": {
"BASE_URL": {
"rank": 0
},
"key": {
"rank": 2,
"name": "API Key"
},
"custom_prompt": { "custom_prompt": {
"name": "自定义_promt", "name": "自定义_promt",
"type": "multiline", "type": "multiline",
@ -228,6 +235,7 @@
"name": "使用自定义promt" "name": "使用自定义promt"
}, },
"model": { "model": {
"rank": 3,
"type": "lineedit_or_combo", "type": "lineedit_or_combo",
"list_function": "list_models", "list_function": "list_models",
"list_cache": "modellistcache" "list_cache": "modellistcache"

View File

@ -968,6 +968,7 @@
"args": { "args": {
"SECRET_KEY": "", "SECRET_KEY": "",
"Temperature": 0.3, "Temperature": 0.3,
"BASE_URL": "https://generativelanguage.googleapis.com",
"model": "gemini-1.5-flash", "model": "gemini-1.5-flash",
"modellistcache": [], "modellistcache": [],
"context": 0, "context": 0,
@ -996,6 +997,9 @@
"refswitch": "use_other_args", "refswitch": "use_other_args",
"name": "其他参数" "name": "其他参数"
}, },
"BASE_URL": {
"rank": 0
},
"usingstream": { "usingstream": {
"name": "流式输出", "name": "流式输出",
"type": "switch", "type": "switch",