Bellamy66 commited on
Commit
8914120
β€’
1 Parent(s): ef97e84

Upload app (2).py

Browse files
Files changed (1) hide show
  1. app (2).py +279 -0
app (2).py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Iterator
2
+
3
+ import gradio as gr
4
+ import torch
5
+
6
+ from model import get_input_token_length, run
7
+
8
+ DEFAULT_SYSTEM_PROMPT = """\
9
+ 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.\
10
+ """
11
+ MAX_MAX_NEW_TOKENS = 4096
12
+ DEFAULT_MAX_NEW_TOKENS = 1024
13
+ MAX_INPUT_TOKEN_LENGTH = 4000
14
+
15
+ DESCRIPTION = """
16
+ # Code Llama 13B Chat
17
+
18
+ This Space demonstrates model [CodeLlama-13b-Instruct](https://huggingface.co/codellama/CodeLlama-13b-Instruct-hf) by Meta, a Code Llama model with 13B parameters fine-tuned for chat instructions and specialized on code tasks. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints).
19
+
20
+ πŸ”Ž For more details about the Code Llama family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/codellama) or [the paper](https://huggingface.co/papers/2308.12950).
21
+
22
+ πŸƒπŸ» Check out our [Playground](https://huggingface.co/spaces/codellama/codellama-playground) for a super-fast code completion demo that leverages a streaming [inference endpoint](https://huggingface.co/inference-endpoints).
23
+
24
+ πŸ’ͺ For a chat demo of the largest Code Llama model (34B parameters), you can [select Code Llama in Hugging Chat!](https://huggingface.co/chat/)
25
+
26
+ """
27
+
28
+ LICENSE = """
29
+ <p/>
30
+
31
+ ---
32
+ As a derivate work of Code Llama by Meta,
33
+ this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/codellama-2-13b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/codellama-2-13b-chat/blob/main/USE_POLICY.md).
34
+ """
35
+
36
+ if not torch.cuda.is_available():
37
+ DESCRIPTION += '\n<p>Running on CPU πŸ₯Ά This demo does not work on CPU.</p>'
38
+
39
+
40
+ def clear_and_save_textbox(message: str) -> tuple[str, str]:
41
+ return '', message
42
+
43
+
44
+ def display_input(message: str,
45
+ history: list[tuple[str, str]]) -> list[tuple[str, str]]:
46
+ history.append((message, ''))
47
+ return history
48
+
49
+
50
+ def delete_prev_fn(
51
+ history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
52
+ try:
53
+ message, _ = history.pop()
54
+ except IndexError:
55
+ message = ''
56
+ return history, message or ''
57
+
58
+
59
+ def generate(
60
+ message: str,
61
+ history_with_input: list[tuple[str, str]],
62
+ system_prompt: str,
63
+ max_new_tokens: int,
64
+ temperature: float,
65
+ top_p: float,
66
+ top_k: int,
67
+ ) -> Iterator[list[tuple[str, str]]]:
68
+ if max_new_tokens > MAX_MAX_NEW_TOKENS:
69
+ raise ValueError
70
+
71
+ history = history_with_input[:-1]
72
+ generator = run(message, history, system_prompt, max_new_tokens, temperature, top_p, top_k)
73
+ try:
74
+ first_response = next(generator)
75
+ yield history + [(message, first_response)]
76
+ except StopIteration:
77
+ yield history + [(message, '')]
78
+ for response in generator:
79
+ yield history + [(message, response)]
80
+
81
+
82
+ def process_example(message: str) -> tuple[str, list[tuple[str, str]]]:
83
+ generator = generate(message, [], DEFAULT_SYSTEM_PROMPT, 1024, 1, 0.95, 50)
84
+ for x in generator:
85
+ pass
86
+ return '', x
87
+
88
+
89
+ def check_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
90
+ input_token_length = get_input_token_length(message, chat_history, system_prompt)
91
+ if input_token_length > MAX_INPUT_TOKEN_LENGTH:
92
+ raise gr.Error(f'The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.')
93
+
94
+
95
+ with gr.Blocks(css='style.css') as demo:
96
+ gr.Markdown(DESCRIPTION)
97
+ gr.DuplicateButton(value='Duplicate Space for private use',
98
+ elem_id='duplicate-button')
99
+
100
+ with gr.Group():
101
+ chatbot = gr.Chatbot(label='Chatbot')
102
+ with gr.Row():
103
+ textbox = gr.Textbox(
104
+ container=False,
105
+ show_label=False,
106
+ placeholder='Type a message...',
107
+ scale=10,
108
+ )
109
+ submit_button = gr.Button('Submit',
110
+ variant='primary',
111
+ scale=1,
112
+ min_width=0)
113
+ with gr.Row():
114
+ retry_button = gr.Button('πŸ”„ Retry', variant='secondary')
115
+ undo_button = gr.Button('↩️ Undo', variant='secondary')
116
+ clear_button = gr.Button('πŸ—‘οΈ Clear', variant='secondary')
117
+
118
+ saved_input = gr.State()
119
+
120
+ with gr.Accordion(label='Advanced options', open=False):
121
+ system_prompt = gr.Textbox(label='System prompt',
122
+ value=DEFAULT_SYSTEM_PROMPT,
123
+ lines=6)
124
+ max_new_tokens = gr.Slider(
125
+ label='Max new tokens',
126
+ minimum=1,
127
+ maximum=MAX_MAX_NEW_TOKENS,
128
+ step=1,
129
+ value=DEFAULT_MAX_NEW_TOKENS,
130
+ )
131
+ temperature = gr.Slider(
132
+ label='Temperature',
133
+ minimum=0.1,
134
+ maximum=4.0,
135
+ step=0.1,
136
+ value=0.1,
137
+ )
138
+ top_p = gr.Slider(
139
+ label='Top-p (nucleus sampling)',
140
+ minimum=0.05,
141
+ maximum=1.0,
142
+ step=0.05,
143
+ value=0.9,
144
+ )
145
+ top_k = gr.Slider(
146
+ label='Top-k',
147
+ minimum=1,
148
+ maximum=1000,
149
+ step=1,
150
+ value=10,
151
+ )
152
+
153
+ gr.Examples(
154
+ examples=[
155
+ 'What is the Fibonacci sequence?',
156
+ 'Can you explain briefly what Python is good for?',
157
+ 'How can I display a grid of images in SwiftUI?',
158
+ ],
159
+ inputs=textbox,
160
+ outputs=[textbox, chatbot],
161
+ fn=process_example,
162
+ cache_examples=True,
163
+ )
164
+
165
+ gr.Markdown(LICENSE)
166
+
167
+ textbox.submit(
168
+ fn=clear_and_save_textbox,
169
+ inputs=textbox,
170
+ outputs=[textbox, saved_input],
171
+ api_name=False,
172
+ queue=False,
173
+ ).then(
174
+ fn=display_input,
175
+ inputs=[saved_input, chatbot],
176
+ outputs=chatbot,
177
+ api_name=False,
178
+ queue=False,
179
+ ).then(
180
+ fn=check_input_token_length,
181
+ inputs=[saved_input, chatbot, system_prompt],
182
+ api_name=False,
183
+ queue=False,
184
+ ).success(
185
+ fn=generate,
186
+ inputs=[
187
+ saved_input,
188
+ chatbot,
189
+ system_prompt,
190
+ max_new_tokens,
191
+ temperature,
192
+ top_p,
193
+ top_k,
194
+ ],
195
+ outputs=chatbot,
196
+ api_name=False,
197
+ )
198
+
199
+ button_event_preprocess = submit_button.click(
200
+ fn=clear_and_save_textbox,
201
+ inputs=textbox,
202
+ outputs=[textbox, saved_input],
203
+ api_name=False,
204
+ queue=False,
205
+ ).then(
206
+ fn=display_input,
207
+ inputs=[saved_input, chatbot],
208
+ outputs=chatbot,
209
+ api_name=False,
210
+ queue=False,
211
+ ).then(
212
+ fn=check_input_token_length,
213
+ inputs=[saved_input, chatbot, system_prompt],
214
+ api_name=False,
215
+ queue=False,
216
+ ).success(
217
+ fn=generate,
218
+ inputs=[
219
+ saved_input,
220
+ chatbot,
221
+ system_prompt,
222
+ max_new_tokens,
223
+ temperature,
224
+ top_p,
225
+ top_k,
226
+ ],
227
+ outputs=chatbot,
228
+ api_name=False,
229
+ )
230
+
231
+ retry_button.click(
232
+ fn=delete_prev_fn,
233
+ inputs=chatbot,
234
+ outputs=[chatbot, saved_input],
235
+ api_name=False,
236
+ queue=False,
237
+ ).then(
238
+ fn=display_input,
239
+ inputs=[saved_input, chatbot],
240
+ outputs=chatbot,
241
+ api_name=False,
242
+ queue=False,
243
+ ).then(
244
+ fn=generate,
245
+ inputs=[
246
+ saved_input,
247
+ chatbot,
248
+ system_prompt,
249
+ max_new_tokens,
250
+ temperature,
251
+ top_p,
252
+ top_k,
253
+ ],
254
+ outputs=chatbot,
255
+ api_name=False,
256
+ )
257
+
258
+ undo_button.click(
259
+ fn=delete_prev_fn,
260
+ inputs=chatbot,
261
+ outputs=[chatbot, saved_input],
262
+ api_name=False,
263
+ queue=False,
264
+ ).then(
265
+ fn=lambda x: x,
266
+ inputs=[saved_input],
267
+ outputs=textbox,
268
+ api_name=False,
269
+ queue=False,
270
+ )
271
+
272
+ clear_button.click(
273
+ fn=lambda: ([], ''),
274
+ outputs=[chatbot, saved_input],
275
+ queue=False,
276
+ api_name=False,
277
+ )
278
+
279
+ demo.queue(max_size=20).launch()