QiyuWu commited on
Commit
0cc47dd
·
verified ·
1 Parent(s): 37515d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -82
app.py CHANGED
@@ -1,82 +1,82 @@
1
- import os
2
- import gradio as gr
3
- import copy
4
- from llama_cpp import Llama
5
- from huggingface_hub import hf_hub_download
6
-
7
-
8
- llm = Llama(
9
- model_path = "./dpo-internlm2-1_8b.Q4_K_M.gguf",
10
- n_ctx=2048,
11
- n_threads=2,
12
- seed=57199,
13
- batch_size=1024,
14
- cache=True
15
- )
16
-
17
-
18
- def generate_text(
19
- message,
20
- history: list[tuple[str, str]],
21
- system_message,
22
- max_tokens,
23
- temperature,
24
- top_p,
25
- ):
26
- temp = ""
27
- input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
28
- for interaction in history:
29
- input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
30
-
31
- input_prompt = input_prompt + str(message) + " [/INST] "
32
-
33
- output = llm(
34
- input_prompt,
35
- temperature=temperature,
36
- top_p=top_p,
37
- top_k=40,
38
- repeat_penalty=1.2,
39
- max_tokens=max_tokens,
40
- stop=["[/INST]", "[UNUSED_TOKEN_145]", "<<SYS>>"],
41
- stream=True,
42
-
43
- )
44
- for out in output:
45
- stream = copy.deepcopy(out)
46
- temp += stream["choices"][0]["text"]
47
- yield temp
48
-
49
-
50
- demo = gr.ChatInterface(
51
- generate_text,
52
- title="DPO-Internlm2-1_8B",
53
- description="Running LLM with llama-cpp-python",
54
- theme = gr.themes.Soft(),
55
- examples=[
56
- ['How to setup a human base on Mars? Give short answer.'],
57
- ['Explain theory of relativity to me like I’m 8 years old.'],
58
- ['What is 9,000 * 9,000?'],
59
- ['Write a pun-filled happy birthday message to my friend Alex.'],
60
- ['Justify why a penguin might make a good king of the jungle.']
61
- ],
62
- cache_examples=False,
63
- retry_btn=None,
64
- undo_btn="Delete Previous",
65
- clear_btn="Clear",
66
- additional_inputs=[
67
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
68
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
69
- gr.Slider(minimum=0.1, maximum=4.0, value=0.3, step=0.1, label="Temperature"),
70
- gr.Slider(
71
- minimum=0.1,
72
- maximum=1.0,
73
- value=0.90,
74
- step=0.05,
75
- label="Top-p (nucleus sampling)",
76
- ),
77
- ],
78
- )
79
-
80
-
81
- if __name__ == "__main__":
82
- demo.launch()
 
1
+ import os
2
+ import gradio as gr
3
+ import copy
4
+ from llama_cpp import Llama
5
+ from huggingface_hub import hf_hub_download
6
+
7
+
8
+ llm = Llama(
9
+ model_path = "./dpo-internlm2-1_8b.Q4_K_M.gguf",
10
+ n_ctx=2048,
11
+ n_threads=2,
12
+ seed=57199,
13
+ batch_size=1024,
14
+ cache=True
15
+ )
16
+
17
+
18
+ def generate_text(
19
+ message,
20
+ history: list[tuple[str, str]],
21
+ system_message,
22
+ max_tokens,
23
+ temperature,
24
+ top_p,
25
+ ):
26
+ temp = ""
27
+ input_prompt = f"<s> <|im_start|>system:{system_message}<|im_end|>"
28
+ for interaction in history:
29
+ input_prompt = input_prompt + "<|im_start|>user:" + str(interaction[0]) + "<|im_end|><|im_start|>" + "assistant:" + str(interaction[1]) + "<|im_end|>"
30
+
31
+ input_prompt = input_prompt + "<|im_start|>user:" + str(message) + "<|im_end|><|im_start|>assistant:"
32
+
33
+ output = llm(
34
+ input_prompt,
35
+ temperature=temperature,
36
+ top_p=top_p,
37
+ top_k=40,
38
+ repeat_penalty=1.2,
39
+ max_tokens=max_tokens,
40
+ stop=["<|im_end|>"],
41
+ stream=True,
42
+
43
+ )
44
+ for out in output:
45
+ stream = copy.deepcopy(out)
46
+ temp += stream["choices"][0]["text"]
47
+ yield temp
48
+
49
+
50
+ demo = gr.ChatInterface(
51
+ generate_text,
52
+ title="DPO-Internlm2-1_8B",
53
+ description="Running LLM with llama-cpp-python",
54
+ theme = gr.themes.Soft(),
55
+ examples=[
56
+ ['How to setup a human base on Mars? Give short answer.'],
57
+ ['Explain theory of relativity to me like I’m 8 years old.'],
58
+ ['What is 9,000 * 9,000?'],
59
+ ['Write a pun-filled happy birthday message to my friend Alex.'],
60
+ ['Justify why a penguin might make a good king of the jungle.']
61
+ ],
62
+ cache_examples=False,
63
+ retry_btn=None,
64
+ undo_btn="Delete Previous",
65
+ clear_btn="Clear",
66
+ additional_inputs=[
67
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
68
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
69
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.3, step=0.1, label="Temperature"),
70
+ gr.Slider(
71
+ minimum=0.1,
72
+ maximum=1.0,
73
+ value=0.90,
74
+ step=0.05,
75
+ label="Top-p (nucleus sampling)",
76
+ ),
77
+ ],
78
+ )
79
+
80
+
81
+ if __name__ == "__main__":
82
+ demo.launch()