ubermenchh commited on
Commit
bdc3d58
1 Parent(s): da682e8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +243 -0
app.py ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Iterator
3
+ import gradio as gr
4
+ from text_generation import Client
5
+
6
+ model_id = 'mistralai/Mistral-7B-Instruct-v0.1'
7
+
8
+ API_URL = "https://api-inference.huggingface.co/models/" + model_id
9
+ HF_TOKEN = os.environ.get('HF_READ_TOKEN', False)
10
+
11
+ client = Client(
12
+ API_URL,
13
+ headers={'Authorization': f"Bearer {HF_TOKEN}"}
14
+ )
15
+ EOS_STRING = "</s>"
16
+ EOT_STRING = "<EOT>"
17
+
18
+ def get_prompt(message, chat_history, system_prompt):
19
+ texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
20
+
21
+ do_strip = False
22
+ for user_input, response in chat_history:
23
+ user_input = user_input.strip() if do_strip else user_input
24
+ do_strip = True
25
+ texts.append(f"{user_input} [/INST] {response.strip()} </s><s>[INST] ")
26
+ message = message.strip() if do_strip else message
27
+ texts.append(f"{message} [/INST]")
28
+ return ''.join(texts)
29
+
30
+ def run(message, chat_history, system_prompt, max_new_tokens=1024, temperature=0.1, top_p=0.9, top_k=50):
31
+ prompt = get_prompt(message, chat_history, system_prompt)
32
+
33
+ generate_kwargs = dict(
34
+ max_new_tokens=max_new_tokens,
35
+ do_sample=True,
36
+ top_p=top_p,
37
+ top_k=top_k,
38
+ temperature=temperature
39
+ )
40
+ stream = client.generate_stream(prompt, **generate_kwargs)
41
+ output = ''
42
+ for response in stream:
43
+ if any([end_token in response.token.text for end_token in [EOS_STRING, EOR_STRING]]):
44
+ return output
45
+ else:
46
+ output += response.token.text
47
+ yield output
48
+ return output
49
+
50
+
51
+ DEFAULT_SYSTEM_PROMPT = """
52
+ You are Mistral. You are an AI assistant, you are moderately-polite and give only true information.
53
+ You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning.
54
+ If you think there might not be a correct answer, you say so. Since you are autoregressive,
55
+ each token you produce is another opportunity to use computation, therefore you always spend a few sentences explaining background context,
56
+ assumptions, and step-by-step thinking BEFORE you try to answer a question.
57
+ """
58
+
59
+ MAX_MAX_NEW_TOKENS = 4096
60
+ DEFAULT_MAX_NEW_TOKENS = 256
61
+ MAX_INPUT_TOKEN_LENGTH = 4000
62
+
63
+ DESCRIPTION = "# [Mistral-7B](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1)"
64
+
65
+ def clear_and_save_textbox(message): return '', message
66
+
67
+ def display_input(message, history=[]):
68
+ history.append((message, ''))
69
+ return history
70
+
71
+ def delete_prev_fn(history=[]):
72
+ try:
73
+ message, _ = history.pop()
74
+ except IndexError:
75
+ message = ''
76
+ return history, message or ''
77
+
78
+ def generate(message, history_with_input, system_prompt, max_new_tokens, temperature, top_p, top_k):
79
+ if max_new_tokens > MAX_MAX_NEW_TOKENS:
80
+ raise ValueError
81
+
82
+ history = history_with_input[:-1]
83
+ generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
84
+ try:
85
+ first_response = next(generator)
86
+ yield history + [(message, first_response)]
87
+ except StopIteration:
88
+ yield history + [(message, '')]
89
+ for response in generator:
90
+ yield history + [(message, response)]
91
+
92
+ def process_example(message):
93
+ generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 1, 0.95, 50)
94
+ for x in generator:
95
+ pass
96
+ return '', x
97
+
98
+ def check_input_token_length(message, chat_history, system_prompt):
99
+ input_token_length = len(message) + len(chat_history)
100
+ if input_token_length > MAX_INPUT_TOKEN_LENGTH:
101
+ raise gr.Error(f"The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.")
102
+
103
+ with gr.Blocks(theme='Taithrah/Minimal') as demo:
104
+ gr.Markdown(DESCRIPTION)
105
+
106
+ with gr.Group():
107
+ chatbot = gr.Chatbot(label='Playground')
108
+ with gr.Row():
109
+ textbox = gr.Textbox(
110
+ container=False,
111
+ show_label=False,
112
+ placeholder='Hi, Zephyr',
113
+ scale=10
114
+ )
115
+ submit_button = gr.Button('Submit', variant='primary', scale=1, min_width=0)
116
+
117
+ with gr.Row():
118
+ retry_button = gr.Button('Retry', variant='secondary')
119
+ undo_button = gr.Button('Undo', variant='secondary')
120
+ clear_button = gr.Button('Clear', variant='secondary')
121
+
122
+ saved_input = gr.State()
123
+
124
+ with gr.Accordion(label='Advanced options', open=False):
125
+ system_prompt = gr.Textbox(label='System prompt', value=DEFAULT_SYSTEM_PROMPT, lines=5, interactive=False)
126
+ max_new_tokens = gr.Slider(label='Max New Tokens', minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
127
+ temperature = gr.Slider(label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=0.1)
128
+ top_p = gr.Slider(label='Top-P (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.9)
129
+ top_k = gr.Slider(label='Top-K', minimum=1, maximum=1000, step=1, value=10)
130
+
131
+ textbox.submit(
132
+ fn=clear_and_save_textbox,
133
+ inputs=textbox,
134
+ outputs=[textbox, saved_input],
135
+ api_name=False,
136
+ queue=False,
137
+ ).then(
138
+ fn=display_input,
139
+ inputs=[saved_input, chatbot],
140
+ outputs=chatbot,
141
+ api_name=False,
142
+ queue=False,
143
+ ).then(
144
+ fn=check_input_token_length,
145
+ inputs=[saved_input, chatbot, system_prompt],
146
+ api_name=False,
147
+ queue=False,
148
+ ).success(
149
+ fn=generate,
150
+ inputs=[
151
+ saved_input,
152
+ chatbot,
153
+ system_prompt,
154
+ max_new_tokens,
155
+ temperature,
156
+ top_p,
157
+ top_k,
158
+ ],
159
+ outputs=chatbot,
160
+ api_name=False,
161
+ )
162
+
163
+ button_event_preprocess = submit_button.click(
164
+ fn=clear_and_save_textbox,
165
+ inputs=textbox,
166
+ outputs=[textbox, saved_input],
167
+ api_name=False,
168
+ queue=False,
169
+ ).then(
170
+ fn=display_input,
171
+ inputs=[saved_input, chatbot],
172
+ outputs=chatbot,
173
+ api_name=False,
174
+ queue=False,
175
+ ).then(
176
+ fn=check_input_token_length,
177
+ inputs=[saved_input, chatbot, system_prompt],
178
+ api_name=False,
179
+ queue=False,
180
+ ).success(
181
+ fn=generate,
182
+ inputs=[
183
+ saved_input,
184
+ chatbot,
185
+ system_prompt,
186
+ max_new_tokens,
187
+ temperature,
188
+ top_p,
189
+ top_k,
190
+ ],
191
+ outputs=chatbot,
192
+ api_name=False,
193
+ )
194
+
195
+ retry_button.click(
196
+ fn=delete_prev_fn,
197
+ inputs=chatbot,
198
+ outputs=[chatbot, saved_input],
199
+ api_name=False,
200
+ queue=False,
201
+ ).then(
202
+ fn=display_input,
203
+ inputs=[saved_input, chatbot],
204
+ outputs=chatbot,
205
+ api_name=False,
206
+ queue=False,
207
+ ).then(
208
+ fn=generate,
209
+ inputs=[
210
+ saved_input,
211
+ chatbot,
212
+ system_prompt,
213
+ max_new_tokens,
214
+ temperature,
215
+ top_p,
216
+ top_k,
217
+ ],
218
+ outputs=chatbot,
219
+ api_name=False,
220
+ )
221
+
222
+ undo_button.click(
223
+ fn=delete_prev_fn,
224
+ inputs=chatbot,
225
+ outputs=[chatbot, saved_input],
226
+ api_name=False,
227
+ queue=False,
228
+ ).then(
229
+ fn=lambda x: x,
230
+ inputs=[saved_input],
231
+ outputs=textbox,
232
+ api_name=False,
233
+ queue=False,
234
+ )
235
+
236
+ clear_button.click(
237
+ fn=lambda: ([], ''),
238
+ outputs=[chatbot, saved_input],
239
+ queue=False,
240
+ api_name=False,
241
+ )
242
+
243
+ demo.queue(max_size=32).launch(show_api=False)