Spaces:
Runtime error
Runtime error
Commit
•
b9dacd8
0
Parent(s):
Duplicate from yizhangliu/chatGPT
Browse filesCo-authored-by: yizhangliu <yizhangliu@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +411 -0
- baidu_translate/module.py +106 -0
- encoder.json +0 -0
- encoder.py +120 -0
- requirements.txt +12 -0
- utils.py +54 -0
- vocab.bpe +0 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: ChatGPT
|
3 |
+
emoji: 📊
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.12.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: yizhangliu/chatGPT
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,411 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pyChatGPT import ChatGPT
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
import os, sys, json
|
5 |
+
from loguru import logger
|
6 |
+
import paddlehub as hub
|
7 |
+
import random
|
8 |
+
from encoder import get_encoder
|
9 |
+
|
10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
11 |
+
|
12 |
+
from utils import get_tmt_client, getTextTrans_tmt
|
13 |
+
tmt_client = get_tmt_client()
|
14 |
+
|
15 |
+
# language_translation_model = hub.Module(directory=f'./baidu_translate')
|
16 |
+
def getTextTrans(text, source='zh', target='en'):
|
17 |
+
return getTextTrans_tmt(tmt_client, text, source, target)
|
18 |
+
# def is_chinese(string):
|
19 |
+
# for ch in string:
|
20 |
+
# if u'\u4e00' <= ch <= u'\u9fff':
|
21 |
+
# return True
|
22 |
+
# return False
|
23 |
+
|
24 |
+
# if not is_chinese(text) and target == 'en':
|
25 |
+
# return text
|
26 |
+
|
27 |
+
# try:
|
28 |
+
# text_translation = language_translation_model.translate(text, source, target)
|
29 |
+
# return text_translation
|
30 |
+
# except Exception as e:
|
31 |
+
# return text
|
32 |
+
|
33 |
+
session_token = os.environ.get('SessionToken')
|
34 |
+
# logger.info(f"session_token_: {session_token}")
|
35 |
+
|
36 |
+
def get_api():
|
37 |
+
api = None
|
38 |
+
try:
|
39 |
+
api = ChatGPT(session_token)
|
40 |
+
# api.refresh_auth()
|
41 |
+
except Exception as e:
|
42 |
+
logger.info(f'get_api_error: {e}')
|
43 |
+
api = None
|
44 |
+
return api
|
45 |
+
|
46 |
+
def get_response_from_chatgpt(api, text):
|
47 |
+
if api is None:
|
48 |
+
# return "Sorry, I'm busy. Try again later.(1)"
|
49 |
+
return "Openai said: I'm too tired. Let me lie down for a few days. If you like, you can visit my home(1)."
|
50 |
+
try:
|
51 |
+
resp = api.send_message(text)
|
52 |
+
# api.refresh_auth()
|
53 |
+
# api.reset_conversation()
|
54 |
+
response = resp['message']
|
55 |
+
conversation_id = resp['conversation_id']
|
56 |
+
parent_id = resp['parent_id']
|
57 |
+
# logger.info(f"response_: {response}")
|
58 |
+
logger.info(f"conversation_id_: [{conversation_id}] / parent_id: [{parent_id}]")
|
59 |
+
except:
|
60 |
+
# response = "Sorry, I'm busy. Try again later.(2)"
|
61 |
+
response = "Openai said: I'm so tired. Let me lie down for a few days. If you like, you can visit my home(2)."
|
62 |
+
return response
|
63 |
+
|
64 |
+
token_encoder = get_encoder()
|
65 |
+
total_tokens = 4096
|
66 |
+
max_output_tokens = 1024
|
67 |
+
max_input_tokens = total_tokens - max_output_tokens
|
68 |
+
|
69 |
+
def get_response_from_openai(input, history):
|
70 |
+
def openai_create(prompt):
|
71 |
+
# no chatgpt, and from gpt-3
|
72 |
+
try:
|
73 |
+
response = openai.Completion.create(
|
74 |
+
model="text-davinci-003",
|
75 |
+
prompt=prompt,
|
76 |
+
temperature=0.9,
|
77 |
+
max_tokens=max_output_tokens,
|
78 |
+
top_p=1,
|
79 |
+
frequency_penalty=0,
|
80 |
+
presence_penalty=0.6,
|
81 |
+
stop=[" Human:", " AI:"]
|
82 |
+
)
|
83 |
+
ret = response.choices[0].text
|
84 |
+
if ret == '':
|
85 |
+
ret = "Openai said: I'm too tired. Let me lie down for a few days. If you like, you can visit my home(3)."
|
86 |
+
except Exception as e:
|
87 |
+
ret = "Openai said: I'm too tired. Let me lie down for a few days. If you like, you can visit my home(4)."
|
88 |
+
|
89 |
+
return ret
|
90 |
+
|
91 |
+
history = history or []
|
92 |
+
his= [tuple(item) for item in history]
|
93 |
+
s = list(sum(his, ()))
|
94 |
+
s.append(input)
|
95 |
+
inp = ' '.join(s)
|
96 |
+
tokens = token_encoder.encode(inp)
|
97 |
+
if len(tokens) > max_input_tokens:
|
98 |
+
new_tokens = tokens[-max_input_tokens:]
|
99 |
+
inp = token_encoder.decode(new_tokens)
|
100 |
+
# tokens_1 = token_encoder.encode(inp)
|
101 |
+
# logger.info(f"tokens_len[1]__{len(tokens)}__{len(new_tokens)}__{len(tokens_1)}")
|
102 |
+
# else:
|
103 |
+
# logger.info(f"tokens_len[0]__{len(tokens)}")
|
104 |
+
|
105 |
+
output = openai_create(inp)
|
106 |
+
return output
|
107 |
+
|
108 |
+
start_work = """async() => {
|
109 |
+
function isMobile() {
|
110 |
+
try {
|
111 |
+
document.createEvent("TouchEvent"); return true;
|
112 |
+
} catch(e) {
|
113 |
+
return false;
|
114 |
+
}
|
115 |
+
}
|
116 |
+
function getClientHeight()
|
117 |
+
{
|
118 |
+
var clientHeight=0;
|
119 |
+
if(document.body.clientHeight&&document.documentElement.clientHeight) {
|
120 |
+
var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
121 |
+
} else {
|
122 |
+
var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
123 |
+
}
|
124 |
+
return clientHeight;
|
125 |
+
}
|
126 |
+
function img_click(img) {
|
127 |
+
this_width = parseInt(img.style.width) + 20;
|
128 |
+
if (this_width > 100) {
|
129 |
+
this_width = 20;
|
130 |
+
}
|
131 |
+
img.style.width = this_width + "%";
|
132 |
+
}
|
133 |
+
function setNativeValue(element, value) {
|
134 |
+
const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;
|
135 |
+
const prototype = Object.getPrototypeOf(element);
|
136 |
+
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
|
137 |
+
|
138 |
+
if (valueSetter && valueSetter !== prototypeValueSetter) {
|
139 |
+
prototypeValueSetter.call(element, value);
|
140 |
+
} else {
|
141 |
+
valueSetter.call(element, value);
|
142 |
+
}
|
143 |
+
element.dispatchEvent(new Event('input', { bubbles: true }));
|
144 |
+
}
|
145 |
+
function save_conversation(chatbot) {
|
146 |
+
var conversations = new Array();
|
147 |
+
var conversations_noimg = new Array();
|
148 |
+
for (var i = 0; i < chatbot.children.length; i++) {
|
149 |
+
innerHTML = chatbot.children[i].innerHTML;
|
150 |
+
conversations.push(innerHTML);
|
151 |
+
if (innerHTML.indexOf("<img ") == -1) {
|
152 |
+
conversations_noimg.push(innerHTML);
|
153 |
+
}
|
154 |
+
}
|
155 |
+
var json_str = JSON.stringify(conversations);
|
156 |
+
setNativeValue(window['chat_his'], JSON.stringify(conversations_noimg));
|
157 |
+
localStorage.setItem('chatgpt_conversations', json_str);
|
158 |
+
}
|
159 |
+
function load_conversation(chatbot) {
|
160 |
+
var json_str = localStorage.getItem('chatgpt_conversations');
|
161 |
+
if (json_str) {
|
162 |
+
var conversations_noimg = new Array();
|
163 |
+
conversations = JSON.parse(json_str);
|
164 |
+
for (var i = 0; i < conversations.length; i++) {
|
165 |
+
var new_div = document.createElement("div");
|
166 |
+
if((i%2)===0){
|
167 |
+
new_div.className = "px-3 py-2 rounded-[22px] rounded-br-none text-white text-sm chat-message svelte-rct66g";
|
168 |
+
new_div.style.backgroundColor = "#16a34a";
|
169 |
+
} else {
|
170 |
+
new_div.className = "px-3 py-2 rounded-[22px] rounded-bl-none place-self-start text-white text-sm chat-message svelte-rct66g";
|
171 |
+
new_div.style.backgroundColor = "#2563eb";
|
172 |
+
if (conversations[i].indexOf("<img ") == 0) {
|
173 |
+
new_div.style.width = "20%";
|
174 |
+
new_div.onclick = function(e){
|
175 |
+
img_click(this);
|
176 |
+
}
|
177 |
+
new_div.style.padding = "0.2rem";
|
178 |
+
}
|
179 |
+
}
|
180 |
+
innerHTML = conversations[i];
|
181 |
+
new_div.innerHTML = innerHTML;
|
182 |
+
chatbot.appendChild(new_div);
|
183 |
+
|
184 |
+
if (innerHTML.indexOf("<img ") == -1) {
|
185 |
+
conversations_noimg.push(innerHTML);
|
186 |
+
}
|
187 |
+
}
|
188 |
+
setNativeValue(window['chat_his'], JSON.stringify(conversations_noimg));
|
189 |
+
}
|
190 |
+
}
|
191 |
+
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
192 |
+
if (!gradioEl) {
|
193 |
+
gradioEl = document.querySelector('body > gradio-app');
|
194 |
+
}
|
195 |
+
|
196 |
+
if (typeof window['gradioEl'] === 'undefined') {
|
197 |
+
window['gradioEl'] = gradioEl;
|
198 |
+
|
199 |
+
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
200 |
+
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
201 |
+
|
202 |
+
page1.style.display = "none";
|
203 |
+
page2.style.display = "block";
|
204 |
+
window['div_count'] = 0;
|
205 |
+
window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
|
206 |
+
window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
|
207 |
+
window['chat_his'] = window['gradioEl'].querySelectorAll('#chat_history')[0].querySelectorAll('textarea')[0];
|
208 |
+
chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0];
|
209 |
+
prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0];
|
210 |
+
window['chat_bot1'].children[1].textContent = '';
|
211 |
+
|
212 |
+
clientHeight = getClientHeight();
|
213 |
+
if (isMobile()) {
|
214 |
+
output_htmls = window['gradioEl'].querySelectorAll('.output-html');
|
215 |
+
for (var i = 0; i < output_htmls.length; i++) {
|
216 |
+
output_htmls[i].style.display = "none";
|
217 |
+
}
|
218 |
+
new_height = (clientHeight - 250) + 'px';
|
219 |
+
} else {
|
220 |
+
new_height = (clientHeight - 350) + 'px';
|
221 |
+
}
|
222 |
+
chat_row.style.height = new_height;
|
223 |
+
window['chat_bot'].style.height = new_height;
|
224 |
+
window['chat_bot'].children[2].style.height = new_height;
|
225 |
+
window['chat_bot1'].style.height = new_height;
|
226 |
+
window['chat_bot1'].children[2].style.height = new_height;
|
227 |
+
prompt_row.children[0].style.flex = 'auto';
|
228 |
+
prompt_row.children[0].style.width = '100%';
|
229 |
+
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.flex = 'auto';
|
230 |
+
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%';
|
231 |
+
prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;')
|
232 |
+
window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem');
|
233 |
+
window['gradioEl'].querySelectorAll('#btns_row')[0].children[0].setAttribute('style', 'min-width: min(10px, 100%); flex-grow: 1');
|
234 |
+
window['gradioEl'].querySelectorAll('#btns_row')[0].children[1].setAttribute('style', 'min-width: min(10px, 100%); flex-grow: 1');
|
235 |
+
|
236 |
+
load_conversation(window['chat_bot1'].children[2].children[0]);
|
237 |
+
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
238 |
+
|
239 |
+
window['gradioEl'].querySelectorAll('#clear-btn')[0].onclick = function(e){
|
240 |
+
if (confirm('Clear all outputs?')==true) {
|
241 |
+
window['chat_bot1'].children[2].children[0].innerHTML = '';
|
242 |
+
save_conversation(window['chat_bot1'].children[2].children[0]);
|
243 |
+
}
|
244 |
+
}
|
245 |
+
|
246 |
+
window['prevPrompt'] = '';
|
247 |
+
window['doCheckPrompt'] = 0;
|
248 |
+
window['prevImgSrc'] = '';
|
249 |
+
window['checkChange'] = function checkChange() {
|
250 |
+
try {
|
251 |
+
if (window['gradioEl'].querySelectorAll('.gr-radio')[0].checked) {
|
252 |
+
if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {
|
253 |
+
new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];
|
254 |
+
for (var i = 0; i < new_len; i++) {
|
255 |
+
new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);
|
256 |
+
window['chat_bot1'].children[2].children[0].appendChild(new_div);
|
257 |
+
}
|
258 |
+
window['div_count'] = chat_bot.children[2].children[0].children.length;
|
259 |
+
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
260 |
+
save_conversation(window['chat_bot1'].children[2].children[0]);
|
261 |
+
}
|
262 |
+
if (window['chat_bot'].children[0].children.length > 1) {
|
263 |
+
window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
|
264 |
+
} else {
|
265 |
+
window['chat_bot1'].children[1].textContent = '';
|
266 |
+
}
|
267 |
+
} else {
|
268 |
+
texts = window['gradioEl'].querySelectorAll('textarea');
|
269 |
+
text0 = texts[0];
|
270 |
+
text1 = texts[1];
|
271 |
+
img_index = 0;
|
272 |
+
text_value = text1.value;
|
273 |
+
if (window['doCheckPrompt'] === 0 && window['prevPrompt'] !== text_value) {
|
274 |
+
console.log('_____new prompt___[' + text_value + ']_');
|
275 |
+
window['doCheckPrompt'] = 1;
|
276 |
+
window['prevPrompt'] = text_value;
|
277 |
+
|
278 |
+
tabitems = window['gradioEl'].querySelectorAll('.tabitem');
|
279 |
+
for (var i = 0; i < tabitems.length; i++) {
|
280 |
+
inputText = tabitems[i].children[0].children[1].children[0].querySelectorAll('.gr-text-input')[0];
|
281 |
+
setNativeValue(inputText, text_value);
|
282 |
+
}
|
283 |
+
setTimeout(function() {
|
284 |
+
btns = window['gradioEl'].querySelectorAll('button');
|
285 |
+
for (var i = 0; i < btns.length; i++) {
|
286 |
+
if (['Generate image','Run'].includes(btns[i].innerText)) {
|
287 |
+
btns[i].click();
|
288 |
+
}
|
289 |
+
}
|
290 |
+
window['doCheckPrompt'] = 0;
|
291 |
+
}, 10);
|
292 |
+
}
|
293 |
+
tabitems = window['gradioEl'].querySelectorAll('.tabitem');
|
294 |
+
imgs = tabitems[img_index].children[0].children[1].children[1].querySelectorAll("img");
|
295 |
+
if (imgs.length > 0) {
|
296 |
+
if (window['prevImgSrc'] !== imgs[0].src) {
|
297 |
+
var user_div = document.createElement("div");
|
298 |
+
user_div.className = "px-3 py-2 rounded-[22px] rounded-br-none text-white text-sm chat-message svelte-rct66g";
|
299 |
+
user_div.style.backgroundColor = "#16a34a";
|
300 |
+
user_div.innerHTML = "<p>" + text0.value + "</p><img ></img>";
|
301 |
+
window['chat_bot1'].children[2].children[0].appendChild(user_div);
|
302 |
+
var bot_div = document.createElement("div");
|
303 |
+
bot_div.className = "px-3 py-2 rounded-[22px] rounded-bl-none place-self-start text-white text-sm chat-message svelte-rct66g";
|
304 |
+
bot_div.style.backgroundColor = "#2563eb";
|
305 |
+
bot_div.style.width = "40%";
|
306 |
+
bot_div.onclick = function(e){
|
307 |
+
img_click(this);
|
308 |
+
}
|
309 |
+
bot_div.style.padding = "0.2rem";
|
310 |
+
bot_div.appendChild(imgs[0].cloneNode(true));
|
311 |
+
window['chat_bot1'].children[2].children[0].appendChild(bot_div);
|
312 |
+
|
313 |
+
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
314 |
+
window['prevImgSrc'] = imgs[0].src;
|
315 |
+
save_conversation(window['chat_bot1'].children[2].children[0]);
|
316 |
+
}
|
317 |
+
}
|
318 |
+
if (tabitems[img_index].children[0].children[1].children[1].children[0].children.length > 1) {
|
319 |
+
window['chat_bot1'].children[1].textContent = tabitems[img_index].children[0].children[1].children[1].children[0].textContent;
|
320 |
+
} else {
|
321 |
+
window['chat_bot1'].children[1].textContent = '';
|
322 |
+
}
|
323 |
+
}
|
324 |
+
|
325 |
+
} catch(e) {
|
326 |
+
}
|
327 |
+
}
|
328 |
+
window['checkChange_interval'] = window.setInterval("window.checkChange()", 500);
|
329 |
+
}
|
330 |
+
|
331 |
+
return false;
|
332 |
+
}"""
|
333 |
+
|
334 |
+
space_ids = {
|
335 |
+
"spaces/stabilityai/stable-diffusion":"Stable Diffusion 2.1",
|
336 |
+
# "spaces/runwayml/stable-diffusion-v1-5":"Stable Diffusion 1.5",
|
337 |
+
# "spaces/stabilityai/stable-diffusion-1":"Stable Diffusion 1.0",
|
338 |
+
}
|
339 |
+
|
340 |
+
tab_actions = []
|
341 |
+
tab_titles = []
|
342 |
+
|
343 |
+
for space_id in space_ids.keys():
|
344 |
+
print(space_id, space_ids[space_id])
|
345 |
+
try:
|
346 |
+
tab = gr.Interface.load(space_id)
|
347 |
+
tab_actions.append(tab)
|
348 |
+
tab_titles.append(space_ids[space_id])
|
349 |
+
except Exception as e:
|
350 |
+
logger.info(f"load_fail__{space_id}_{e}")
|
351 |
+
|
352 |
+
def chat(api, input0, input1, chat_radio, chat_history):
|
353 |
+
out_chat = []
|
354 |
+
chat_history = chat_history.replace('<p>', '').replace('</p>', '')
|
355 |
+
if chat_history != '':
|
356 |
+
out_chat_1 = json.loads(chat_history)
|
357 |
+
for i in range(int(len(out_chat_1)/2)):
|
358 |
+
out_chat.append([out_chat_1[2*i], out_chat_1[2*i+1]])
|
359 |
+
|
360 |
+
# logger.info(f"out_chat_: {len(out_chat)} / {chat_radio}")
|
361 |
+
if chat_radio == "Talk to chatGPT":
|
362 |
+
# response = get_response_from_chatgpt(api, input0)
|
363 |
+
response = get_response_from_openai(input0, out_chat)
|
364 |
+
out_chat.append((input0, response))
|
365 |
+
# logger.info(f'liuyz_5___{out_chat}__')
|
366 |
+
return api, out_chat, input1
|
367 |
+
else:
|
368 |
+
prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
369 |
+
return api, out_chat, prompt_en
|
370 |
+
|
371 |
+
with gr.Blocks(title='Talk to chatGPT') as demo:
|
372 |
+
with gr.Group(elem_id="page_0", visible=True) as page_0:
|
373 |
+
gr.HTML("<p>You can duplicating this space and use your own session token: <a style='display:inline-block' href='https://huggingface.co/spaces/yizhangliu/chatGPT?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>")
|
374 |
+
gr.HTML("<p> Instruction on how to get session token can be seen in video <a style='display:inline-block' href='https://www.youtube.com/watch?v=TdNSj_qgdFk'><font style='color:blue;weight:bold;'>here</font></a>. Add your session token by going to settings and add under secrets. </p>")
|
375 |
+
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
376 |
+
with gr.Box():
|
377 |
+
with gr.Row():
|
378 |
+
start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True)
|
379 |
+
start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
|
380 |
+
|
381 |
+
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
382 |
+
with gr.Row(elem_id="chat_row"):
|
383 |
+
chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
|
384 |
+
chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
|
385 |
+
with gr.Row(elem_id="prompt_row"):
|
386 |
+
prompt_input0 = gr.Textbox(lines=2, label="prompt",show_label=False)
|
387 |
+
prompt_input1 = gr.Textbox(lines=4, label="prompt", visible=False)
|
388 |
+
chat_history = gr.Textbox(lines=4, label="prompt", elem_id="chat_history", visible=False)
|
389 |
+
chat_radio = gr.Radio(["Talk to chatGPT", "Text to Image"], elem_id="chat_radio",value="Talk to chatGPT", show_label=False, visible=True)
|
390 |
+
with gr.Row(elem_id="btns_row"):
|
391 |
+
with gr.Column(id="submit_col"):
|
392 |
+
submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
|
393 |
+
margin=True,
|
394 |
+
rounded=(True, True, True, True),
|
395 |
+
width=100
|
396 |
+
)
|
397 |
+
with gr.Column(id="clear_col"):
|
398 |
+
clear_btn = gr.Button(value = "clear outputs", elem_id="clear-btn").style(
|
399 |
+
margin=True,
|
400 |
+
rounded=(True, True, True, True),
|
401 |
+
width=100
|
402 |
+
)
|
403 |
+
api = gr.State(value=get_api())
|
404 |
+
submit_btn.click(fn=chat,
|
405 |
+
inputs=[api, prompt_input0, prompt_input1, chat_radio, chat_history],
|
406 |
+
outputs=[api, chatbot, prompt_input1],
|
407 |
+
)
|
408 |
+
with gr.Row(elem_id='tab_img', visible=False).style(height=5):
|
409 |
+
tab_img = gr.TabbedInterface(tab_actions, tab_titles)
|
410 |
+
|
411 |
+
demo.launch(debug = True)
|
baidu_translate/module.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import random, os
|
3 |
+
from hashlib import md5
|
4 |
+
from typing import Optional
|
5 |
+
|
6 |
+
import requests
|
7 |
+
|
8 |
+
import paddlehub as hub
|
9 |
+
from paddlehub.module.module import moduleinfo
|
10 |
+
from paddlehub.module.module import runnable
|
11 |
+
from paddlehub.module.module import serving
|
12 |
+
|
13 |
+
|
14 |
+
def make_md5(s, encoding='utf-8'):
|
15 |
+
return md5(s.encode(encoding)).hexdigest()
|
16 |
+
|
17 |
+
|
18 |
+
@moduleinfo(name="baidu_translate",
|
19 |
+
version="1.0.0",
|
20 |
+
type="text/machine_translation",
|
21 |
+
summary="",
|
22 |
+
author="baidu-nlp",
|
23 |
+
author_email="paddle-dev@baidu.com")
|
24 |
+
class BaiduTranslate:
|
25 |
+
|
26 |
+
def __init__(self, appid=None, appkey=None):
|
27 |
+
"""
|
28 |
+
:param appid: appid for requesting Baidu translation service.
|
29 |
+
:param appkey: appkey for requesting Baidu translation service.
|
30 |
+
"""
|
31 |
+
appid = os.environ.get('baidu_translate_appid')
|
32 |
+
appkey = os.environ.get('baidu_translate_appkey')
|
33 |
+
# Set your own appid/appkey.
|
34 |
+
if appid is None:
|
35 |
+
self.appid = ''
|
36 |
+
else:
|
37 |
+
self.appid = appid
|
38 |
+
if appkey is None:
|
39 |
+
self.appkey = ''
|
40 |
+
else:
|
41 |
+
self.appkey = appkey
|
42 |
+
self.url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
|
43 |
+
|
44 |
+
def translate(self, query: str, from_lang: Optional[str] = "en", to_lang: Optional[int] = "zh"):
|
45 |
+
"""
|
46 |
+
Create image by text prompts using ErnieVilG model.
|
47 |
+
|
48 |
+
:param query: Text to be translated.
|
49 |
+
:param from_lang: Source language.
|
50 |
+
:param to_lang: Dst language.
|
51 |
+
|
52 |
+
Return translated string.
|
53 |
+
"""
|
54 |
+
# Generate salt and sign
|
55 |
+
salt = random.randint(32768, 65536)
|
56 |
+
sign = make_md5(self.appid + query + str(salt) + self.appkey)
|
57 |
+
|
58 |
+
# Build request
|
59 |
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
60 |
+
payload = {'appid': self.appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
|
61 |
+
|
62 |
+
# Send request
|
63 |
+
try:
|
64 |
+
r = requests.post(self.url, params=payload, headers=headers)
|
65 |
+
result = r.json()
|
66 |
+
except Exception as e:
|
67 |
+
error_msg = str(e)
|
68 |
+
raise RuntimeError(error_msg)
|
69 |
+
if 'error_code' in result:
|
70 |
+
raise RuntimeError(result['error_msg'])
|
71 |
+
return result['trans_result'][0]['dst']
|
72 |
+
|
73 |
+
@runnable
|
74 |
+
def run_cmd(self, argvs):
|
75 |
+
"""
|
76 |
+
Run as a command.
|
77 |
+
"""
|
78 |
+
self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
|
79 |
+
prog='hub run {}'.format(self.name),
|
80 |
+
usage='%(prog)s',
|
81 |
+
add_help=True)
|
82 |
+
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
|
83 |
+
self.add_module_input_arg()
|
84 |
+
args = self.parser.parse_args(argvs)
|
85 |
+
if args.appid is not None and args.appkey is not None:
|
86 |
+
self.appid = args.appid
|
87 |
+
self.appkey = args.appkey
|
88 |
+
result = self.translate(args.query, args.from_lang, args.to_lang)
|
89 |
+
return result
|
90 |
+
|
91 |
+
@serving
|
92 |
+
def serving_method(self, query, from_lang, to_lang):
|
93 |
+
"""
|
94 |
+
Run as a service.
|
95 |
+
"""
|
96 |
+
return self.translate(query, from_lang, to_lang)
|
97 |
+
|
98 |
+
def add_module_input_arg(self):
|
99 |
+
"""
|
100 |
+
Add the command input options.
|
101 |
+
"""
|
102 |
+
self.arg_input_group.add_argument('--query', type=str)
|
103 |
+
self.arg_input_group.add_argument('--from_lang', type=str, default='en', help="源语言")
|
104 |
+
self.arg_input_group.add_argument('--to_lang', type=str, default='zh', help="目标语言")
|
105 |
+
self.arg_input_group.add_argument('--appid', type=str, default=None, help="注册得到的个人appid")
|
106 |
+
self.arg_input_group.add_argument('--appkey', type=str, default=None, help="注册得到的个人appkey")
|
encoder.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
encoder.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file includes code which was modified from https://github.com/openai/gpt-2
|
2 |
+
|
3 |
+
import tensorflow as tf
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import regex as re
|
7 |
+
from functools import lru_cache
|
8 |
+
import requests
|
9 |
+
import boto3
|
10 |
+
import pdb
|
11 |
+
|
12 |
+
|
13 |
+
@lru_cache()
|
14 |
+
def bytes_to_unicode():
|
15 |
+
|
16 |
+
bs = (
|
17 |
+
list(range(ord("!"), ord("~") + 1))
|
18 |
+
+ list(range(ord("¡"), ord("¬") + 1))
|
19 |
+
+ list(range(ord("®"), ord("ÿ") + 1))
|
20 |
+
)
|
21 |
+
cs = bs[:]
|
22 |
+
n = 0
|
23 |
+
for b in range(2 ** 8):
|
24 |
+
if b not in bs:
|
25 |
+
bs.append(b)
|
26 |
+
cs.append(2 ** 8 + n)
|
27 |
+
n += 1
|
28 |
+
cs = [chr(n) for n in cs]
|
29 |
+
return dict(zip(bs, cs))
|
30 |
+
|
31 |
+
|
32 |
+
def get_pairs(word):
|
33 |
+
pairs = set()
|
34 |
+
prev_char = word[0]
|
35 |
+
for char in word[1:]:
|
36 |
+
pairs.add((prev_char, char))
|
37 |
+
prev_char = char
|
38 |
+
return pairs
|
39 |
+
|
40 |
+
|
41 |
+
class Encoder:
|
42 |
+
def __init__(self, encoder, bpe_merges, errors="replace"):
|
43 |
+
self.encoder = encoder
|
44 |
+
self.decoder = {v: k for k, v in self.encoder.items()}
|
45 |
+
self.errors = errors
|
46 |
+
self.byte_encoder = bytes_to_unicode()
|
47 |
+
self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
|
48 |
+
self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
|
49 |
+
self.cache = {}
|
50 |
+
self.pat = re.compile(
|
51 |
+
r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""
|
52 |
+
)
|
53 |
+
|
54 |
+
def bpe(self, token):
|
55 |
+
if token in self.cache:
|
56 |
+
return self.cache[token]
|
57 |
+
word = tuple(token)
|
58 |
+
|
59 |
+
pairs = get_pairs(word)
|
60 |
+
|
61 |
+
if not pairs:
|
62 |
+
return token
|
63 |
+
|
64 |
+
while True:
|
65 |
+
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf")))
|
66 |
+
if bigram not in self.bpe_ranks:
|
67 |
+
break
|
68 |
+
first, second = bigram
|
69 |
+
new_word = []
|
70 |
+
i = 0
|
71 |
+
while i < len(word):
|
72 |
+
try:
|
73 |
+
j = word.index(first, i)
|
74 |
+
new_word.extend(word[i:j])
|
75 |
+
i = j
|
76 |
+
except:
|
77 |
+
new_word.extend(word[i:])
|
78 |
+
break
|
79 |
+
|
80 |
+
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
|
81 |
+
new_word.append(first + second)
|
82 |
+
i += 2
|
83 |
+
else:
|
84 |
+
new_word.append(word[i])
|
85 |
+
i += 1
|
86 |
+
new_word = tuple(new_word)
|
87 |
+
word = new_word
|
88 |
+
if len(word) == 1:
|
89 |
+
break
|
90 |
+
else:
|
91 |
+
pairs = get_pairs(word)
|
92 |
+
|
93 |
+
word = " ".join(word)
|
94 |
+
self.cache[token] = word
|
95 |
+
return word
|
96 |
+
|
97 |
+
def encode(self, text):
|
98 |
+
bpe_tokens = []
|
99 |
+
for token in re.findall(self.pat, text):
|
100 |
+
token = "".join(self.byte_encoder[b] for b in token.encode("utf-8"))
|
101 |
+
|
102 |
+
bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(" "))
|
103 |
+
return bpe_tokens
|
104 |
+
|
105 |
+
def decode(self, tokens):
|
106 |
+
text = "".join([self.decoder[token] for token in tokens])
|
107 |
+
text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors)
|
108 |
+
return text
|
109 |
+
|
110 |
+
|
111 |
+
def get_encoder():
|
112 |
+
with open("encoder.json", "r") as f:
|
113 |
+
encoder = json.load(f)
|
114 |
+
with open("vocab.bpe", "r", encoding="utf-8") as f:
|
115 |
+
bpe_data = f.read()
|
116 |
+
bpe_merges = [tuple(merge_str.split()) for merge_str in bpe_data.split("\n")[1:-1]]
|
117 |
+
return Encoder(encoder=encoder, bpe_merges=bpe_merges)
|
118 |
+
|
119 |
+
# encoder = get_encoder()
|
120 |
+
# print('encoded is ', encoder.encode('hello 👋 world 🌍 This is a long string to test whether or not the emoji issue was fixed!'))
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pyChatGPT
|
2 |
+
openai
|
3 |
+
loguru
|
4 |
+
paddlepaddle==2.3.2
|
5 |
+
paddlehub
|
6 |
+
# transformers
|
7 |
+
# torch
|
8 |
+
tensorflow
|
9 |
+
regex
|
10 |
+
boto3
|
11 |
+
gradio==3.12.0
|
12 |
+
tencentcloud-sdk-python
|
utils.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json, os
|
2 |
+
from tencentcloud.common import credential
|
3 |
+
from tencentcloud.common.profile.client_profile import ClientProfile
|
4 |
+
from tencentcloud.common.profile.http_profile import HttpProfile
|
5 |
+
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
6 |
+
from tencentcloud.tmt.v20180321 import tmt_client, models
|
7 |
+
|
8 |
+
def get_tmt_client():
|
9 |
+
try:
|
10 |
+
# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
11 |
+
# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
12 |
+
# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
13 |
+
SecretId = os.environ.get("TENCENTCLOUD_SECRET_ID")
|
14 |
+
SecretKey = os.environ.get("TENCENTCLOUD_SECRET_KEY")
|
15 |
+
cred = credential.Credential(SecretId, SecretKey)
|
16 |
+
# 实例化一个http选项,可选的,没有特殊需求可以跳过
|
17 |
+
httpProfile = HttpProfile()
|
18 |
+
httpProfile.endpoint = "tmt.tencentcloudapi.com"
|
19 |
+
|
20 |
+
# 实例化一个client选项,可选的,没有特殊需求可以跳过
|
21 |
+
clientProfile = ClientProfile()
|
22 |
+
clientProfile.httpProfile = httpProfile
|
23 |
+
# 实例化要请求产品的client对象,clientProfile是可选的
|
24 |
+
client = tmt_client.TmtClient(cred, "ap-shanghai", clientProfile)
|
25 |
+
print(f'client_{client}')
|
26 |
+
return client
|
27 |
+
except TencentCloudSDKException as err:
|
28 |
+
print(f'client_err_{err}')
|
29 |
+
return None
|
30 |
+
|
31 |
+
def getTextTrans_tmt(tmt_client, text, source='zh', target='en'):
|
32 |
+
def is_chinese(string):
|
33 |
+
for ch in string:
|
34 |
+
if u'\u4e00' <= ch <= u'\u9fff':
|
35 |
+
return True
|
36 |
+
return False
|
37 |
+
|
38 |
+
if tmt_client is None:
|
39 |
+
return text
|
40 |
+
if not is_chinese(text) and target == 'en':
|
41 |
+
return text
|
42 |
+
try:
|
43 |
+
req = models.TextTranslateRequest()
|
44 |
+
params = {
|
45 |
+
"SourceText": text,
|
46 |
+
"Source": source,
|
47 |
+
"Target": target,
|
48 |
+
"ProjectId": 0
|
49 |
+
}
|
50 |
+
req.from_json_string(json.dumps(params))
|
51 |
+
resp = tmt_client.TextTranslate(req)
|
52 |
+
return resp.TargetText
|
53 |
+
except Exception as e:
|
54 |
+
return text
|
vocab.bpe
ADDED
The diff for this file is too large to render.
See raw diff
|
|