Spaces:
Build error
Build error
Upload 8 files
Browse filesFiles changed (7)
- README.md+4-4
- app.py+511-14
- encoder.jsonAdded
- encoder.pyAdded+120
- requirements.txt+11-1
- utils.pyAdded+54
- vocab.bpeAdded
123453456778910---title: ChatGPTemoji: 💻colorFrom: purplecolorTo: grayemoji: 📊colorFrom: bluecolorTo: bluesdk: gradiosdk_version: 3.21.0sdk_version: 3.12.0app_file: app.pypinned: false---11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604461462463464465466467468469470471472473474475476477478479648048148248348448548648748848949049149249349449549649749849950050150250350489101112131415165055065075085095105115125135145151819516import gradio as grimport os, sys, jsonos.system("pip install gradio==3.19.1")import openaiimport gradio as gr
from loguru import loggerimport paddlehub as hubimport randomfrom encoder import get_encoder
openai.api_key = os.getenv("OPENAI_API_KEY")
from utils import get_tmt_client, getTextTrans_tmttmt_client = get_tmt_client()
def getTextTrans(text, source='zh', target='en'): def is_chinese(string): for ch in string: if u'\u4e00' <= ch <= u'\u9fff': return True return False if not is_chinese(text) and target == 'en': return text try: text_translation = getTextTrans_tmt(tmt_client, text, source, target) return text_translation except Exception as e: return text
start_work = """async() => { function isMobile() { try { document.createEvent("TouchEvent"); return true; } catch(e) { return false; } } function getClientHeight() { var clientHeight=0; if(document.body.clientHeight&&document.documentElement.clientHeight) { var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight; } else { var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight; } return clientHeight; } function setNativeValue(element, value) { const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set; const prototype = Object.getPrototypeOf(element); const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set; if (valueSetter && valueSetter !== prototypeValueSetter) { prototypeValueSetter.call(element, value); } else { valueSetter.call(element, value); } element.dispatchEvent(new Event('input', { bubbles: true })); } function get_clear_innerHTML(innerHTML) { innerHTML = innerHTML.replace(/<p>|<\\/p>|\\n/g, ''); regexp = /\\★☆(.*?)\\☆★/; match = innerHTML.match(regexp); if (match) { innerHTML = match[1]; } return innerHTML; } function save_conversation(chatbot) { var conversations = new Array(); var conversations_clear = new Array(); for (var i = 0; i < chatbot.children.length; i++) { testid_icon = '☟:'; //'user' if (chatbot.children[i].dataset['testid'] == 'bot') { testid_icon = '☝:'; //'bot' } innerHTML = chatbot.children[i].innerHTML; conversations.push(testid_icon + innerHTML); if (innerHTML.indexOf("<img") == -1 && innerHTML.indexOf("null_") == -1) { conversations_clear.push(testid_icon + get_clear_innerHTML(innerHTML)); } } var json_str = JSON.stringify(conversations); setNativeValue(window['chat_his'], JSON.stringify(conversations_clear)); localStorage.setItem('chatgpt_conversations', json_str); } function img_click(img) { this_width = parseInt(img.style.width) + 20; if (this_width > 100) { this_width = 20; } img.style.width = this_width + "%"; img.style.height = img.offsetWidth + 'px'; } function load_conversation(chatbot) { var json_str = localStorage.getItem('chatgpt_conversations'); if (json_str) { var conversations_clear = new Array(); conversations = JSON.parse(json_str); for (var i = 0; i < conversations.length; i++) { innerHTML = conversations[i]; if (innerHTML.indexOf("☝:") == -1) { className = "message user svelte-134zwfa"; bgcolor = "#16a34a"; testid = "user"; testid_icon = '☟:'; //'user' } else { className = "message bot svelte-134zwfa"; bgcolor = "#2563eb"; testid = "bot"; testid_icon = '☝:'; //'bot' } var new_div = document.createElement("div"); new_div.className = className; new_div.style.backgroundColor = bgcolor; new_div.dataset.testid = testid; if (innerHTML.indexOf("data:image/jpeg") >= 0) { new_div.style.width = "20%"; new_div.style.padding = "0.2rem"; new_div.onclick = function(e) { img_click(this); } setTimeout(function(){ new_div.style.height = new_div.offsetWidth + 'px'; new_div.children[0].setAttribute('style', 'max-width: none; width:100%'); }, 10); } innerHTML = innerHTML.replace("☝:", ""); innerHTML = innerHTML.replace("☟:", ""); new_div.innerHTML = innerHTML; if (innerHTML.indexOf("null_") != -1) { new_div.style.display = 'none'; } chatbot.appendChild(new_div); if (innerHTML.indexOf("<img") == -1 && innerHTML.indexOf("null_") == -1) { conversations_clear.push(testid_icon + get_clear_innerHTML(innerHTML)); } } setNativeValue(window['chat_his'], JSON.stringify(conversations_clear)); setTimeout(function(){ window['chat_bot1'].children[1].scrollTop = window['chat_bot1'].children[1].scrollHeight; }, 500); } } var gradioEl = document.querySelector('body > gradio-app').shadowRoot; if (!gradioEl) { gradioEl = document.querySelector('body > gradio-app'); } if (typeof window['gradioEl'] === 'undefined') { window['gradioEl'] = gradioEl; const page1 = window['gradioEl'].querySelectorAll('#page_1')[0]; const page2 = window['gradioEl'].querySelectorAll('#page_2')[0]; page1.style.display = "none"; page2.style.display = "block"; window['div_count'] = 0; window['chat_radio_0'] = window['gradioEl'].querySelectorAll('#chat_radio')[0].querySelectorAll('input[name=radio-chat_radio]')[0]; window['chat_radio_1'] = window['gradioEl'].querySelectorAll('#chat_radio')[0].querySelectorAll('input[name=radio-chat_radio]')[1]; window['chat_bot'] = window['gradioEl'].querySelectorAll('#chat_bot')[0]; window['chat_bot1'] = window['gradioEl'].querySelectorAll('#chat_bot1')[0]; window['my_prompt'] = window['gradioEl'].querySelectorAll('#my_prompt')[0].querySelectorAll('textarea')[0]; window['my_prompt_en'] = window['gradioEl'].querySelectorAll('#my_prompt_en')[0].querySelectorAll('textarea')[0]; window['chat_his'] = window['gradioEl'].querySelectorAll('#chat_history')[0].querySelectorAll('textarea')[0]; chat_row = window['gradioEl'].querySelectorAll('#chat_row')[0]; prompt_row = window['gradioEl'].querySelectorAll('#prompt_row')[0]; window['chat_bot1'].children[1].children[0].textContent = ''; clientHeight = getClientHeight(); if (isMobile()) { output_htmls = window['gradioEl'].querySelectorAll('.output-html'); for (var i = 0; i < output_htmls.length; i++) { output_htmls[i].style.display = "none"; } new_height = (clientHeight - 250) + 'px'; } else { new_height = (clientHeight - 350) + 'px'; } chat_row.style.height = new_height; window['chat_bot'].style.height = new_height; window['chat_bot'].children[1].style.height = new_height; window['chat_bot1'].style.height = new_height; window['chat_bot1'].children[1].style.height = new_height; window['chat_bot1'].children[0].style.top = (parseInt(window['chat_bot1'].style.height)-window['chat_bot1'].children[0].offsetHeight-2) + 'px'; prompt_row.children[0].style.flex = 'auto'; prompt_row.children[0].style.width = '100%'; window['gradioEl'].querySelectorAll('#chat_radio')[0].style.flex = 'auto'; window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%'; prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;') window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem'); window['gradioEl'].querySelectorAll('#btns_row')[0].children[0].setAttribute('style', 'min-width: min(10px, 100%); flex-grow: 1'); window['gradioEl'].querySelectorAll('#btns_row')[0].children[1].setAttribute('style', 'min-width: min(10px, 100%); flex-grow: 1'); load_conversation(window['chat_bot1'].children[1].children[0]); window['chat_bot1'].children[1].scrollTop = window['chat_bot1'].children[1].scrollHeight; window['gradioEl'].querySelectorAll('#clear-btn')[0].onclick = function(e){ if (confirm('Clear all outputs?')==true) { for (var i = window['chat_bot'].children[1].children[0].children.length-1; i >= 0; i--) { window['chat_bot'].children[1].children[0].removeChild(window['chat_bot'].children[1].children[0].children[i]); } for (var i = window['chat_bot1'].children[1].children[0].children.length-1; i >= 0; i--) { window['chat_bot1'].children[1].children[0].removeChild(window['chat_bot1'].children[1].children[0].children[i]); } window['div_count'] = 0; save_conversation(window['chat_bot1'].children[1].children[0]); } } function set_buttons(action) { window['submit-btn'].disabled = action; window['clear-btn'].disabled = action; window['chat_radio_0'].disabled = action; window['chat_radio_1'].disabled = action; btn_color = 'color:#000'; if (action) { btn_color = 'color:#ccc'; } window['submit-btn'].setAttribute('style', btn_color); window['clear-btn'].setAttribute('style', btn_color); window['chat_radio_0'].setAttribute('style', btn_color); window['chat_radio_1'].setAttribute('style', btn_color); } window['prevPrompt'] = ''; window['doCheckPrompt'] = 0; window['prevImgSrc'] = ''; window['checkChange'] = function checkChange() { try { if (window['chat_radio_0'].checked) { dot_flashing = window['chat_bot'].children[1].children[0].querySelectorAll('.dot-flashing'); if (window['chat_bot'].children[1].children[0].children.length > window['div_count'] && dot_flashing.length == 0) { new_len = window['chat_bot'].children[1].children[0].children.length - window['div_count']; for (var i = 0; i < new_len; i++) { new_div = window['chat_bot'].children[1].children[0].children[window['div_count'] + i].cloneNode(true); window['chat_bot1'].children[1].children[0].appendChild(new_div); } window['div_count'] = window['chat_bot'].children[1].children[0].children.length; window['chat_bot1'].children[1].scrollTop = window['chat_bot1'].children[1].scrollHeight; save_conversation(window['chat_bot1'].children[1].children[0]); } if (window['chat_bot'].children[0].children.length > 1) { set_buttons(true); window['chat_bot1'].children[0].textContent = window['chat_bot'].children[0].children[1].textContent; } else { set_buttons(false); window['chat_bot1'].children[0].textContent = ''; } } else { img_index = 0; draw_prompt_en = window['my_prompt_en'].value; if (window['doCheckPrompt'] == 0 && window['prevPrompt'] != draw_prompt_en) { console.log('_____draw_prompt_en___[' + draw_prompt_en + ']_'); window['doCheckPrompt'] = 1; window['prevPrompt'] = draw_prompt_en; tabitems = window['gradioEl'].querySelectorAll('.tabitem'); for (var i = 0; i < tabitems.length; i++) { inputText = tabitems[i].children[0].children[1].children[0].querySelectorAll('input')[0]; setNativeValue(inputText, draw_prompt_en); } setTimeout(function() { window['draw_prompt'] = window['my_prompt'].value; btns = window['gradioEl'].querySelectorAll('button'); for (var i = 0; i < btns.length; i++) { if (['Generate image','Run'].includes(btns[i].innerText)) { btns[i].click(); } } window['doCheckPrompt'] = 0; }, 10); } tabitems = window['gradioEl'].querySelectorAll('.tabitem'); imgs = tabitems[img_index].children[0].children[1].children[1].querySelectorAll("img"); if (imgs.length > 0) { if (window['prevImgSrc'] !== imgs[0].src) { var user_div = document.createElement("div"); user_div.className = "message user svelte-134zwfa"; user_div.style.backgroundColor = "#16a34a"; user_div.dataset.testid = 'user'; user_div.innerHTML = "<p>作画: " + window['draw_prompt'] + "</p><img></img>"; window['chat_bot1'].children[1].children[0].appendChild(user_div); var bot_div = document.createElement("div"); bot_div.className = "message bot svelte-134zwfa"; bot_div.style.backgroundColor = "#2563eb"; bot_div.style.width = "20%"; bot_div.dataset.testid = 'bot'; bot_div.onclick = function(e){ img_click(this); } setTimeout(function(){ bot_div.style.height = bot_div.offsetWidth + 'px'; bot_div.children[0].setAttribute('style', 'max-width:none; width:100%'); }, 10); bot_div.style.padding = "0.2rem"; bot_div.appendChild(imgs[0].cloneNode(true)); window['chat_bot1'].children[1].children[0].appendChild(bot_div); window['chat_bot1'].children[1].scrollTop = window['chat_bot1'].children[1].scrollHeight; window['prevImgSrc'] = imgs[0].src; save_conversation(window['chat_bot1'].children[1].children[0]); } } if (tabitems[img_index].children[0].children[1].children[1].children[0].children.length > 1) { tips = tabitems[img_index].children[0].children[1].children[1].children[0].textContent; if (tips.indexOf("Error") == -1) { set_buttons(true); } else { set_buttons(false); } window['chat_bot1'].children[0].textContent = '作画中 ' + tips; } else { set_buttons(false); window['chat_bot1'].children[0].textContent = ''; } } } catch(e) { } } window['checkChange_interval'] = window.setInterval("window.checkChange()", 500); } return false;}"""
space_ids = { "spaces/stabilityai/stable-diffusion":"Stable Diffusion 2.1", # "spaces/runwayml/stable-diffusion-v1-5":"Stable Diffusion 1.5", # "spaces/stabilityai/stable-diffusion-1":"Stable Diffusion 1.0", }
tab_actions = []tab_titles = []
for space_id in space_ids.keys(): print(space_id, space_ids[space_id]) try: tab = gr.Interface.load(space_id) tab_actions.append(tab) tab_titles.append(space_ids[space_id]) except Exception as e: logger.info(f"load_fail__{space_id}_{e}") token_encoder = get_encoder()total_tokens = 4096max_output_tokens = 1024max_input_tokens = total_tokens - max_output_tokens
def set_openai_api_key(api_key): if api_key and api_key.startswith("sk-") and len(api_key) > 50: openai.api_key = api_key
def get_response_from_openai(input, chat_history, model_radio): error_1 = 'You exceeded your current quota, please check your plan and billing details.' def openai_create(input_list, model_radio): try: # print(f'input_list={input_list}') input_list_len = len(input_list) out_prompt = '' messages = [] if model_radio == 'GPT-3.0': out_prompt = 'AI:' for i in range(input_list_len): input = input_list[input_list_len-i-1].replace("<br>", '\n\n') if input.startswith("Openai said:"): input = "☝:" if input.startswith("☝:"): if model_radio == 'GPT-3.0': out_prompt = input.replace("☝:", "AI:") + '\n' + out_prompt else: out_prompt = input.replace("☝:", "") + out_prompt messages.insert(0, {"role": "assistant", "content": input.replace("☝:", "")}) elif input.startswith("☟:"): if model_radio == 'GPT-3.0': out_prompt = input.replace("☟:", "Human:") + '\n' + out_prompt else: out_prompt = input.replace("☟:", "") + out_prompt messages.insert(0, {"role": "user", "content": input.replace("☟:", "")}) tokens = token_encoder.encode(out_prompt) if len(tokens) > max_input_tokens: break if model_radio == 'GPT-3.0': # print(out_prompt) response = openai.Completion.create( model="text-davinci-003", prompt=out_prompt, temperature=0.7, max_tokens=max_output_tokens, top_p=1, frequency_penalty=0, presence_penalty=0, stop=[" Human:", " AI:"] ) # print(f'response_3.0__:{response}') ret = response.choices[0].text else: # print(messages) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, temperature=0.7, max_tokens=max_output_tokens, top_p=1, frequency_penalty=0, presence_penalty=0, stop=[" Human:", " AI:"] ) # print(f'response_3.5__:{response}') ret = response.choices[0].message['content'] if ret.startswith("\n\n"): ret = ret.replace("\n\n", '') ret = ret.replace('\n', '<br>') if ret == '': ret = f"Openai said: I'm too tired." return ret, response.usage except Exception as e: logger.info(f"openai_create_error__{e}") ret = f"Openai said: {e} Perhaps enter your OpenAI API key." return ret, {"completion_tokens": -1, "prompt_tokens": -1, "total_tokens": -1} # logger.info(f'chat_history = {chat_history}') chat_history_list = [] chat_history = chat_history.replace("<p>", "").replace("</p>", "") if chat_history != '': chat_history_list = json.loads(chat_history) chat_history_list.append(f'☟:{input}') output, response_usage = openai_create(chat_history_list, model_radio) logger.info(f'response_usage={response_usage}') return output def chat(input0, input1, chat_radio, model_radio, all_chat_history, chat_history): all_chat = [] if all_chat_history != '': all_chat = json.loads(all_chat_history)
if len(input0) == 0: return all_chat, json.dumps(all_chat), input0, input1 if chat_radio == "Talk to chatGPT": response = get_response_from_openai(input0, chat_history, model_radio) all_chat.append((input0, response)) return all_chat, json.dumps(all_chat), '', input1 else: prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}' return all_chat, json.dumps(all_chat), input0, prompt_en def chat_radio_change(chat_radio): if chat_radio == "Talk to chatGPT": return gr.Radio.update(visible=True), gr.Text.update(visible=True) else: return gr.Radio.update(visible=False), gr.Text.update(visible=False)
openai.api_key = "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"with gr.Blocks(title='Talk to chatGPT') as demo: with gr.Row(elem_id="page_0", visible=False) as page_0: 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>") with gr.Group(elem_id="page_1", visible=True) as page_1: with gr.Box(): with gr.Row(): start_button = gr.Button("Let's talk to chatGPT!", elem_id="start-btn", visible=True) start_button.click(fn=None, inputs=[], outputs=[], _js=start_work) with gr.Row(elem_id="page_2", visible=False) as page_2: with gr.Row(elem_id="chat_row"): chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue")) chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue")) with gr.Row(elem_id="prompt_row"): prompt_input0 = gr.Textbox(lines=2, label="input", elem_id="my_prompt", show_label=True) prompt_input1 = gr.Textbox(lines=4, label="prompt", elem_id="my_prompt_en", visible=False) chat_history = gr.Textbox(lines=4, label="chat_history", elem_id="chat_history", visible=False) all_chat_history = gr.Textbox(lines=4, label="会话上下文:", elem_id="all_chat_history", visible=False)
messages = [{"role": "system", "content": "You are a financial experts that specializes in real estate investment and negotiation"}] chat_radio = gr.Radio(["Talk to chatGPT", "Text to Image"], elem_id="chat_radio",value="Talk to chatGPT", show_label=False, visible=True) model_radio = gr.Radio(["GPT-3.0", "GPT-3.5"], elem_id="model_radio", value="GPT-3.5", label='GPT model: ', show_label=True,interactive=True, visible=True) openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...) and hit Enter", show_label=False, lines=1, type='password') with gr.Row(elem_id="btns_row"): with gr.Column(id="submit_col"): submit_btn = gr.Button(value = "submit",elem_id="submit-btn").style( margin=True, rounded=(True, True, True, True), width=100 ) with gr.Column(id="clear_col"): clear_btn = gr.Button(value = "clear outputs", elem_id="clear-btn").style( margin=True, rounded=(True, True, True, True), width=100 ) submit_btn.click(fn=chat, inputs=[prompt_input0, prompt_input1, chat_radio, model_radio, all_chat_history, chat_history], outputs=[chatbot, all_chat_history, prompt_input0, prompt_input1], ) with gr.Row(elem_id='tab_img', visible=False).style(height=5): tab_img = gr.TabbedInterface(tab_actions, tab_titles)
def CustomChatGPT(user_input): messages.append({"role": "user", "content": user_input}) response = openai.ChatCompletion.create( model = "gpt-3.5-turbo", messages = messages ) ChatGPT_reply = response["choices"][0]["message"]["content"] messages.append({"role": "assistant", "content": ChatGPT_reply}) return ChatGPT_reply openai_api_key_textbox.change(set_openai_api_key, inputs=[openai_api_key_textbox], outputs=[]) openai_api_key_textbox.submit(set_openai_api_key, inputs=[openai_api_key_textbox], outputs=[]) chat_radio.change(fn=chat_radio_change, inputs=[chat_radio], outputs=[model_radio, openai_api_key_textbox], )
iface = gr.Interface(fn=CustomChatGPT, inputs="text", outputs="text", title = "ChatBot")iface.launch()demo.launch(debug = True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120# This file includes code which was modified from https://github.com/openai/gpt-2
import tensorflow as tfimport osimport jsonimport regex as refrom functools import lru_cacheimport requestsimport boto3import pdb
@lru_cache()def bytes_to_unicode():
bs = ( list(range(ord("!"), ord("~") + 1)) + list(range(ord("¡"), ord("¬") + 1)) + list(range(ord("®"), ord("ÿ") + 1)) ) cs = bs[:] n = 0 for b in range(2 ** 8): if b not in bs: bs.append(b) cs.append(2 ** 8 + n) n += 1 cs = [chr(n) for n in cs] return dict(zip(bs, cs))
def get_pairs(word): pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs
class Encoder: def __init__(self, encoder, bpe_merges, errors="replace"): self.encoder = encoder self.decoder = {v: k for k, v in self.encoder.items()} self.errors = errors self.byte_encoder = bytes_to_unicode() self.byte_decoder = {v: k for k, v in self.byte_encoder.items()} self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges)))) self.cache = {} self.pat = re.compile( r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""" )
def bpe(self, token): if token in self.cache: return self.cache[token] word = tuple(token)
pairs = get_pairs(word)
if not pairs: return token
while True: bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float("inf"))) if bigram not in self.bpe_ranks: break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) new_word.extend(word[i:j]) i = j except: new_word.extend(word[i:]) break
if word[i] == first and i < len(word) - 1 and word[i + 1] == second: new_word.append(first + second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word if len(word) == 1: break else: pairs = get_pairs(word)
word = " ".join(word) self.cache[token] = word return word
def encode(self, text): bpe_tokens = [] for token in re.findall(self.pat, text): token = "".join(self.byte_encoder[b] for b in token.encode("utf-8"))
bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(" ")) return bpe_tokens
def decode(self, tokens): text = "".join([self.decoder[token] for token in tokens]) text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text
def get_encoder(): with open("encoder.json", "r") as f: encoder = json.load(f) with open("vocab.bpe", "r", encoding="utf-8") as f: bpe_data = f.read() bpe_merges = [tuple(merge_str.split()) for merge_str in bpe_data.split("\n")[1:-1]] return Encoder(encoder=encoder, bpe_merges=bpe_merges)
# encoder = get_encoder()# print('encoded is ', encoder.encode('hello 👋 world 🌍 This is a long string to test whether or not the emoji issue was fixed!'))11234567891011openaiopenailogurupaddlepaddle==2.3.2paddlehub# transformers# torchtensorflowregexboto3gradio==3.19.1tencentcloud-sdk-python123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import json, osfrom tencentcloud.common import credentialfrom tencentcloud.common.profile.client_profile import ClientProfilefrom tencentcloud.common.profile.http_profile import HttpProfilefrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.tmt.v20180321 import tmt_client, models
def get_tmt_client(): try: # 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 # 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 # 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 SecretId = os.environ.get("TENCENTCLOUD_SECRET_ID") SecretKey = os.environ.get("TENCENTCLOUD_SECRET_KEY") cred = credential.Credential(SecretId, SecretKey) # 实例化一个http选项,可选的,没有特殊需求可以跳过 httpProfile = HttpProfile() httpProfile.endpoint = "tmt.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品的client对象,clientProfile是可选的 client = tmt_client.TmtClient(cred, "ap-shanghai", clientProfile) print(f'client_{client}') return client except TencentCloudSDKException as err: print(f'client_err_{err}') return None
def getTextTrans_tmt(tmt_client, text, source='zh', target='en'): def is_chinese(string): for ch in string: if u'\u4e00' <= ch <= u'\u9fff': return True return False
if tmt_client is None: return text if not is_chinese(text) and target == 'en': return text try: req = models.TextTranslateRequest() params = { "SourceText": text, "Source": source, "Target": target, "ProjectId": 0 } req.from_json_string(json.dumps(params)) resp = tmt_client.TextTranslate(req) return resp.TargetText except Exception as e: return text README.md+4-4
app.py+511-14
encoder.jsonAdded
encoder.pyAdded+120
requirements.txt+11-1
utils.pyAdded+54
vocab.bpeAdded