|
import gradio as gr |
|
import torch |
|
from transformers import AutoTokenizer |
|
from open_lm.utils.transformers.hf_config import OpenLMConfig |
|
from open_lm.utils.transformers.hf_model import OpenLMforCausalLM |
|
|
|
title = """# ππ»ββοΈ Welcome to Tonic's DCLM 1B""" |
|
|
|
|
|
|
|
model_name = "TRI-ML/DCLM-1B-IT" |
|
|
|
|
|
config = OpenLMConfig.from_pretrained(model_name) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = OpenLMforCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="cuda", config=config ) |
|
|
|
|
|
def create_prompt(instruction): |
|
PROMPT = '''Below is an instruction that describes a task.\n\nWrite a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:''' |
|
return PROMPT.format(instruction=instruction) |
|
|
|
|
|
def respond(message, history, system_message, max_tokens, temperature, top_p): |
|
|
|
prompt = create_prompt(message) |
|
|
|
|
|
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(torch.device('cuda')) |
|
|
|
|
|
output = model.generate(input_ids, max_length=max_tokens, top_p=top_p, do_sample=True, temperature=temperature) |
|
|
|
|
|
response = tokenizer.decode(output[0][len(input_ids[0]):]) |
|
response = response.split("<|endoftext|>")[0] |
|
|
|
return response |
|
|
|
|
|
demo = gr.ChatInterface( |
|
gr.markdown(title), |
|
|
|
respond, |
|
additional_inputs=[ |
|
|
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)") |
|
], |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|