Tonic commited on
Commit
2ff4050
Β·
verified Β·
1 Parent(s): b9c0e2b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from transformers import LlamaTokenizerFast
4
+ import torch
5
+ import sentencepiece
6
+ import os
7
+ import gradio as gr
8
+
9
+
10
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:120'
11
+ model_id = "eastwind/grok-1-hf-4bit"
12
+ tokenizer_id = "Xenova/grok-1-tokenizer"
13
+ # tokenizer_path = "./"
14
+ # eos_token_id = 7
15
+
16
+ DESCRIPTION = """
17
+ # Welcome to Tonic's Grok-1
18
+ """
19
+
20
+ #tokenizer = AutoTokenizer.from_pretrained(model_id, device_map="auto", trust_remote_code=True)
21
+ tokenizer = LlamaTokenizerFast.from_pretrained(tokenizer_id, trust_remote_code=True)
22
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True)
23
+
24
+ def format_prompt(user_message, system_message="You are Grok-1, an AI language model created by Tonic-AI. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and follow ethical guidelines and promote positive behavior.\n\n"):
25
+ # prompt = f"<|im_start|>assistant\n{system_message}<|im_end|>\n<|im_start|>\nuser\n{user_message}<|im_end|>\nassistant\n"
26
+ prompt = f"{system_message}{user_message}"
27
+ return prompt
28
+
29
+ @spaces.GPU
30
+ def predict(message, system_message, max_new_tokens=600, temperature=3.5, top_p=0.9, top_k=40, do_sample=False):
31
+ formatted_prompt = format_prompt(message, system_message)
32
+
33
+ input_ids = tokenizer.encode(formatted_prompt, return_tensors='pt')
34
+ input_ids = input_ids.to(model.device)
35
+
36
+ response_ids = model.generate(
37
+ input_ids,
38
+ max_length=max_new_tokens + input_ids.shape[1],
39
+ temperature=temperature,
40
+ top_p=top_p,
41
+ top_k=top_k,
42
+ no_repeat_ngram_size=9,
43
+ pad_token_id=tokenizer.eos_token_id,
44
+ do_sample=do_sample
45
+ )
46
+
47
+ response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
48
+ truncate_str = "<|im_end|>"
49
+ if truncate_str and truncate_str in response:
50
+ response = response.split(truncate_str)[0]
51
+
52
+ return [("bot", response)]
53
+
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown(DESCRIPTION)
56
+ with gr.Group():
57
+ textbox = gr.Textbox(placeholder='Your Message Here', label='Your Message', lines=2)
58
+ system_prompt = gr.Textbox(placeholder='Provide a System Prompt In The First Person', label='System Prompt', lines=2, value="You are YiTonic, an AI language model created by Tonic-AI. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.")
59
+
60
+ with gr.Group():
61
+ chatbot = gr.Chatbot(label='Grok-1🀯')
62
+
63
+ with gr.Group():
64
+ submit_button = gr.Button('Submit', variant='primary')
65
+
66
+ with gr.Accordion(label='Advanced options', open=False):
67
+ max_new_tokens = gr.Slider(label='Max New Tokens', minimum=1, maximum=55000, step=1, value=4056)
68
+ temperature = gr.Slider(label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.2)
69
+ top_p = gr.Slider(label='Top-P (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.9)
70
+ top_k = gr.Slider(label='Top-K', minimum=1, maximum=1000, step=1, value=40)
71
+ do_sample_checkbox = gr.Checkbox(label='Disable for faster inference', value=True)
72
+
73
+ submit_button.click(
74
+ fn=predict,
75
+ inputs=[textbox, system_prompt, max_new_tokens, temperature, top_p, top_k, do_sample_checkbox],
76
+ outputs=chatbot
77
+ )
78
+
79
+ demo.launch()