Spaces:
Sleeping
Sleeping
def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p): | |
if CHAT_TEMPLATE == "Auto": | |
stop_tokens = [tokenizer.eos_token_id] | |
instruction = system_prompt + "\n\n" | |
for user, assistant in history: | |
instruction += f"User: {user}\nAssistant: {assistant}\n" | |
instruction += f"User: {message}\nAssistant:" | |
elif CHAT_TEMPLATE == "ChatML": | |
stop_tokens = ["<|endoftext|>", "<|im_end|>"] | |
instruction = '<|im_start|>system\n' + system_prompt + '\n<|im_end|>\n' | |
for user, assistant in history: | |
instruction += f'<|im_start|>user\n{user}\n<|im_end|>\n<|im_start|>assistant\n{assistant}\n<|im_end|>\n' | |
instruction += f'<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n' | |
elif CHAT_TEMPLATE == "Mistral Instruct": | |
stop_tokens = ["</s>", "[INST]", "[INST] ", "<s>", "[/INST]", "[/INST] "] | |
instruction = f'<s>[INST] {system_prompt}\n' | |
for user, assistant in history: | |
instruction += f'{user} [/INST] {assistant}</s>[INST]' | |
instruction += f' {message} [/INST]' | |
else: | |
raise Exception("Incorrect chat template, select 'Auto', 'ChatML' or 'Mistral Instruct'") | |
print(instruction) | |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) | |
enc = tokenizer(instruction, return_tensors="pt", padding=True, truncation=True) | |
input_ids, attention_mask = enc.input_ids, enc.attention_mask | |
if input_ids.shape[1] > CONTEXT_LENGTH: | |
input_ids = input_ids[:, -CONTEXT_LENGTH:] | |
attention_mask = attention_mask[:, -CONTEXT_LENGTH:] | |
generate_kwargs = dict( | |
input_ids=input_ids.to(device), | |
attention_mask=attention_mask.to(device), | |
streamer=streamer, | |
do_sample=True, | |
temperature=temperature, | |
max_new_tokens=max_new_tokens, | |
top_k=top_k, | |
repetition_penalty=repetition_penalty, | |
top_p=top_p | |
) | |
t = Thread(target=model.generate, kwargs=generate_kwargs) | |
t.start() | |
outputs = [] | |
for new_token in streamer: | |
outputs.append(new_token) | |
if new_token in stop_tokens: | |
break | |
result = "".join(outputs) | |
# Wrapping result in Markdown for LaTeX rendering | |
yield f"$$ {result} $$" | |
# Create Gradio interface | |
gr.ChatInterface( | |
predict, | |
title=EMOJI + " " + MODEL_NAME, | |
description=DESCRIPTION, | |
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False), | |
additional_inputs=[ | |
gr.Textbox("You are a code assistant.", label="System prompt"), | |
gr.Slider(0, 1, 0.3, label="Temperature"), | |
gr.Slider(128, 4096, 1024, label="Max new tokens"), | |
gr.Slider(1, 80, 40, label="Top K sampling"), | |
gr.Slider(0, 2, 1.1, label="Repetition penalty"), | |
gr.Slider(0, 1, 0.95, label="Top P sampling"), | |
], | |
theme=gr.themes.Soft(primary_hue=COLOR), | |
).queue().launch() | |