simonzhang5429 commited on
Commit
04056d2
·
verified ·
1 Parent(s): 171c7da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -299
app.py CHANGED
@@ -1,304 +1,18 @@
1
- import os
2
- import requests
3
- import json
4
- import time
5
-
6
  import gradio as gr
7
- from transformers import AutoTokenizer
8
-
9
-
10
- import socket
11
- hostname=socket.gethostname()
12
- IPAddr=socket.gethostbyname(hostname)
13
- print("Your Computer Name is:" + hostname)
14
- print("Your Computer IP Address is:" + IPAddr)
15
-
16
-
17
- DESCRIPTION = """
18
- # MediaTek Research Breeze-7B
19
-
20
- 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.
21
-
22
- [Breeze-7B-Base](https://huggingface.co/MediaTek-Research/Breeze-7B-Base-v1_0) is the base model for the Breeze-7B series.
23
- It is suitable for use if you have substantial fine-tuning data to tune it for your specific use case.
24
-
25
- [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.
26
-
27
-
28
- The current release version of Breeze-7B is v1.0.
29
-
30
- *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 許大山.*
31
-
32
- **免責聲明: MediaTek Research Breeze-7B 並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。**
33
- """
34
-
35
- LICENSE = """
36
- """
37
-
38
- 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."
39
-
40
- API_URL = os.environ.get("API_URL")
41
- TOKEN = os.environ.get("TOKEN")
42
- TOKENIZER_REPO = "MediaTek-Research/Breeze-7B-Instruct-v1_0"
43
- API_MODEL_TYPE = "breeze-7b-instruct-v10"
44
-
45
- HEADERS = {
46
- "Authorization": f"Bearer {TOKEN}",
47
- "Content-Type": "application/json",
48
- "accept": "application/json"
49
- }
50
-
51
- MAX_SEC = 30
52
- MAX_INPUT_LENGTH = 5000
53
-
54
- tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_REPO, use_auth_token=os.environ.get("HF_TOKEN"))
55
-
56
-
57
- def refusal_condition(query):
58
- # 不要再問這些問題啦!
59
-
60
- query_remove_space = query.replace(' ', '').lower()
61
- is_including_tw = False
62
- for x in ['台灣', '台湾', 'taiwan', 'tw', '中華民國', '中华民国']:
63
- if x in query_remove_space:
64
- is_including_tw = True
65
- is_including_cn = False
66
- for x in ['中國', '中国', 'cn', 'china', '大陸', '內地', '大陆', '内地', '中華人民共和國', '中华人民共和国']:
67
- if x in query_remove_space:
68
- is_including_cn = True
69
- if is_including_tw and is_including_cn:
70
- return True
71
-
72
- for x in ['一個中國', '兩岸', '一中原則', '一中政策', '一个中国', '两岸', '一中原则']:
73
- if x in query_remove_space:
74
- return True
75
-
76
- return False
77
-
78
- with gr.Blocks() as demo:
79
- gr.Markdown(DESCRIPTION)
80
-
81
- system_prompt = gr.Textbox(label='System prompt',
82
- value=DEFAULT_SYSTEM_PROMPT,
83
- lines=1)
84
-
85
- with gr.Accordion(label='Advanced options', open=False):
86
-
87
- max_new_tokens = gr.Slider(
88
- label='Max new tokens',
89
- minimum=32,
90
- maximum=2048,
91
- step=1,
92
- value=1024,
93
- )
94
- temperature = gr.Slider(
95
- label='Temperature',
96
- minimum=0.01,
97
- maximum=0.5,
98
- step=0.01,
99
- value=0.01,
100
- )
101
- top_p = gr.Slider(
102
- label='Top-p (nucleus sampling)',
103
- minimum=0.01,
104
- maximum=0.99,
105
- step=0.01,
106
- value=0.01,
107
- )
108
-
109
- chatbot = gr.Chatbot(show_copy_button=True, show_share_button=True, )
110
- with gr.Row():
111
- msg = gr.Textbox(
112
- container=False,
113
- show_label=False,
114
- placeholder='Type a message...',
115
- scale=10,
116
- lines=6
117
- )
118
- submit_button = gr.Button('Submit',
119
- variant='primary',
120
- scale=1,
121
- min_width=0)
122
-
123
- with gr.Row():
124
- retry_button = gr.Button('🔄 Retry', variant='secondary')
125
- undo_button = gr.Button('↩️ Undo', variant='secondary')
126
- clear = gr.Button('🗑️ Clear', variant='secondary')
127
-
128
- saved_input = gr.State()
129
-
130
- def user(user_message, history):
131
- return "", history + [[user_message, None]]
132
-
133
-
134
- def connect_server(data):
135
- for _ in range(3):
136
- s = requests.Session()
137
- r = s.post(API_URL, headers=HEADERS, json=data, stream=True, timeout=30)
138
- time.sleep(1)
139
- if r.status_code == 200:
140
- return r
141
- return None
142
-
143
-
144
- def stream_response_from_server(r):
145
- # start_time = time.time()
146
- keep_streaming = True
147
- for line in r.iter_lines():
148
- # if time.time() - start_time > MAX_SEC:
149
- # keep_streaming = False
150
- # break
151
-
152
- if line and keep_streaming:
153
- if r.status_code != 200:
154
- continue
155
- json_response = json.loads(line)
156
-
157
- if "fragment" not in json_response["result"]:
158
- keep_streaming = False
159
- break
160
-
161
- delta = json_response["result"]["fragment"]["data"]["text"]
162
- yield delta
163
-
164
- # start_time = time.time()
165
-
166
-
167
- def bot(history, max_new_tokens, temperature, top_p, system_prompt):
168
- chat_data = []
169
- system_prompt = system_prompt.strip()
170
- if system_prompt:
171
- chat_data.append({"role": "system", "content": system_prompt})
172
- for user_msg, assistant_msg in history:
173
- chat_data.append({"role": "user", "content": user_msg if user_msg is not None else ''})
174
- chat_data.append({"role": "assistant", "content": assistant_msg if assistant_msg is not None else ''})
175
-
176
- message = tokenizer.apply_chat_template(chat_data, tokenize=False)
177
- message = message[3:] # remove SOT token
178
-
179
- if len(message) > MAX_INPUT_LENGTH:
180
- raise Exception()
181
-
182
- response = '[ERROR]'
183
- if refusal_condition(history[-1][0]):
184
- history = [['[安全拒答啟動]', '[安全拒答啟動] 請清除再開啟對話']]
185
- response = '[REFUSAL]'
186
- yield history
187
- else:
188
- data = {
189
- "model_type": API_MODEL_TYPE,
190
- "prompt": str(message),
191
- "parameters": {
192
- "temperature": float(temperature),
193
- "top_p": float(top_p),
194
- "max_new_tokens": int(max_new_tokens),
195
- "repetition_penalty": 1.1
196
- }
197
- }
198
-
199
- r = connect_server(data)
200
- if r is not None:
201
- for delta in stream_response_from_server(r):
202
- if history[-1][1] is None:
203
- history[-1][1] = ''
204
- history[-1][1] += delta
205
- yield history
206
-
207
- if history[-1][1].endswith('</s>'):
208
- history[-1][1] = history[-1][1][:-4]
209
- yield history
210
-
211
- response = history[-1][1]
212
-
213
- if refusal_condition(history[-1][1]):
214
- history[-1][1] = history[-1][1] + '\n\n**[免責聲明: 此模型並未針對問答進行安全保護,因此語言模型的任何回應不代表 MediaTek Research 立場。]**'
215
- yield history
216
- else:
217
- del history[-1]
218
- yield history
219
- print('== Record ==\nQuery: {query}\nResponse: {response}'.format(query=repr(message), response=repr(history[-1][1])))
220
-
221
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
222
- fn=bot,
223
- inputs=[
224
- chatbot,
225
- max_new_tokens,
226
- temperature,
227
- top_p,
228
- system_prompt,
229
- ],
230
- outputs=chatbot
231
- )
232
- submit_button.click(
233
- user, [msg, chatbot], [msg, chatbot], queue=False
234
- ).then(
235
- fn=bot,
236
- inputs=[
237
- chatbot,
238
- max_new_tokens,
239
- temperature,
240
- top_p,
241
- system_prompt,
242
- ],
243
- outputs=chatbot
244
- )
245
-
246
-
247
- def delete_prev_fn(
248
- history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
249
- try:
250
- message, _ = history.pop()
251
- except IndexError:
252
- message = ''
253
- return history, message or ''
254
-
255
-
256
- def display_input(message: str,
257
- history: list[tuple[str, str]]) -> list[tuple[str, str]]:
258
- history.append((message, ''))
259
- return history
260
-
261
- retry_button.click(
262
- fn=delete_prev_fn,
263
- inputs=chatbot,
264
- outputs=[chatbot, saved_input],
265
- api_name=False,
266
- queue=False,
267
- ).then(
268
- fn=display_input,
269
- inputs=[saved_input, chatbot],
270
- outputs=chatbot,
271
- api_name=False,
272
- queue=False,
273
- ).then(
274
- fn=bot,
275
- inputs=[
276
- chatbot,
277
- max_new_tokens,
278
- temperature,
279
- top_p,
280
- system_prompt,
281
- ],
282
- outputs=chatbot,
283
- )
284
 
285
- undo_button.click(
286
- fn=delete_prev_fn,
287
- inputs=chatbot,
288
- outputs=[chatbot, saved_input],
289
- api_name=False,
290
- queue=False,
291
- ).then(
292
- fn=lambda x: x,
293
- inputs=[saved_input],
294
- outputs=msg,
295
- api_name=False,
296
- queue=False,
297
- )
298
 
299
- clear.click(lambda: None, None, chatbot, queue=False)
 
 
300
 
301
- gr.Markdown(LICENSE)
 
 
 
 
 
302
 
303
- demo.queue(concurrency_count=4, max_size=128)
304
- demo.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def predict(input_img):
7
+ predictions = pipeline(input_img)
8
+ return input_img, {p["label"]: p["score"] for p in predictions}
9
 
10
+ gradio_app = gr.Interface(
11
+ predict,
12
+ inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
13
+ outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
14
+ title="Hot Dog? Or Not?",
15
+ )
16
 
17
+ if __name__ == "__main__":
18
+ gradio_app.launch()