Spaces:
Sleeping
Sleeping
import kminterface | |
import requests | |
import json | |
def tokenize(text): | |
return text | |
# return tok.encode(text, add_special_tokens=False) | |
def format_prompt(message, history): | |
prompt = "" | |
for user_prompt, bot_response in history: | |
prompt += "<s>" + tokenize("[INST]") + tokenize(user_prompt) + tokenize("[/INST]") | |
prompt += tokenize(bot_response) + "</s> " | |
prompt += tokenize("[INST]") + tokenize(message) + tokenize("[/INST]") | |
return prompt | |
def generate(prompt, history, system_prompt, temperature=0.2, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0): | |
print(type(history), history) | |
temperature = float(temperature) | |
if temperature < 1e-2: | |
temperature = 1e-2 | |
top_p = float(top_p) | |
generate_kwargs = dict( | |
temperature=temperature, | |
max_new_tokens=max_new_tokens, | |
top_p=top_p, | |
repetition_penalty=repetition_penalty, | |
do_sample=True, | |
seed=42, | |
) | |
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history) | |
stream = kminterface.client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False) | |
output = "" | |
for response in stream: | |
print(response.token.text + "/n") | |
output += response.token.text | |
yield output | |
return output | |
demo = kminterface.gr.ChatInterface(fn=generate, | |
chatbot=kminterface.mychatbot, | |
additional_inputs=kminterface.additional_inputs, | |
title="Kamran's Mixtral 8x7b Chat", | |
retry_btn=None, | |
undo_btn=None | |
) | |
demo.queue().launch(show_api=False) | |