Ashrafb commited on
Commit
d01ca09
·
1 Parent(s): aa4d0ca

Upload app (40).py

Browse files
Files changed (1) hide show
  1. app (40).py +270 -0
app (40).py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Iterator
3
+
4
+ import gradio as gr
5
+
6
+ from model import run
7
+
8
+ HF_PUBLIC = os.environ.get("HF_PUBLIC", False)
9
+
10
+ DEFAULT_SYSTEM_PROMPT = """\
11
+ You are a helpful, respectful and honest assistant with a deep knowledge of code and software design. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content.Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\
12
+ """
13
+ MAX_MAX_NEW_TOKENS = 10000
14
+ DEFAULT_MAX_NEW_TOKENS = 1024
15
+ MAX_INPUT_TOKEN_LENGTH = 10000
16
+
17
+ DESCRIPTION = """
18
+ # Code Llama 34B Chat
19
+
20
+
21
+
22
+ """
23
+
24
+ LICENSE = """
25
+ <p/>
26
+
27
+ ---
28
+ As a derivate work of Code Llama by Meta,
29
+ this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/codellama-2-34b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/codellama-2-34b-chat/blob/main/USE_POLICY.md).
30
+ """
31
+
32
+
33
+ def clear_and_save_textbox(message: str) -> tuple[str, str]:
34
+ return '', message
35
+
36
+
37
+ def display_input(message: str,
38
+ history: list[tuple[str, str]]) -> list[tuple[str, str]]:
39
+ history.append((message, ''))
40
+ return history
41
+
42
+
43
+ def delete_prev_fn(
44
+ history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
45
+ try:
46
+ message, _ = history.pop()
47
+ except IndexError:
48
+ message = ''
49
+ return history, message or ''
50
+
51
+
52
+ def generate(
53
+ message: str,
54
+ history_with_input: list[tuple[str, str]],
55
+ system_prompt: str,
56
+ max_new_tokens: int,
57
+ temperature: float,
58
+ top_p: float,
59
+ top_k: int,
60
+ ) -> Iterator[list[tuple[str, str]]]:
61
+ if max_new_tokens > MAX_MAX_NEW_TOKENS:
62
+ raise ValueError
63
+
64
+ history = history_with_input[:-1]
65
+ generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
66
+ try:
67
+ first_response = next(generator)
68
+ yield history + [(message, first_response)]
69
+ except StopIteration:
70
+ yield history + [(message, '')]
71
+ for response in generator:
72
+ yield history + [(message, response)]
73
+
74
+
75
+ def process_example(message: str) -> tuple[str, list[tuple[str, str]]]:
76
+ generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 1, 0.95, 50)
77
+ for x in generator:
78
+ pass
79
+ return '', x
80
+
81
+
82
+ def check_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
83
+ input_token_length = len(message) + len(chat_history)
84
+ if input_token_length > MAX_INPUT_TOKEN_LENGTH:
85
+ raise gr.Error(f'The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.')
86
+
87
+
88
+ with gr.Blocks(css=".gradio-container {background-color: #FFE4C4}") as demo:
89
+
90
+ with gr.Group():
91
+ chatbot = gr.Chatbot(label='Chatbot')
92
+ with gr.Row():
93
+ textbox = gr.Textbox(
94
+ container=False,
95
+ show_label=False,
96
+ placeholder='Type a message...',
97
+ scale=10,
98
+ )
99
+ submit_button = gr.Button('Submit',
100
+ variant='primary',
101
+ scale=1,
102
+ min_width=0)
103
+ with gr.Row():
104
+ retry_button = gr.Button('🔄 Retry', variant='secondary')
105
+ undo_button = gr.Button('↩️ Undo', variant='secondary')
106
+ clear_button = gr.Button('🗑️ Clear', variant='secondary')
107
+
108
+ saved_input = gr.State()
109
+
110
+ with gr.Accordion(label='Advanced options', open=False):
111
+ system_prompt = gr.Textbox(label='System prompt',
112
+ value=DEFAULT_SYSTEM_PROMPT,
113
+ lines=6)
114
+ max_new_tokens = gr.Slider(
115
+ label='Max new tokens',
116
+ minimum=1,
117
+ maximum=MAX_MAX_NEW_TOKENS,
118
+ step=1,
119
+ value=DEFAULT_MAX_NEW_TOKENS,
120
+ )
121
+ temperature = gr.Slider(
122
+ label='Temperature',
123
+ minimum=0.1,
124
+ maximum=4.0,
125
+ step=0.1,
126
+ value=0.1,
127
+ )
128
+ top_p = gr.Slider(
129
+ label='Top-p (nucleus sampling)',
130
+ minimum=0.05,
131
+ maximum=1.0,
132
+ step=0.05,
133
+ value=0.9,
134
+ )
135
+ top_k = gr.Slider(
136
+ label='Top-k',
137
+ minimum=1,
138
+ maximum=1000,
139
+ step=1,
140
+ value=10,
141
+ )
142
+
143
+ gr.Examples(
144
+ examples=[
145
+ 'What is the Fibonacci sequence?',
146
+ 'Can you explain briefly what Python is good for?',
147
+ 'How can I display a grid of images in SwiftUI?',
148
+ ],
149
+ inputs=textbox,
150
+ outputs=[textbox, chatbot],
151
+ fn=process_example,
152
+ cache_examples=True,
153
+ )
154
+
155
+ gr.Markdown(LICENSE)
156
+
157
+ textbox.submit(
158
+ fn=clear_and_save_textbox,
159
+ inputs=textbox,
160
+ outputs=[textbox, saved_input],
161
+ api_name=False,
162
+ queue=False,
163
+ ).then(
164
+ fn=display_input,
165
+ inputs=[saved_input, chatbot],
166
+ outputs=chatbot,
167
+ api_name=False,
168
+ queue=False,
169
+ ).then(
170
+ fn=check_input_token_length,
171
+ inputs=[saved_input, chatbot, system_prompt],
172
+ api_name=False,
173
+ queue=False,
174
+ ).success(
175
+ fn=generate,
176
+ inputs=[
177
+ saved_input,
178
+ chatbot,
179
+ system_prompt,
180
+ max_new_tokens,
181
+ temperature,
182
+ top_p,
183
+ top_k,
184
+ ],
185
+ outputs=chatbot,
186
+ api_name=False,
187
+ )
188
+
189
+ button_event_preprocess = submit_button.click(
190
+ fn=clear_and_save_textbox,
191
+ inputs=textbox,
192
+ outputs=[textbox, saved_input],
193
+ api_name=False,
194
+ queue=False,
195
+ ).then(
196
+ fn=display_input,
197
+ inputs=[saved_input, chatbot],
198
+ outputs=chatbot,
199
+ api_name=False,
200
+ queue=False,
201
+ ).then(
202
+ fn=check_input_token_length,
203
+ inputs=[saved_input, chatbot, system_prompt],
204
+ api_name=False,
205
+ queue=False,
206
+ ).success(
207
+ fn=generate,
208
+ inputs=[
209
+ saved_input,
210
+ chatbot,
211
+ system_prompt,
212
+ max_new_tokens,
213
+ temperature,
214
+ top_p,
215
+ top_k,
216
+ ],
217
+ outputs=chatbot,
218
+ api_name=False,
219
+ )
220
+
221
+ retry_button.click(
222
+ fn=delete_prev_fn,
223
+ inputs=chatbot,
224
+ outputs=[chatbot, saved_input],
225
+ api_name=False,
226
+ queue=False,
227
+ ).then(
228
+ fn=display_input,
229
+ inputs=[saved_input, chatbot],
230
+ outputs=chatbot,
231
+ api_name=False,
232
+ queue=False,
233
+ ).then(
234
+ fn=generate,
235
+ inputs=[
236
+ saved_input,
237
+ chatbot,
238
+ system_prompt,
239
+ max_new_tokens,
240
+ temperature,
241
+ top_p,
242
+ top_k,
243
+ ],
244
+ outputs=chatbot,
245
+ api_name=False,
246
+ )
247
+
248
+ undo_button.click(
249
+ fn=delete_prev_fn,
250
+ inputs=chatbot,
251
+ outputs=[chatbot, saved_input],
252
+ api_name=False,
253
+ queue=False,
254
+ ).then(
255
+ fn=lambda x: x,
256
+ inputs=[saved_input],
257
+ outputs=textbox,
258
+ api_name=False,
259
+ queue=False,
260
+ )
261
+
262
+ clear_button.click(
263
+ fn=lambda: ([], ''),
264
+ outputs=[chatbot, saved_input],
265
+ queue=False,
266
+ api_name=False,
267
+ )
268
+
269
+ demo.queue(max_size=32).launch(share=HF_PUBLIC)
270
+