parent
34b4889e56
commit
845e38cae8
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -9,7 +9,7 @@ from scripts.physton_prompt.storage import Storage
|
|||
from scripts.physton_prompt.get_extensions import get_extensions
|
||||
from scripts.physton_prompt.get_token_counter import get_token_counter
|
||||
from scripts.physton_prompt.get_i18n import get_i18n
|
||||
from scripts.physton_prompt.get_translate_apis import get_translate_apis
|
||||
from scripts.physton_prompt.get_translate_apis import get_translate_apis, privacy_translate_api_config, unprotected_translate_api_config
|
||||
from scripts.physton_prompt.translate import translate
|
||||
from scripts.physton_prompt.history import History
|
||||
from scripts.physton_prompt.csv import get_csvs, get_csv
|
||||
|
|
@ -105,7 +105,9 @@ def on_app_started(_: gr.Blocks, app: FastAPI):
|
|||
|
||||
@app.get("/physton_prompt/get_data")
|
||||
async def _get_data(key: str):
|
||||
return {"data": st.get(key)}
|
||||
data = st.get(key)
|
||||
data = privacy_translate_api_config(key, data)
|
||||
return {"data": data}
|
||||
|
||||
@app.get("/physton_prompt/get_datas")
|
||||
async def _get_datas(keys: str):
|
||||
|
|
@ -113,6 +115,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI):
|
|||
datas = {}
|
||||
for key in keys:
|
||||
datas[key] = st.get(key)
|
||||
datas[key] = privacy_translate_api_config(key, datas[key])
|
||||
return {"datas": datas}
|
||||
|
||||
@app.post("/physton_prompt/set_data")
|
||||
|
|
@ -122,6 +125,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI):
|
|||
return {"success": False, "message": get_lang('is_required', {'0': 'key'})}
|
||||
if 'data' not in data:
|
||||
return {"success": False, "message": get_lang('is_required', {'0': 'data'})}
|
||||
data['data'] = unprotected_translate_api_config(data['key'], data['data'])
|
||||
st.set(data['key'], data['data'])
|
||||
return {"success": True}
|
||||
|
||||
|
|
@ -131,6 +135,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI):
|
|||
if not isinstance(data, dict):
|
||||
return {"success": False, "message": get_lang('is_not_dict', {'0': 'data'})}
|
||||
for key in data:
|
||||
data[key] = unprotected_translate_api_config(key, data[key])
|
||||
st.set(key, data[key])
|
||||
return {"success": True}
|
||||
|
||||
|
|
@ -288,9 +293,19 @@ def on_app_started(_: gr.Blocks, app: FastAPI):
|
|||
return {"success": hi.remove_histories(data['type'])}
|
||||
|
||||
@app.post("/physton_prompt/translate")
|
||||
async def _translate(text: str = Body(...), from_lang: str = Body(...), to_lang: str = Body(...),
|
||||
api: str = Body(...), api_config: dict = Body(...)):
|
||||
return translate(text, from_lang, to_lang, api, api_config)
|
||||
async def _translate(request: Request):
|
||||
data = await request.json()
|
||||
if 'text' not in data:
|
||||
return {"success": False, "message": get_lang('is_required', {'0': 'text'})}
|
||||
if 'from_lang' not in data:
|
||||
return {"success": False, "message": get_lang('is_required', {'0': 'from_lang'})}
|
||||
if 'to_lang' not in data:
|
||||
return {"success": False, "message": get_lang('is_required', {'0': 'to_lang'})}
|
||||
if 'api' not in data:
|
||||
return {"success": False, "message": get_lang('is_required', {'0': 'api'})}
|
||||
if 'api_config' not in data:
|
||||
return {"success": False, "message": get_lang('is_required', {'0': 'api_config'})}
|
||||
return translate(data['text'], data['from_lang'], data['to_lang'], data['api'], data['api_config'])
|
||||
|
||||
@app.post("/physton_prompt/translates")
|
||||
async def _translates(request: Request):
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
from scripts.physton_prompt.get_lang import get_lang
|
||||
from scripts.physton_prompt.get_translate_apis import unprotected_translate_api_config
|
||||
|
||||
|
||||
def gen_openai(messages, api_config):
|
||||
import openai
|
||||
api_config = unprotected_translate_api_config('chatgpt_key', api_config)
|
||||
openai.api_base = api_config.get('api_base', 'https://api.openai.com/v1')
|
||||
openai.api_key = api_config.get('api_key', '')
|
||||
model = api_config.get('model', 'gpt-3.5-turbo')
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
import os
|
||||
import json
|
||||
import re
|
||||
from scripts.physton_prompt.storage import Storage
|
||||
st = Storage()
|
||||
|
||||
# from scripts.physton_prompt.storage import Storage
|
||||
|
||||
|
|
@ -36,3 +39,80 @@ def get_translate_apis(reload=False):
|
|||
# config_item['value'] = ''
|
||||
|
||||
return translate_apis
|
||||
|
||||
|
||||
def privacy_translate_api_config(data_key, data):
|
||||
# 如果 data 为空或者不是 dict
|
||||
if not data or not isinstance(data, dict):
|
||||
return data
|
||||
# 如果 data_key 是 translate_api. 开头
|
||||
api = None
|
||||
if data_key == 'chatgpt_key':
|
||||
api = 'openai'
|
||||
else:
|
||||
start = 'translate_api.'
|
||||
if not data_key.startswith(start):
|
||||
return data
|
||||
api = data_key[len(start):]
|
||||
apis = get_translate_apis()
|
||||
find = False
|
||||
for group in apis['apis']:
|
||||
for item in group['children']:
|
||||
if item['key'] == api:
|
||||
find = item
|
||||
break
|
||||
if not find:
|
||||
return data
|
||||
api_item = find
|
||||
for config in api_item['config']:
|
||||
# 如果有 privacy 的属性并且为 True
|
||||
if 'privacy' in config and config['privacy'] and config['type'] == 'input':
|
||||
if config['key'] in data:
|
||||
# 前面6个字符可见,后面的字符用 * 替换
|
||||
value = data[config['key']]
|
||||
if len(value) > 6:
|
||||
value = value[:6] + '*' * (len(value) - 6)
|
||||
data[config['key']] = value
|
||||
|
||||
return data
|
||||
|
||||
def unprotected_translate_api_config(data_key, data):
|
||||
api = None
|
||||
if data_key == 'chatgpt_key':
|
||||
api = 'openai'
|
||||
else:
|
||||
start = 'translate_api.'
|
||||
if not data_key.startswith(start):
|
||||
return data
|
||||
api = data_key[len(start):]
|
||||
|
||||
apis = get_translate_apis()
|
||||
find = False
|
||||
for group in apis['apis']:
|
||||
for item in group['children']:
|
||||
if item['key'] == api:
|
||||
find = item
|
||||
break
|
||||
if not find:
|
||||
return data
|
||||
api_item = find
|
||||
|
||||
storage_data = st.get(data_key)
|
||||
|
||||
for config in api_item['config']:
|
||||
# 如果有 privacy 的属性并且为 True
|
||||
if 'privacy' in config and config['privacy'] and config['type'] == 'input':
|
||||
if storage_data and config['key'] in storage_data:
|
||||
if config['key'] in data:
|
||||
value = data[config['key']]
|
||||
# 如果包含 * 号,并且前面6个字符等于 storage_data 的前面6个字符
|
||||
if '*' in value and value[:6] == storage_data[config['key']][:6]:
|
||||
data[config['key']] = storage_data[config['key']]
|
||||
|
||||
# 多个 * 替换成一个 *
|
||||
# value = re.sub(r'\*+', '*', value)
|
||||
# if value == '*' and storage_data and config['key'] in storage_data:
|
||||
# value = storage_data[config['key']]
|
||||
# data[config['key']] = value
|
||||
|
||||
return data
|
||||
|
|
@ -53,6 +53,8 @@ class Storage:
|
|||
filename = self.__get_data_filename(key)
|
||||
if not os.path.exists(filename):
|
||||
return None
|
||||
if os.path.getsize(filename) == 0:
|
||||
return None
|
||||
try:
|
||||
import launch
|
||||
if not launch.is_installed("chardet"):
|
||||
|
|
@ -65,8 +67,12 @@ class Storage:
|
|||
encoding = chardet.detect(data).get('encoding')
|
||||
data = json.loads(data.decode(encoding))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return None
|
||||
try:
|
||||
with open(filename, 'r') as f:
|
||||
data = json.load(f)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return None
|
||||
return data
|
||||
|
||||
def __set(self, key, data):
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import json
|
||||
import hashlib
|
||||
from scripts.physton_prompt.get_lang import get_lang
|
||||
from scripts.physton_prompt.get_translate_apis import get_translate_apis
|
||||
from scripts.physton_prompt.get_translate_apis import get_translate_apis, unprotected_translate_api_config
|
||||
from scripts.physton_prompt.translator.alibaba_translator import AlibabaTranslator
|
||||
from scripts.physton_prompt.translator.amazon_translator import AmazonTranslator
|
||||
from scripts.physton_prompt.translator.baidu_translator import BaiduTranslator
|
||||
|
|
@ -37,8 +37,7 @@ def translate(text, from_lang, to_lang, api, api_config=None):
|
|||
"translated_text": translated_text,
|
||||
"from_lang": from_lang,
|
||||
"to_lang": to_lang,
|
||||
"api": api,
|
||||
"api_config": api_config
|
||||
"api": api
|
||||
}
|
||||
|
||||
def _cache_name(text):
|
||||
|
|
@ -124,7 +123,7 @@ def translate(text, from_lang, to_lang, api, api_config=None):
|
|||
|
||||
translator.set_from_lang(from_lang)
|
||||
translator.set_to_lang(to_lang)
|
||||
translator.set_api_config(api_config)
|
||||
translator.set_api_config(unprotected_translate_api_config('translate_api.' + api, api_config))
|
||||
|
||||
if isinstance(text, list):
|
||||
translate_texts = []
|
||||
|
|
|
|||
|
|
@ -154,15 +154,16 @@ export default {
|
|||
configs[item.key] = res.chatgpt_key[item.key] || item.default || ''
|
||||
}
|
||||
} else {
|
||||
if (res['translate_api.openai'] && res['translate_api.openai'].api_key) {
|
||||
/*if (res['translate_api.openai'] && res['translate_api.openai'].api_key) {
|
||||
for (const item of api.config) {
|
||||
configs[item.key] = res['translate_api.openai'][item.key] || item.default || ''
|
||||
}
|
||||
} else {
|
||||
for (const item of api.config) {
|
||||
configs[item.key] = item.default || ''
|
||||
}
|
||||
} else {*/
|
||||
for (const item of api.config) {
|
||||
configs[item.key] = item.default || ''
|
||||
}
|
||||
/*}*/
|
||||
// this.gradioAPI.setData('chatgpt_key', configs)
|
||||
}
|
||||
if (!configs['api_key']) {
|
||||
this.hidePanels['api'] = false
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
import os
|
||||
import sys
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
||||
from scripts.physton_prompt.storage import Storage
|
||||
from scripts.physton_prompt.get_translate_apis import privacy_translate_api_config, unprotected_translate_api_config
|
||||
st = Storage()
|
||||
key = 'translate_api.volcengine'
|
||||
data = st.get(key)
|
||||
data = privacy_translate_api_config(key, data)
|
||||
print(data)
|
||||
data = unprotected_translate_api_config(key, data)
|
||||
print(data)
|
||||
|
||||
data = {
|
||||
'key': 'translate_api.volcengine',
|
||||
'data': {
|
||||
'access_key_id': 'AKLTYz*****************************************',
|
||||
'access_key_secret': 'TWpVNV******************************************************',
|
||||
'region': 'cn-north-1',
|
||||
}
|
||||
}
|
||||
data['data'] = unprotected_translate_api_config(data['key'], data['data'])
|
||||
print(data)
|
||||
|
|
@ -1827,7 +1827,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -1987,7 +1988,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "model",
|
||||
|
|
@ -2150,7 +2152,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "region",
|
||||
|
|
@ -2260,12 +2263,14 @@
|
|||
{
|
||||
"key": "api_key_id",
|
||||
"title": "API Key ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "api_key_secret",
|
||||
"title": "API Key Secret",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "region",
|
||||
|
|
@ -2372,7 +2377,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -2476,7 +2482,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -2607,7 +2614,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -2672,12 +2680,14 @@
|
|||
{
|
||||
"key": "app_id",
|
||||
"title": "APP ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "app_secret",
|
||||
"title": "APP Secret",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -2808,12 +2818,14 @@
|
|||
{
|
||||
"key": "access_key_id",
|
||||
"title": "Access Key ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "access_key_secret",
|
||||
"title": "Access Key Secret",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "region",
|
||||
|
|
@ -2968,12 +2980,14 @@
|
|||
{
|
||||
"key": "app_id",
|
||||
"title": "App ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "app_secret",
|
||||
"title": "App Secret",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -3028,12 +3042,14 @@
|
|||
{
|
||||
"key": "secret_id",
|
||||
"title": "Secret ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "secret_key",
|
||||
"title": "Secret Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "region",
|
||||
|
|
@ -3190,7 +3206,8 @@
|
|||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -3230,7 +3247,8 @@
|
|||
{
|
||||
"key": "token",
|
||||
"title": "Token",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -3347,12 +3365,14 @@
|
|||
{
|
||||
"key": "access_key_id",
|
||||
"title": "Access Key ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "access_key_secret",
|
||||
"title": "Secret Access Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "region",
|
||||
|
|
@ -3460,17 +3480,20 @@
|
|||
{
|
||||
"key": "app_id",
|
||||
"title": "APP ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "api_secret",
|
||||
"title": "API Secret",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -3544,17 +3567,20 @@
|
|||
{
|
||||
"key": "app_id",
|
||||
"title": "APP ID",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "api_secret",
|
||||
"title": "API Secret",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
},
|
||||
{
|
||||
"key": "api_key",
|
||||
"title": "API Key",
|
||||
"type": "input"
|
||||
"type": "input",
|
||||
"privacy": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue