Commit
•
ee91e52
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 +253 -0
- baidu_translate/module.py +104 -0
- requirements.txt +4 -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,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pyChatGPT import ChatGPT
|
2 |
+
import gradio as gr
|
3 |
+
import os, sys, json
|
4 |
+
from loguru import logger
|
5 |
+
import paddlehub as hub
|
6 |
+
import random
|
7 |
+
|
8 |
+
language_translation_model = hub.Module(directory=f'./baidu_translate')
|
9 |
+
def getTextTrans(text, source='zh', target='en'):
|
10 |
+
try:
|
11 |
+
text_translation = language_translation_model.translate(text, source, target)
|
12 |
+
return text_translation
|
13 |
+
except Exception as e:
|
14 |
+
return text
|
15 |
+
|
16 |
+
session_token = os.environ.get('SessionToken')
|
17 |
+
# logger.info(f"session_token_: {session_token}")
|
18 |
+
|
19 |
+
def get_response_from_chatbot(text):
|
20 |
+
try:
|
21 |
+
api = ChatGPT(session_token)
|
22 |
+
resp = api.send_message(text)
|
23 |
+
api.refresh_auth()
|
24 |
+
api.reset_conversation()
|
25 |
+
response = resp['message']
|
26 |
+
# logger.info(f"response_: {response}")
|
27 |
+
except:
|
28 |
+
response = "Sorry, I'm busy. Try again later."
|
29 |
+
return response
|
30 |
+
|
31 |
+
model_ids = {
|
32 |
+
# "models/stabilityai/stable-diffusion-2-1":"sd-v2-1",
|
33 |
+
# "models/stabilityai/stable-diffusion-2":"sd-v2-0",
|
34 |
+
# "models/runwayml/stable-diffusion-v1-5":"sd-v1-5",
|
35 |
+
# "models/CompVis/stable-diffusion-v1-4":"sd-v1-4",
|
36 |
+
"models/prompthero/openjourney":"openjourney",
|
37 |
+
# "models/ShadoWxShinigamI/Midjourney-Rangoli":"midjourney",
|
38 |
+
# "models/hakurei/waifu-diffusion":"waifu-diffusion",
|
39 |
+
# "models/Linaqruf/anything-v3.0":"anything-v3.0",
|
40 |
+
}
|
41 |
+
|
42 |
+
tab_actions = []
|
43 |
+
tab_titles = []
|
44 |
+
for model_id in model_ids.keys():
|
45 |
+
print(model_id, model_ids[model_id])
|
46 |
+
try:
|
47 |
+
tab = gr.Interface.load(model_id)
|
48 |
+
tab_actions.append(tab)
|
49 |
+
tab_titles.append(model_ids[model_id])
|
50 |
+
except:
|
51 |
+
logger.info(f"load_fail__{model_id}_")
|
52 |
+
|
53 |
+
def chat(input0, input1, chat_radio, chat_history):
|
54 |
+
out_chat = []
|
55 |
+
if chat_history != '':
|
56 |
+
out_chat = json.loads(chat_history)
|
57 |
+
logger.info(f"out_chat_: {len(out_chat)} / {chat_radio}")
|
58 |
+
if chat_radio == "Talk to chatGPT":
|
59 |
+
response = get_response_from_chatbot(input0)
|
60 |
+
out_chat.append((input0, response))
|
61 |
+
chat_history = json.dumps(out_chat)
|
62 |
+
return out_chat, input1, chat_history
|
63 |
+
else:
|
64 |
+
prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
65 |
+
return out_chat, prompt_en, chat_history
|
66 |
+
|
67 |
+
|
68 |
+
start_work = """async() => {
|
69 |
+
function isMobile() {
|
70 |
+
try {
|
71 |
+
document.createEvent("TouchEvent"); return true;
|
72 |
+
} catch(e) {
|
73 |
+
return false;
|
74 |
+
}
|
75 |
+
}
|
76 |
+
function getClientHeight()
|
77 |
+
{
|
78 |
+
var clientHeight=0;
|
79 |
+
if(document.body.clientHeight&&document.documentElement.clientHeight) {
|
80 |
+
var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
81 |
+
} else {
|
82 |
+
var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
83 |
+
}
|
84 |
+
return clientHeight;
|
85 |
+
}
|
86 |
+
|
87 |
+
function setNativeValue(element, value) {
|
88 |
+
const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;
|
89 |
+
const prototype = Object.getPrototypeOf(element);
|
90 |
+
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
|
91 |
+
|
92 |
+
if (valueSetter && valueSetter !== prototypeValueSetter) {
|
93 |
+
prototypeValueSetter.call(element, value);
|
94 |
+
} else {
|
95 |
+
valueSetter.call(element, value);
|
96 |
+
}
|
97 |
+
}
|
98 |
+
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
99 |
+
if (!gradioEl) {
|
100 |
+
gradioEl = document.querySelector('body > gradio-app');
|
101 |
+
}
|
102 |
+
|
103 |
+
if (typeof window['gradioEl'] === 'undefined') {
|
104 |
+
window['gradioEl'] = gradioEl;
|
105 |
+
|
106 |
+
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
107 |
+
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
108 |
+
|
109 |
+
page1.style.display = "none";
|
110 |
+
page2.style.display = "block";
|
111 |
+
|
112 |
+
window['div_count'] = 0;
|
113 |
+
window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0];
|
114 |
+
window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0];
|
115 |
+
chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0];
|
116 |
+
prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0];
|
117 |
+
window['chat_bot1'].children[1].textContent = '';
|
118 |
+
|
119 |
+
clientHeight = getClientHeight();
|
120 |
+
if (isMobile()) {
|
121 |
+
output_htmls = window['gradioEl'].querySelectorAll('.output-html');
|
122 |
+
for (var i = 0; i < output_htmls.length; i++) {
|
123 |
+
output_htmls[i].style.display = "none";
|
124 |
+
}
|
125 |
+
new_height = (clientHeight - 250) + 'px';
|
126 |
+
} else {
|
127 |
+
new_height = (clientHeight - 350) + 'px';
|
128 |
+
}
|
129 |
+
chat_row.style.height = new_height;
|
130 |
+
window['chat_bot'].style.height = new_height;
|
131 |
+
window['chat_bot'].children[2].style.height = new_height;
|
132 |
+
window['chat_bot1'].style.height = new_height;
|
133 |
+
window['chat_bot1'].children[2].style.height = new_height;
|
134 |
+
prompt_row.children[0].style.flex = 'auto';
|
135 |
+
prompt_row.children[0].style.width = '100%';
|
136 |
+
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.flex = 'auto';
|
137 |
+
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%';
|
138 |
+
prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;')
|
139 |
+
window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem;');
|
140 |
+
|
141 |
+
window['prevPrompt'] = '';
|
142 |
+
window['doCheckPrompt'] = 0;
|
143 |
+
window['prevImgSrc'] = '';
|
144 |
+
window['checkChange'] = function checkChange() {
|
145 |
+
try {
|
146 |
+
if (window['gradioEl'].querySelectorAll('.gr-radio')[0].checked) {
|
147 |
+
if (window['chat_bot'].children[2].children[0].children.length > window['div_count']) {
|
148 |
+
new_len = window['chat_bot'].children[2].children[0].children.length - window['div_count'];
|
149 |
+
for (var i = 0; i < new_len; i++) {
|
150 |
+
new_div = window['chat_bot'].children[2].children[0].children[window['div_count'] + i].cloneNode(true);
|
151 |
+
window['chat_bot1'].children[2].children[0].appendChild(new_div);
|
152 |
+
}
|
153 |
+
window['div_count'] = chat_bot.children[2].children[0].children.length;
|
154 |
+
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
155 |
+
}
|
156 |
+
if (window['chat_bot'].children[0].children.length > 1) {
|
157 |
+
window['chat_bot1'].children[1].textContent = window['chat_bot'].children[0].children[1].textContent;
|
158 |
+
} else {
|
159 |
+
window['chat_bot1'].children[1].textContent = '';
|
160 |
+
}
|
161 |
+
} else {
|
162 |
+
texts = window['gradioEl'].querySelectorAll('textarea');
|
163 |
+
text0 = texts[0];
|
164 |
+
text1 = texts[1];
|
165 |
+
img_index = 0;
|
166 |
+
if (window['doCheckPrompt'] === 0 && window['prevPrompt'] !== text1.value) {
|
167 |
+
console.log('_____new prompt___[' + text1.value + ']_');
|
168 |
+
window['doCheckPrompt'] = 1;
|
169 |
+
window['prevPrompt'] = text1.value;
|
170 |
+
for (var i = 3; i < texts.length; i++) {
|
171 |
+
setNativeValue(texts[i], text1.value);
|
172 |
+
texts[i].dispatchEvent(new Event('input', { bubbles: true }));
|
173 |
+
}
|
174 |
+
setTimeout(function() {
|
175 |
+
img_submit_btns = window['gradioEl'].querySelectorAll('#tab_img')[0].querySelectorAll("button");
|
176 |
+
for (var i = 0; i < img_submit_btns.length; i++) {
|
177 |
+
if (img_submit_btns[i].innerText == 'Submit') {
|
178 |
+
img_submit_btns[i].click();
|
179 |
+
}
|
180 |
+
}
|
181 |
+
window['doCheckPrompt'] = 0;
|
182 |
+
}, 10);
|
183 |
+
}
|
184 |
+
tabitems = window['gradioEl'].querySelectorAll('.tabitem');
|
185 |
+
imgs = tabitems[img_index].children[0].children[1].children[1].children[0].querySelectorAll("img");
|
186 |
+
if (imgs.length > 0) {
|
187 |
+
if (window['prevImgSrc'] !== imgs[0].src) {
|
188 |
+
var user_div = document.createElement("div");
|
189 |
+
user_div.className = "px-3 py-2 rounded-[22px] rounded-br-none text-white text-sm chat-message svelte-rct66g";
|
190 |
+
user_div.style.backgroundColor = "#16a34a";
|
191 |
+
user_div.innerHTML = "<p>" + text0.value + "</p>";
|
192 |
+
window['chat_bot1'].children[2].children[0].appendChild(user_div);
|
193 |
+
|
194 |
+
var bot_div = document.createElement("div");
|
195 |
+
bot_div.className = "px-3 py-2 rounded-[22px] rounded-bl-none place-self-start text-white text-sm chat-message svelte-rct66g";
|
196 |
+
bot_div.style.backgroundColor = "#2563eb";
|
197 |
+
bot_div.style.width = "80%";
|
198 |
+
bot_div.style.padding = "0.2rem";
|
199 |
+
bot_div.appendChild(imgs[0].cloneNode(true));
|
200 |
+
window['chat_bot1'].children[2].children[0].appendChild(bot_div);
|
201 |
+
|
202 |
+
window['chat_bot1'].children[2].scrollTop = window['chat_bot1'].children[2].scrollHeight;
|
203 |
+
window['prevImgSrc'] = imgs[0].src;
|
204 |
+
}
|
205 |
+
}
|
206 |
+
if (tabitems[img_index].children[0].children[1].children[1].children[0].children[0].children.length > 1) {
|
207 |
+
window['chat_bot1'].children[1].textContent = tabitems[img_index].children[0].children[1].children[1].children[0].children[0].children[1].textContent;
|
208 |
+
} else {
|
209 |
+
window['chat_bot1'].children[1].textContent = '';
|
210 |
+
}
|
211 |
+
}
|
212 |
+
|
213 |
+
} catch(e) {
|
214 |
+
}
|
215 |
+
}
|
216 |
+
window['checkChange_interval'] = window.setInterval("window.checkChange()", 500);
|
217 |
+
}
|
218 |
+
|
219 |
+
return false;
|
220 |
+
}"""
|
221 |
+
|
222 |
+
|
223 |
+
with gr.Blocks(title='Talk to chatGPT') as demo:
|
224 |
+
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>")
|
225 |
+
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>")
|
226 |
+
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
227 |
+
with gr.Box():
|
228 |
+
with gr.Row():
|
229 |
+
start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True)
|
230 |
+
start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
|
231 |
+
|
232 |
+
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
233 |
+
with gr.Row(elem_id="chat_row"):
|
234 |
+
chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
|
235 |
+
chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))
|
236 |
+
with gr.Row(elem_id="prompt_row"):
|
237 |
+
prompt_input0 = gr.Textbox(lines=2, label="prompt",show_label=False)
|
238 |
+
prompt_input1 = gr.Textbox(lines=4, label="prompt", visible=False)
|
239 |
+
chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
|
240 |
+
chat_radio = gr.Radio(["Talk to chatGPT", "Text to Image"], elem_id="chat_radio",value="Talk to chatGPT", show_label=False)
|
241 |
+
submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style(
|
242 |
+
margin=True,
|
243 |
+
rounded=(True, True, True, True),
|
244 |
+
width=100
|
245 |
+
)
|
246 |
+
submit_btn.click(fn=chat,
|
247 |
+
inputs=[prompt_input0, prompt_input1, chat_radio, chat_history],
|
248 |
+
outputs=[chatbot, prompt_input1, chat_history],
|
249 |
+
)
|
250 |
+
with gr.Row(elem_id='tab_img', visible=False).style(height=5):
|
251 |
+
tab_img = gr.TabbedInterface(tab_actions, tab_titles)
|
252 |
+
|
253 |
+
demo.launch(debug = True)
|
baidu_translate/module.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import random
|
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 |
+
# Set your own appid/appkey.
|
32 |
+
if appid == None:
|
33 |
+
self.appid = '20201015000580007'
|
34 |
+
else:
|
35 |
+
self.appid = appid
|
36 |
+
if appkey is None:
|
37 |
+
self.appkey = 'IFJB6jBORFuMmVGDRud1'
|
38 |
+
else:
|
39 |
+
self.appkey = appkey
|
40 |
+
self.url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
|
41 |
+
|
42 |
+
def translate(self, query: str, from_lang: Optional[str] = "en", to_lang: Optional[int] = "zh"):
|
43 |
+
"""
|
44 |
+
Create image by text prompts using ErnieVilG model.
|
45 |
+
|
46 |
+
:param query: Text to be translated.
|
47 |
+
:param from_lang: Source language.
|
48 |
+
:param to_lang: Dst language.
|
49 |
+
|
50 |
+
Return translated string.
|
51 |
+
"""
|
52 |
+
# Generate salt and sign
|
53 |
+
salt = random.randint(32768, 65536)
|
54 |
+
sign = make_md5(self.appid + query + str(salt) + self.appkey)
|
55 |
+
|
56 |
+
# Build request
|
57 |
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
58 |
+
payload = {'appid': self.appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
|
59 |
+
|
60 |
+
# Send request
|
61 |
+
try:
|
62 |
+
r = requests.post(self.url, params=payload, headers=headers)
|
63 |
+
result = r.json()
|
64 |
+
except Exception as e:
|
65 |
+
error_msg = str(e)
|
66 |
+
raise RuntimeError(error_msg)
|
67 |
+
if 'error_code' in result:
|
68 |
+
raise RuntimeError(result['error_msg'])
|
69 |
+
return result['trans_result'][0]['dst']
|
70 |
+
|
71 |
+
@runnable
|
72 |
+
def run_cmd(self, argvs):
|
73 |
+
"""
|
74 |
+
Run as a command.
|
75 |
+
"""
|
76 |
+
self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
|
77 |
+
prog='hub run {}'.format(self.name),
|
78 |
+
usage='%(prog)s',
|
79 |
+
add_help=True)
|
80 |
+
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
|
81 |
+
self.add_module_input_arg()
|
82 |
+
args = self.parser.parse_args(argvs)
|
83 |
+
if args.appid is not None and args.appkey is not None:
|
84 |
+
self.appid = args.appid
|
85 |
+
self.appkey = args.appkey
|
86 |
+
result = self.translate(args.query, args.from_lang, args.to_lang)
|
87 |
+
return result
|
88 |
+
|
89 |
+
@serving
|
90 |
+
def serving_method(self, query, from_lang, to_lang):
|
91 |
+
"""
|
92 |
+
Run as a service.
|
93 |
+
"""
|
94 |
+
return self.translate(query, from_lang, to_lang)
|
95 |
+
|
96 |
+
def add_module_input_arg(self):
|
97 |
+
"""
|
98 |
+
Add the command input options.
|
99 |
+
"""
|
100 |
+
self.arg_input_group.add_argument('--query', type=str)
|
101 |
+
self.arg_input_group.add_argument('--from_lang', type=str, default='en', help="源语言")
|
102 |
+
self.arg_input_group.add_argument('--to_lang', type=str, default='zh', help="目标语言")
|
103 |
+
self.arg_input_group.add_argument('--appid', type=str, default=None, help="注册得到的个人appid")
|
104 |
+
self.arg_input_group.add_argument('--appkey', type=str, default=None, help="注册得到的个人appkey")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pyChatGPT
|
2 |
+
loguru
|
3 |
+
paddlepaddle==2.3.2
|
4 |
+
paddlehub
|