gojiteji commited on
Commit
c0be996
·
1 Parent(s): 88c296d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -32,14 +32,18 @@ client = InferenceClient(
32
  "mistralai/Mistral-7B-Instruct-v0.1"
33
  )
34
 
35
-
 
36
  def format_prompt(message, history):
 
 
 
 
37
  prompt = "<s>"
38
- print(history)
39
- for user_prompt, bot_response in history:
40
  prompt += f"[INST] {user_prompt} [/INST]"
41
  prompt += f" {bot_response}</s> "
42
- prompt += f"[INST] {message} [/INST]"
43
  return prompt
44
 
45
  def generate(
@@ -61,13 +65,10 @@ def generate(
61
 
62
  formatted_prompt = format_prompt(prompt, history)
63
 
64
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
65
- output = ""
66
-
67
- for response in stream:
68
- output += response.token.text
69
- yield output
70
- return output
71
 
72
 
73
  additional_inputs=[
@@ -118,13 +119,13 @@ css = """
118
  """
119
 
120
  with gr.Blocks(css=css) as demo:
121
- gr.HTML("<h1><center>Mistral 7B Instruct<h1><center>")
122
- gr.HTML("<h3><center>In this demo, you can chat with <a href='https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1'>Mistral-7B-Instruct</a> model. 💬<h3><center>")
123
- gr.HTML("<h3><center>Learn more about the model <a href='https://huggingface.co/docs/transformers/main/model_doc/mistral'>here</a>. 📚<h3><center>")
124
  gr.ChatInterface(
125
  generate,
126
  additional_inputs=additional_inputs,
127
- examples=[["What is the secret to life?"], ["Write me a recipe for pancakes."]]
128
  )
129
 
130
  demo.queue().launch(debug=True)
 
32
  "mistralai/Mistral-7B-Instruct-v0.1"
33
  )
34
 
35
+ message_en = None
36
+ real_history = []
37
  def format_prompt(message, history):
38
+ global message_en
39
+ message_en = translate_ja_to_en(message)
40
+
41
+ #history の日本語部分をlast_eng_resposeに置き換える
42
  prompt = "<s>"
43
+ for user_prompt, bot_response in real_history:
 
44
  prompt += f"[INST] {user_prompt} [/INST]"
45
  prompt += f" {bot_response}</s> "
46
+ prompt += f"[INST] {message_en} [/INST]"
47
  return prompt
48
 
49
  def generate(
 
65
 
66
  formatted_prompt = format_prompt(prompt, history)
67
 
68
+ output = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, details=True, return_full_text=False)
69
+ last_eng_respose = output.generated_text
70
+ real_history.append([message_en, last_eng_respose])
71
+ return translate_en_to_ja(output.generated_text)
 
 
 
72
 
73
 
74
  additional_inputs=[
 
119
  """
120
 
121
  with gr.Blocks(css=css) as demo:
122
+ gr.HTML("<h1><center>Mistral 7B + Japanese MT <h1><center>")
123
+ gr.HTML("<h3><center><a href='https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1'>Mistral-7B-Instruct</a> に機械翻訳をかけたものです。💬<h3><center>")
124
+ gr.HTML("<h3><center>モデルの詳細については<a href='https://huggingface.co/docs/transformers/main/model_doc/mistral'>ここから</a>. 📚<h3><center>")
125
  gr.ChatInterface(
126
  generate,
127
  additional_inputs=additional_inputs,
128
+ examples=[["人生の秘密は何ですか?"], ["パンケーキのレシピを書いて。"]]
129
  )
130
 
131
  demo.queue().launch(debug=True)