zhangtao commited on
Commit
728ac62
1 Parent(s): 5b3edb5

增加中文翻译功能

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -0
  2. app.py +82 -25
Dockerfile CHANGED
@@ -7,6 +7,8 @@ WORKDIR /code
7
 
8
  RUN wget https://huggingface.co/TheBloke/NeuralHermes-2.5-Mistral-7B-GGUF/resolve/main/neuralhermes-2.5-mistral-7b.Q5_K_M.gguf?download=true -O neuralhermes-2.5-mistral-7b.Q5_K_M.gguf
9
 
 
 
10
  COPY ./requirements.txt /code/requirements.txt
11
 
12
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
 
7
 
8
  RUN wget https://huggingface.co/TheBloke/NeuralHermes-2.5-Mistral-7B-GGUF/resolve/main/neuralhermes-2.5-mistral-7b.Q5_K_M.gguf?download=true -O neuralhermes-2.5-mistral-7b.Q5_K_M.gguf
9
 
10
+ RUN wget https://huggingface.co/zhangtao103239/Qwen-1.8B-GGUF/resolve/main/qwen-1.8b-q5_k_m.gguf?download=true -O qwen-1.8b-q5_k_m.gguf
11
+
12
  COPY ./requirements.txt /code/requirements.txt
13
 
14
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
app.py CHANGED
@@ -1,45 +1,102 @@
1
  import gradio as gr
2
  from llama_cpp import Llama
3
  import json
 
4
  llm = Llama(model_path="./neuralhermes-2.5-mistral-7b.Q5_K_M.gguf",
5
- n_ctx=32768,
6
  n_threads=2,
7
  chat_format="chatml")
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
9
 
10
- def chat_completion(messages, history, system_prompt):
 
 
 
 
 
 
 
 
11
  messages_prompts = [{"role": "system", "content": system_prompt}]
12
  for human, assistant in history:
13
  messages_prompts.append({"role": "user", "content": human})
14
  messages_prompts.append({"role": "assistant", "content": assistant})
15
- messages_prompts.append({"role": "user", "content": messages})
16
  response = llm.create_chat_completion(
17
  messages=messages_prompts,
18
  stream=False
19
  )
20
  print(json.dumps(response, ensure_ascii=False, indent=2))
21
- return response['choices'][0]['content']
22
 
23
 
24
- def chat_stream_completion(messages, history, system_prompt):
25
  messages_prompts = [{"role": "system", "content": system_prompt}]
26
- for human, assistant in history:
27
- messages_prompts.append({"role": "user", "content": human})
28
- messages_prompts.append({"role": "assistant", "content": assistant})
29
- messages_prompts.append({"role": "user", "content": messages})
30
- response = llm.create_chat_completion(
31
- messages=messages_prompts,
32
- stream=True
33
- )
34
- partial_message = ""
35
- for chunk in response:
36
- if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
37
- partial_message = partial_message + \
38
- chunk['choices'][0]["delta"]["content"]
39
- yield partial_message
40
-
41
-
42
- gr.ChatInterface(chat_stream_completion,
43
- additional_inputs=[gr.Textbox(
44
- "You are helpful AI.", label="System Prompt")]
45
- ).queue().launch(server_name="0.0.0.0")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
  import json
4
+ import time
5
  llm = Llama(model_path="./neuralhermes-2.5-mistral-7b.Q5_K_M.gguf",
6
+ n_ctx=1024,
7
  n_threads=2,
8
  chat_format="chatml")
9
+ llm_for_translate = Llama(model_path="./qwen-1.8b-q5_k_m.gguf",
10
+ n_ctx=1024,
11
+ n_threads=2,
12
+ chat_format="chatml")
13
+
14
+ chi_eng_dict = []
15
+
16
 
17
+ def get_dict_result(original_text):
18
+ for d in chi_eng_dict:
19
+ if d[0] == original_text:
20
+ return d[1]
21
+ elif d[1] == original_text:
22
+ return d[0]
23
+ return None
24
 
25
+ def stream_translate_into(message, language='English'):
26
+ return llm.create_chat_completion(
27
+ messages=[{"role": "system", "content": f"Translate words into {language}. Regardless the meanning!"},
28
+ {"role": "user", "content": f"'{message}'"}],
29
+ stream=True,
30
+ stop=['\n\n']
31
+ )
32
+
33
+ def chat_completion(message, history, system_prompt):
34
  messages_prompts = [{"role": "system", "content": system_prompt}]
35
  for human, assistant in history:
36
  messages_prompts.append({"role": "user", "content": human})
37
  messages_prompts.append({"role": "assistant", "content": assistant})
38
+ messages_prompts.append({"role": "user", "content": message})
39
  response = llm.create_chat_completion(
40
  messages=messages_prompts,
41
  stream=False
42
  )
43
  print(json.dumps(response, ensure_ascii=False, indent=2))
44
+ return response['choices'][0]['message']['content']
45
 
46
 
47
+ def chat_stream_completion(message, history, system_prompt, translate_check):
48
  messages_prompts = [{"role": "system", "content": system_prompt}]
49
+ if translate_check:
50
+ if len(history) > 0:
51
+ for human, assistant in history:
52
+ human_repl = get_dict_result(human)
53
+ assistant_repl = get_dict_result(assistant)
54
+ if human_repl is None or assistant_repl is None:
55
+ print(chi_eng_dict)
56
+ raise gr.Error("历史信息缺少翻译字典,请勿中途修改翻译功能!")
57
+ messages_prompts.append({"role": "user", "content": human_repl})
58
+ messages_prompts.append({"role": "assistant", "content": assistant_repl})
59
+ message_repl = ""
60
+ for chunk in stream_translate_into(message, language='English'):
61
+ if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
62
+ message_repl = message_repl + \
63
+ chunk['choices'][0]["delta"]["content"]
64
+ chi_eng_dict.append((message, message_repl))
65
+ messages_prompts.append({"role": "user", "content": message_repl})
66
+ print(messages_prompts)
67
+ response = llm.create_chat_completion(
68
+ messages=messages_prompts,
69
+ stream=False,
70
+ stop=['\n\n']
71
+ )
72
+ print(json.dumps(response, ensure_ascii=False, indent=2))
73
+ result = response['choices'][0]['message']['content']
74
+ result_repl = ""
75
+ for chunk in stream_translate_into(result, language='Chinese'):
76
+ if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
77
+ result_repl = result_repl + \
78
+ chunk['choices'][0]["delta"]["content"]
79
+ yield result_repl
80
+ chi_eng_dict.append((result, result_repl))
81
+ else:
82
+ for human, assistant in history:
83
+ messages_prompts.append({"role": "user", "content": human})
84
+ messages_prompts.append({"role": "assistant", "content": assistant})
85
+ messages_prompts.append({"role": "user", "content": message})
86
+ response = llm.create_chat_completion(
87
+ messages=messages_prompts,
88
+ stream=True
89
+ )
90
+ message_repl = ""
91
+ for chunk in response:
92
+ if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
93
+ message_repl = message_repl + \
94
+ chunk['choices'][0]["delta"]["content"]
95
+ yield message_repl
96
+
97
+
98
+ gr.ChatInterface(
99
+ chat_stream_completion,
100
+ additional_inputs=[gr.Textbox(
101
+ "You are helpful AI.", label="System Prompt"), gr.Checkbox(label="Translate?")]
102
+ ).queue().launch(server_name="0.0.0.0")