File size: 2,632 Bytes
c88e2a4
651f3a2
 
 
6d32964
651f3a2
cdb21b9
 
 
 
 
6d32964
651f3a2
 
 
 
 
 
 
 
 
 
3e3905b
651f3a2
c88e2a4
 
651f3a2
 
 
 
 
 
 
 
 
 
 
 
381d701
203f6c1
381d701
3e3905b
5e5fa62
651f3a2
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import spaces
import gradio as gr  
import torch  
from transformers import AutoTokenizer, AutoModelForCausalLM

title = """# 🙋🏻‍♂️ Welcome to Tonic's Minitron-8B-Base"""
description = """
Minitron is a family of small language models (SLMs) obtained by pruning [NVIDIA's](https://huggingface.co/nvidia) Nemotron-4 15B model. We prune model embedding size, attention heads, and MLP intermediate dimension, following which, we perform continued training with distillation to arrive at the final models.
### Join us : 
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [BuildTonic](https://github.com/buildtonic/)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
"""

# Load the tokenizer and model
model_path = "nvidia/Minitron-8B-Base"
tokenizer = AutoTokenizer.from_pretrained(model_path)

device='cuda'
dtype=torch.bfloat16
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=dtype, device_map=device)

# Define the prompt format  
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)  

@spaces.GPU  
def respond(message, history, system_message, max_tokens, temperature, top_p):  
    prompt = create_prompt(message)  
      
    input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)

    output_ids = model.generate(input_ids, max_length=50, num_return_sequences=1)

    output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
      
    return output_text  
  
demo = gr.ChatInterface(
    title=gr.Markdown(title),
    # description=gr.Markdown(description),
    fn=respond,  
    additional_inputs=[
        gr.Textbox(value="You are Minitron an AI assistant created by Tonic-AI", label="System message"),   
        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()