from huggingface_hub import InferenceClient import gradio as gr import os APIKEY = os.environ["MT_TOKEN"] EP = os.environ["MT_EP"] import requests def translate(source_lang,target_lang,text): params = { 'auth_key' : APIKEY, 'text' : text, 'source_lang' : source_lang, "target_lang": target_lang } request = requests.post(EP, data=params) result = request.json() return result["translations"][0]["text"] def translate_en_to_ja(text): if text is None: return None return translate("EN","JA",text) def translate_ja_to_en(text): if text is None: return None return translate("JA","EN",text) client = InferenceClient( "mistralai/Mistral-7B-Instruct-v0.1" ) message_en = None real_history = [] def format_prompt(message, history): global message_en print(message) message_en = translate_ja_to_en(message) print(message_en) prompt = "" for user_prompt, bot_response in real_history: prompt += f"[INST] {user_prompt} [/INST]" prompt += f" {bot_response} " prompt += f"[INST] {message_en} [/INST]" return prompt def generate( prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, ): temperature = float(temperature) if temperature < 1e-2: temperature = 1e-2 top_p = float(top_p) generate_kwargs = dict( temperature=temperature, max_new_tokens=max_new_tokens, top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, ) formatted_prompt = format_prompt(prompt, history) output = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=True, return_full_text=False) last_eng_respose = output.generated_text real_history.append([message_en, last_eng_respose]) return translate_en_to_ja(output.generated_text) additional_inputs=[ gr.Slider( label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs", ), gr.Slider( label="Max new tokens", value=256, minimum=0, maximum=1048, step=64, interactive=True, info="The maximum numbers of new tokens", ), gr.Slider( label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens", ), gr.Slider( label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens", ) ] css = """ #mkd { height: 500px; overflow: auto; border: 1px solid #ccc; } """ with gr.Blocks(css=css) as demo: gr.HTML("

Mistral 7B + Japanese MT

") gr.HTML("

Mistral-7B-Instruct の入出力に機械翻訳をかけたものです。💬

") gr.HTML("

モデルの詳細についてはここから. 📚

") gr.ChatInterface( generate, additional_inputs=additional_inputs, examples=[["人生の秘密は何ですか?"], ["パンケーキのレシピを書いて。"]] ) demo.queue().launch(debug=True)