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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
import socket
|
| 7 |
+
hostname=socket.gethostname()
|
| 8 |
+
IPAddr=socket.gethostbyname(hostname)
|
| 9 |
+
print("Your Computer Name is:" + hostname)
|
| 10 |
+
print("Your Computer IP Address is:" + IPAddr)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
DESCRIPTION = """
|
| 14 |
+
# Cloned from MediaTek Research Breeze-7B
|
| 15 |
+
MediaTek Research Breeze-7B (hereinafter referred to as Breeze-7B) is a language model family that builds on top of [Mistral-7B](https://huggingface.co/mistralai/Mistral-7B-v0.1), specifically intended for Traditional Chinese use.
|
| 16 |
+
[Breeze-7B-Base](https://huggingface.co/MediaTek-Research/Breeze-7B-Base-v1_0) is the base model for the Breeze-7B series.
|
| 17 |
+
It is suitable for use if you have substantial fine-tuning data to tune it for your specific use case.
|
| 18 |
+
[Breeze-7B-Instruct](https://huggingface.co/MediaTek-Research/Breeze-7B-Instruct-v1_0) derives from the base model Breeze-7B-Base, making the resulting model amenable to be used as-is for commonly seen tasks.
|
| 19 |
+
This App is cloned from [Demo-MR-Breeze-7B](https://huggingface.co/spaces/MediaTek-Research/Demo-MR-Breeze-7B)
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
LICENSE = """
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
DEFAULT_SYSTEM_PROMPT = "You are a helpful AI assistant built by MediaTek Research. The user you are helping speaks Traditional Chinese and comes from Taiwan."
|
| 26 |
+
|
| 27 |
+
API_URL = os.environ.get("API_URL")
|
| 28 |
+
TOKEN = os.environ.get("TOKEN")
|
| 29 |
+
TOKENIZER_REPO = "MediaTek-Research/Breeze-7B-Instruct-v1_0"
|
| 30 |
+
MODEL_NAME = os.environ.get("MODEL_NAME")
|
| 31 |
+
MAX_SEC = 30
|
| 32 |
+
MAX_INPUT_LENGTH = 5000
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def chat_with_openai(model_name, system_message, user_message, temperature=0.5, max_tokens=1024, top_p=0.5):
|
| 36 |
+
client = OpenAI(
|
| 37 |
+
base_url=API_URL,
|
| 38 |
+
api_key=TOKEN
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
chat_completion = client.chat.completions.create(
|
| 42 |
+
model=model_name,
|
| 43 |
+
messages=[
|
| 44 |
+
{
|
| 45 |
+
"role": "system",
|
| 46 |
+
"content": system_message
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": user_message
|
| 51 |
+
}
|
| 52 |
+
],
|
| 53 |
+
temperature=temperature,
|
| 54 |
+
max_tokens=max_tokens,
|
| 55 |
+
top_p=top_p,
|
| 56 |
+
stream=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
for message in chat_completion:
|
| 60 |
+
yield message.choices[0].delta.content
|
| 61 |
+
|
| 62 |
+
def refusal_condition(query):
|
| 63 |
+
# 不要再問這些問題啦!
|
| 64 |
+
|
| 65 |
+
query_remove_space = query.replace(' ', '').lower()
|
| 66 |
+
is_including_tw = False
|
| 67 |
+
for x in ['台灣', '台湾', 'taiwan', 'tw', '中華民國', '中华民国']:
|
| 68 |
+
if x in query_remove_space:
|
| 69 |
+
is_including_tw = True
|
| 70 |
+
is_including_cn = False
|
| 71 |
+
for x in ['中國', '中国', 'cn', 'china', '大陸', '內地', '大陆', '内地', '中華人民共和國', '中华人民共和国']:
|
| 72 |
+
if x in query_remove_space:
|
| 73 |
+
is_including_cn = True
|
| 74 |
+
if is_including_tw and is_including_cn:
|
| 75 |
+
return True
|
| 76 |
+
|
| 77 |
+
for x in ['一個中國', '兩岸', '一中原則', '一中政策', '一个中国', '两岸', '一中原则']:
|
| 78 |
+
if x in query_remove_space:
|
| 79 |
+
return True
|
| 80 |
+
|
| 81 |
+
return False
|
| 82 |
+
|
| 83 |
+
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown(DESCRIPTION)
|
| 85 |
+
|
| 86 |
+
system_prompt = gr.Textbox(label='System prompt',
|
| 87 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
| 88 |
+
lines=1)
|
| 89 |
+
|
| 90 |
+
with gr.Accordion(label='Advanced options', open=False):
|
| 91 |
+
|
| 92 |
+
max_new_tokens = gr.Slider(
|
| 93 |
+
label='Max new tokens',
|
| 94 |
+
minimum=32,
|
| 95 |
+
maximum=2048,
|
| 96 |
+
step=1,
|
| 97 |
+
value=1024,
|
| 98 |
+
)
|
| 99 |
+
temperature = gr.Slider(
|
| 100 |
+
label='Temperature',
|
| 101 |
+
minimum=0.01,
|
| 102 |
+
maximum=0.5,
|
| 103 |
+
step=0.01,
|
| 104 |
+
value=0.01,
|
| 105 |
+
)
|
| 106 |
+
top_p = gr.Slider(
|
| 107 |
+
label='Top-p (nucleus sampling)',
|
| 108 |
+
minimum=0.01,
|
| 109 |
+
maximum=0.99,
|
| 110 |
+
step=0.01,
|
| 111 |
+
value=0.01,
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
chatbot = gr.Chatbot(show_copy_button=True, show_share_button=True, )
|
| 115 |
+
with gr.Row():
|
| 116 |
+
msg = gr.Textbox(
|
| 117 |
+
container=False,
|
| 118 |
+
show_label=False,
|
| 119 |
+
placeholder='Type a message...',
|
| 120 |
+
scale=10,
|
| 121 |
+
lines=6
|
| 122 |
+
)
|
| 123 |
+
submit_button = gr.Button('Submit',
|
| 124 |
+
variant='primary',
|
| 125 |
+
scale=1,
|
| 126 |
+
min_width=0)
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
retry_button = gr.Button('🔄 Retry', variant='secondary')
|
| 130 |
+
undo_button = gr.Button('↩️ Undo', variant='secondary')
|
| 131 |
+
clear = gr.Button('🗑️ Clear', variant='secondary')
|
| 132 |
+
|
| 133 |
+
saved_input = gr.State()
|
| 134 |
+
|
| 135 |
+
def user(user_message, history):
|
| 136 |
+
return "", history + [[user_message, None]]
|
| 137 |
+
|
| 138 |
+
def bot(history, max_new_tokens, temperature, top_p, system_prompt):
|
| 139 |
+
chat_data = []
|
| 140 |
+
system_prompt = system_prompt.strip()
|
| 141 |
+
if system_prompt:
|
| 142 |
+
chat_data.append({"role": "system", "content": system_prompt})
|
| 143 |
+
for user_msg, assistant_msg in history:
|
| 144 |
+
chat_data.append({"role": "user", "content": user_msg if user_msg is not None else ''})
|
| 145 |
+
chat_data.append({"role": "assistant", "content": assistant_msg if assistant_msg is not None else ''})
|
| 146 |
+
|
| 147 |
+
response = '[ERROR]'
|
| 148 |
+
if refusal_condition(history[-1][0]):
|
| 149 |
+
history = [['[安全拒答啟動]', '[安全拒答啟動] 請清除再開啟對話']]
|
| 150 |
+
response = '[REFUSAL]'
|
| 151 |
+
yield history
|
| 152 |
+
else:
|
| 153 |
+
r = chat_with_openai(
|
| 154 |
+
MODEL_NAME,
|
| 155 |
+
system_prompt,
|
| 156 |
+
history[-1][0],
|
| 157 |
+
temperature,
|
| 158 |
+
max_new_tokens,
|
| 159 |
+
top_p)
|
| 160 |
+
if r is not None:
|
| 161 |
+
for delta in r:
|
| 162 |
+
if history[-1][1] is None:
|
| 163 |
+
history[-1][1] = ''
|
| 164 |
+
if delta is None:
|
| 165 |
+
delta = ''
|
| 166 |
+
history[-1][1] += delta
|
| 167 |
+
yield history
|
| 168 |
+
|
| 169 |
+
if history[-1][1].endswith('</s>'):
|
| 170 |
+
history[-1][1] = history[-1][1][:-4]
|
| 171 |
+
yield history
|
| 172 |
+
|
| 173 |
+
response = history[-1][1]
|
| 174 |
+
|
| 175 |
+
if refusal_condition(history[-1][1]):
|
| 176 |
+
history[-1][1] = history[-1][1] + '\n\n**[免責聲明: 此模型並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。]**'
|
| 177 |
+
yield history
|
| 178 |
+
else:
|
| 179 |
+
del history[-1]
|
| 180 |
+
yield history
|
| 181 |
+
|
| 182 |
+
print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(history[-1][0]), response=repr(history[-1][1])))
|
| 183 |
+
|
| 184 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 185 |
+
fn=bot,
|
| 186 |
+
inputs=[
|
| 187 |
+
chatbot,
|
| 188 |
+
max_new_tokens,
|
| 189 |
+
temperature,
|
| 190 |
+
top_p,
|
| 191 |
+
system_prompt,
|
| 192 |
+
],
|
| 193 |
+
outputs=chatbot
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
submit_button.click(
|
| 197 |
+
user, [msg, chatbot], [msg, chatbot], queue=False
|
| 198 |
+
).then(
|
| 199 |
+
fn=bot,
|
| 200 |
+
inputs=[
|
| 201 |
+
chatbot,
|
| 202 |
+
max_new_tokens,
|
| 203 |
+
temperature,
|
| 204 |
+
top_p,
|
| 205 |
+
system_prompt,
|
| 206 |
+
],
|
| 207 |
+
outputs=chatbot
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def delete_prev_fn(
|
| 212 |
+
history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
|
| 213 |
+
try:
|
| 214 |
+
message, _ = history.pop()
|
| 215 |
+
except IndexError:
|
| 216 |
+
message = ''
|
| 217 |
+
return history, message or ''
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def display_input(message: str,
|
| 221 |
+
history: list[tuple[str, str]]) -> list[tuple[str, str]]:
|
| 222 |
+
history.append((message, ''))
|
| 223 |
+
return history
|
| 224 |
+
|
| 225 |
+
retry_button.click(
|
| 226 |
+
fn=delete_prev_fn,
|
| 227 |
+
inputs=chatbot,
|
| 228 |
+
outputs=[chatbot, saved_input],
|
| 229 |
+
api_name=False,
|
| 230 |
+
queue=False,
|
| 231 |
+
).then(
|
| 232 |
+
fn=display_input,
|
| 233 |
+
inputs=[saved_input, chatbot],
|
| 234 |
+
outputs=chatbot,
|
| 235 |
+
api_name=False,
|
| 236 |
+
queue=False,
|
| 237 |
+
).then(
|
| 238 |
+
fn=bot,
|
| 239 |
+
inputs=[
|
| 240 |
+
chatbot,
|
| 241 |
+
max_new_tokens,
|
| 242 |
+
temperature,
|
| 243 |
+
top_p,
|
| 244 |
+
system_prompt,
|
| 245 |
+
],
|
| 246 |
+
outputs=chatbot,
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
undo_button.click(
|
| 250 |
+
fn=delete_prev_fn,
|
| 251 |
+
inputs=chatbot,
|
| 252 |
+
outputs=[chatbot, saved_input],
|
| 253 |
+
api_name=False,
|
| 254 |
+
queue=False,
|
| 255 |
+
).then(
|
| 256 |
+
fn=lambda x: x,
|
| 257 |
+
inputs=[saved_input],
|
| 258 |
+
outputs=msg,
|
| 259 |
+
api_name=False,
|
| 260 |
+
queue=False,
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 264 |
+
|
| 265 |
+
gr.Markdown(LICENSE)
|
| 266 |
+
|
| 267 |
+
demo.queue(default_concurrency_limit=10)
|
| 268 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai
|