Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from open_lm.utils.transformers.hf_config import OpenLMConfig
|
| 5 |
+
from open_lm.utils.transformers.hf_model import OpenLMforCausalLM
|
| 6 |
+
|
| 7 |
+
title = """# ππ»ββοΈ Welcome to Tonic's DCLM 1B"""
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load the model and tokenizer
|
| 11 |
+
model_name = "TRI-ML/DCLM-1B-IT"
|
| 12 |
+
|
| 13 |
+
# Load the configuration, tokenizer, and model separately
|
| 14 |
+
config = OpenLMConfig.from_pretrained(model_name)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
model = OpenLMforCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="cuda", config=config)
|
| 17 |
+
|
| 18 |
+
# Define the prompt format
|
| 19 |
+
def create_prompt(instruction):
|
| 20 |
+
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:'''
|
| 21 |
+
return PROMPT.format(instruction=instruction)
|
| 22 |
+
|
| 23 |
+
# Define the respond function for Gradio
|
| 24 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 25 |
+
# Create the prompt
|
| 26 |
+
prompt = create_prompt(message)
|
| 27 |
+
|
| 28 |
+
# Tokenize the input
|
| 29 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(torch.device('cuda'))
|
| 30 |
+
|
| 31 |
+
# Generate the response
|
| 32 |
+
output = model.generate(input_ids, max_length=max_tokens, top_p=top_p, do_sample=True, temperature=temperature)
|
| 33 |
+
|
| 34 |
+
# Decode the response
|
| 35 |
+
response = tokenizer.decode(output[0][len(input_ids[0]):])
|
| 36 |
+
response = response.split("<|endoftext|>")[0]
|
| 37 |
+
|
| 38 |
+
return response
|
| 39 |
+
|
| 40 |
+
# Create Gradio ChatInterface
|
| 41 |
+
demo = gr.ChatInterface(
|
| 42 |
+
gr.markdown(title),
|
| 43 |
+
# gr.markdown(description),
|
| 44 |
+
respond,
|
| 45 |
+
additional_inputs=[
|
| 46 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 47 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 48 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 49 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 50 |
+
],
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|