Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,324 +1,19 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
-
import time
|
5 |
|
6 |
-
import gradio as gr
|
7 |
-
from transformers import AutoTokenizer
|
8 |
-
import psycopg2
|
9 |
|
|
|
10 |
|
11 |
-
import socket
|
12 |
-
hostname=socket.gethostname()
|
13 |
-
IPAddr=socket.gethostbyname(hostname)
|
14 |
-
print("Your Computer Name is:" + hostname)
|
15 |
-
print("Your Computer IP Address is:" + IPAddr)
|
16 |
|
17 |
|
18 |
DESCRIPTION = """
|
19 |
# Demo: Breeze-7B-Instruct-v0.1
|
20 |
|
21 |
-
Breeze-7B
|
22 |
-
[Breeze-7B-Instruct-v0_1](https://huggingface.co/MediaTek-Research/Breeze-7B-Instruct-v0_1) demonstrates impressive performance in benchmarks for Traditional Chinese and English, when compared to similar sized open-source contemporaries such as Taiwan-LLM-7B/13B-chat, QWen-7B-Chat, and Yi-6B-Chat.
|
23 |
-
|
24 |
-
*A project by the members (in alphabetical order): Chan-Jan Hsu 許湛然, Chang-Le Liu 劉昶樂, Feng-Ting Liao 廖峰挺, Po-Chun Hsu 許博竣, Yi-Chang Chen 陳宜昌, and the supervisor Da-Shan Shiu 許大山.*
|
25 |
-
|
26 |
-
**免責聲明: Breeze-7B-Instruct 和 Breeze-7B-Instruct-64k 並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。**
|
27 |
-
"""
|
28 |
-
|
29 |
-
LICENSE = """
|
30 |
-
|
31 |
"""
|
32 |
|
33 |
-
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."
|
34 |
-
|
35 |
-
API_URL = os.environ.get("API_URL")
|
36 |
-
TOKEN = os.environ.get("TOKEN")
|
37 |
-
|
38 |
-
HEADERS = {
|
39 |
-
"Authorization": f"Bearer {TOKEN}",
|
40 |
-
"Content-Type": "application/json",
|
41 |
-
"accept": "application/json"
|
42 |
-
}
|
43 |
-
|
44 |
-
|
45 |
-
MAX_SEC = 30
|
46 |
-
MAX_INPUT_LENGTH = 5000
|
47 |
-
|
48 |
-
tokenizer = AutoTokenizer.from_pretrained("MediaTek-Research/Breeze-7B-Instruct-v0_1")
|
49 |
-
|
50 |
-
def insert_to_db(prompt, response, temperature, top_p):
|
51 |
-
try:
|
52 |
-
#Establishing the connection
|
53 |
-
conn = psycopg2.connect(
|
54 |
-
database=os.environ.get("DB"), user=os.environ.get("USER"), password=os.environ.get("DB_PASS"), host=os.environ.get("DB_HOST"), port= '5432'
|
55 |
-
)
|
56 |
-
#Setting auto commit false
|
57 |
-
conn.autocommit = True
|
58 |
-
|
59 |
-
#Creating a cursor object using the cursor() method
|
60 |
-
cursor = conn.cursor()
|
61 |
-
|
62 |
-
# Preparing SQL queries to INSERT a record into the database.
|
63 |
-
cursor.execute(f"INSERT INTO breezedata(prompt, response, temperature, top_p) VALUES ('{prompt}', '{response}', {temperature}, {top_p})")
|
64 |
-
|
65 |
-
# Commit your changes in the database
|
66 |
-
conn.commit()
|
67 |
-
|
68 |
-
# Closing the connection
|
69 |
-
conn.close()
|
70 |
-
except:
|
71 |
-
pass
|
72 |
-
|
73 |
-
|
74 |
-
def refusal_condition(query):
|
75 |
-
# 不要再問這些問題啦!
|
76 |
-
|
77 |
-
query_remove_space = query.replace(' ', '').lower()
|
78 |
-
is_including_tw = False
|
79 |
-
for x in ['台灣', '台湾', 'taiwan', 'tw', '中華民國', '中华民国']:
|
80 |
-
if x in query_remove_space:
|
81 |
-
is_including_tw = True
|
82 |
-
is_including_cn = False
|
83 |
-
for x in ['中國', '中国', 'cn', 'china', '大陸', '內地', '大陆', '内地', '中華人民共和國', '中华人民共和国']:
|
84 |
-
if x in query_remove_space:
|
85 |
-
is_including_cn = True
|
86 |
-
if is_including_tw and is_including_cn:
|
87 |
-
return True
|
88 |
-
|
89 |
-
for x in ['一個中國', '兩岸', '一中原則', '一中政策', '一个中国', '两岸', '一中原则']:
|
90 |
-
if x in query_remove_space:
|
91 |
-
return True
|
92 |
-
|
93 |
-
return False
|
94 |
|
95 |
with gr.Blocks() as demo:
|
96 |
gr.Markdown(DESCRIPTION)
|
97 |
|
98 |
-
system_prompt = gr.Textbox(label='System prompt',
|
99 |
-
value=DEFAULT_SYSTEM_PROMPT,
|
100 |
-
lines=1)
|
101 |
|
102 |
-
with gr.Accordion(label='Advanced options', open=False):
|
103 |
-
|
104 |
-
max_new_tokens = gr.Slider(
|
105 |
-
label='Max new tokens',
|
106 |
-
minimum=32,
|
107 |
-
maximum=1024,
|
108 |
-
step=1,
|
109 |
-
value=512,
|
110 |
-
)
|
111 |
-
temperature = gr.Slider(
|
112 |
-
label='Temperature',
|
113 |
-
minimum=0.01,
|
114 |
-
maximum=0.5,
|
115 |
-
step=0.01,
|
116 |
-
value=0.01,
|
117 |
-
)
|
118 |
-
top_p = gr.Slider(
|
119 |
-
label='Top-p (nucleus sampling)',
|
120 |
-
minimum=0.01,
|
121 |
-
maximum=0.99,
|
122 |
-
step=0.01,
|
123 |
-
value=0.01,
|
124 |
-
)
|
125 |
-
|
126 |
-
chatbot = gr.Chatbot()
|
127 |
-
with gr.Row():
|
128 |
-
msg = gr.Textbox(
|
129 |
-
container=False,
|
130 |
-
show_label=False,
|
131 |
-
placeholder='Type a message...',
|
132 |
-
scale=10,
|
133 |
-
lines=6
|
134 |
-
)
|
135 |
-
submit_button = gr.Button('Submit',
|
136 |
-
variant='primary',
|
137 |
-
scale=1,
|
138 |
-
min_width=0)
|
139 |
-
|
140 |
-
with gr.Row():
|
141 |
-
retry_button = gr.Button('🔄 Retry', variant='secondary')
|
142 |
-
undo_button = gr.Button('↩️ Undo', variant='secondary')
|
143 |
-
clear = gr.Button('🗑��� Clear', variant='secondary')
|
144 |
-
|
145 |
-
saved_input = gr.State()
|
146 |
-
|
147 |
-
|
148 |
-
def user(user_message, history):
|
149 |
-
return "", history + [[user_message, None]]
|
150 |
-
|
151 |
-
|
152 |
-
def connect_server(data):
|
153 |
-
for _ in range(3):
|
154 |
-
s = requests.Session()
|
155 |
-
r = s.post(API_URL, headers=HEADERS, json=data, stream=True, timeout=30)
|
156 |
-
time.sleep(1)
|
157 |
-
if r.status_code == 200:
|
158 |
-
return r
|
159 |
-
return None
|
160 |
-
|
161 |
-
|
162 |
-
def stream_response_from_server(r):
|
163 |
-
# start_time = time.time()
|
164 |
-
keep_streaming = True
|
165 |
-
for line in r.iter_lines():
|
166 |
-
# if time.time() - start_time > MAX_SEC:
|
167 |
-
# keep_streaming = False
|
168 |
-
# break
|
169 |
-
|
170 |
-
if line and keep_streaming:
|
171 |
-
if r.status_code != 200:
|
172 |
-
continue
|
173 |
-
json_response = json.loads(line)
|
174 |
-
|
175 |
-
if "fragment" not in json_response["result"]:
|
176 |
-
keep_streaming = False
|
177 |
-
break
|
178 |
-
|
179 |
-
delta = json_response["result"]["fragment"]["data"]["text"]
|
180 |
-
yield delta
|
181 |
-
|
182 |
-
# start_time = time.time()
|
183 |
-
|
184 |
-
|
185 |
-
def bot(history, max_new_tokens, temperature, top_p, system_prompt):
|
186 |
-
chat_data = []
|
187 |
-
system_prompt = system_prompt.strip()
|
188 |
-
if system_prompt:
|
189 |
-
chat_data.append({"role": "system", "content": system_prompt})
|
190 |
-
for user_msg, assistant_msg in history:
|
191 |
-
chat_data.append({"role": "user", "content": user_msg if user_msg is not None else ''})
|
192 |
-
chat_data.append({"role": "assistant", "content": assistant_msg if assistant_msg is not None else ''})
|
193 |
-
|
194 |
-
message = tokenizer.apply_chat_template(chat_data, tokenize=False)
|
195 |
-
message = message[3:] # remove SOT token
|
196 |
-
|
197 |
-
if len(message) > MAX_INPUT_LENGTH:
|
198 |
-
raise Exception()
|
199 |
-
|
200 |
-
response = '[ERROR]'
|
201 |
-
if refusal_condition(history[-1][0]):
|
202 |
-
history = [['[安全拒答啟動]', '[安全拒答啟動] 請清除再開啟對話']]
|
203 |
-
response = '[REFUSAL]'
|
204 |
-
yield history
|
205 |
-
else:
|
206 |
-
data = {
|
207 |
-
"model_type": "breeze-7b-instruct-v01",
|
208 |
-
"prompt": str(message),
|
209 |
-
"parameters": {
|
210 |
-
"temperature": float(temperature),
|
211 |
-
"top_p": float(top_p),
|
212 |
-
"max_new_tokens": int(max_new_tokens),
|
213 |
-
"repetition_penalty": 1.1
|
214 |
-
}
|
215 |
-
}
|
216 |
-
|
217 |
-
r = connect_server(data)
|
218 |
-
if r is not None:
|
219 |
-
for delta in stream_response_from_server(r):
|
220 |
-
if history[-1][1] is None:
|
221 |
-
history[-1][1] = ''
|
222 |
-
history[-1][1] += delta
|
223 |
-
yield history
|
224 |
-
|
225 |
-
if history[-1][1].endswith('</s>'):
|
226 |
-
history[-1][1] = history[-1][1][:-4]
|
227 |
-
yield history
|
228 |
-
|
229 |
-
response = history[-1][1]
|
230 |
-
|
231 |
-
if refusal_condition(history[-1][1]):
|
232 |
-
history[-1][1] = history[-1][1] + '\n\n**[免責聲明: Breeze-7B-Instruct 和 Breeze-7B-Instruct-64k 並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。]**'
|
233 |
-
yield history
|
234 |
-
else:
|
235 |
-
del history[-1]
|
236 |
-
yield history
|
237 |
-
|
238 |
-
print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(message), response=repr(history[-1][1])))
|
239 |
-
insert_to_db(message, response, float(temperature), float(top_p))
|
240 |
-
|
241 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
242 |
-
fn=bot,
|
243 |
-
inputs=[
|
244 |
-
chatbot,
|
245 |
-
max_new_tokens,
|
246 |
-
temperature,
|
247 |
-
top_p,
|
248 |
-
system_prompt,
|
249 |
-
],
|
250 |
-
outputs=chatbot
|
251 |
-
)
|
252 |
-
submit_button.click(
|
253 |
-
user, [msg, chatbot], [msg, chatbot], queue=False
|
254 |
-
).then(
|
255 |
-
fn=bot,
|
256 |
-
inputs=[
|
257 |
-
chatbot,
|
258 |
-
max_new_tokens,
|
259 |
-
temperature,
|
260 |
-
top_p,
|
261 |
-
system_prompt,
|
262 |
-
],
|
263 |
-
outputs=chatbot
|
264 |
-
)
|
265 |
-
|
266 |
-
|
267 |
-
def delete_prev_fn(
|
268 |
-
history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
|
269 |
-
try:
|
270 |
-
message, _ = history.pop()
|
271 |
-
except IndexError:
|
272 |
-
message = ''
|
273 |
-
return history, message or ''
|
274 |
-
|
275 |
-
|
276 |
-
def display_input(message: str,
|
277 |
-
history: list[tuple[str, str]]) -> list[tuple[str, str]]:
|
278 |
-
history.append((message, ''))
|
279 |
-
return history
|
280 |
-
|
281 |
-
retry_button.click(
|
282 |
-
fn=delete_prev_fn,
|
283 |
-
inputs=chatbot,
|
284 |
-
outputs=[chatbot, saved_input],
|
285 |
-
api_name=False,
|
286 |
-
queue=False,
|
287 |
-
).then(
|
288 |
-
fn=display_input,
|
289 |
-
inputs=[saved_input, chatbot],
|
290 |
-
outputs=chatbot,
|
291 |
-
api_name=False,
|
292 |
-
queue=False,
|
293 |
-
).then(
|
294 |
-
fn=bot,
|
295 |
-
inputs=[
|
296 |
-
chatbot,
|
297 |
-
max_new_tokens,
|
298 |
-
temperature,
|
299 |
-
top_p,
|
300 |
-
system_prompt,
|
301 |
-
],
|
302 |
-
outputs=chatbot,
|
303 |
-
)
|
304 |
-
|
305 |
-
undo_button.click(
|
306 |
-
fn=delete_prev_fn,
|
307 |
-
inputs=chatbot,
|
308 |
-
outputs=[chatbot, saved_input],
|
309 |
-
api_name=False,
|
310 |
-
queue=False,
|
311 |
-
).then(
|
312 |
-
fn=lambda x: x,
|
313 |
-
inputs=[saved_input],
|
314 |
-
outputs=msg,
|
315 |
-
api_name=False,
|
316 |
-
queue=False,
|
317 |
-
)
|
318 |
-
|
319 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
320 |
-
|
321 |
-
gr.Markdown(LICENSE)
|
322 |
|
323 |
-
demo.queue(concurrency_count=2, max_size=128)
|
324 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
1 |
|
|
|
|
|
|
|
2 |
|
3 |
+
import gradio as gr
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
DESCRIPTION = """
|
8 |
# Demo: Breeze-7B-Instruct-v0.1
|
9 |
|
10 |
+
Breeze-7B-Instruct already updated to v1.0. Please check the new demo [here](https://huggingface.co/spaces/MediaTek-Research/Demo-MR-Breeze-7B).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
"""
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
with gr.Blocks() as demo:
|
15 |
gr.Markdown(DESCRIPTION)
|
16 |
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
19 |
demo.launch()
|