wannaphong commited on
Commit
325d5a7
·
verified ·
1 Parent(s): f336f90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -4
app.py CHANGED
@@ -1,7 +1,173 @@
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Thank you code from https://huggingface.co/spaces/gokaygokay/Gemma-2-llamacpp
2
+ import spaces
3
+ import os
4
+ import json
5
+ import subprocess
6
+ from llama_cpp import Llama
7
+ from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
8
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
9
+ from llama_cpp_agent.chat_history import BasicChatHistory
10
+ from llama_cpp_agent.chat_history.messages import Roles
11
  import gradio as gr
12
+ from huggingface_hub import hf_hub_download
13
 
 
 
14
 
15
+ # huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
16
+
17
+ hf_hub_download(
18
+ repo_id="wannaphong/KhanomTanLLM-1B-Instruct-Q2_K-GGUF",
19
+ filename="khanomtanllm-1b-instruct-q2_k.gguf",
20
+ local_dir="./models"
21
+ )
22
+
23
+ hf_hub_download(
24
+ repo_id="wannaphong/KhanomTanLLM-3B-Instruct-Q2_K-GGUF",
25
+ filename="khanomtanllm-3b-instruct-q2_k.gguf",
26
+ local_dir="./models"
27
+ )
28
+
29
+ # hf_hub_download(
30
+ # repo_id="google/gemma-2-2b-it-GGUF",
31
+ # filename="2b_it_v2.gguf",
32
+ # local_dir="./models",
33
+ # token=huggingface_token
34
+ # )
35
+
36
+
37
+
38
+ llm = None
39
+ llm_model = None
40
+
41
+ @spaces.GPU(duration=120)
42
+ def respond(
43
+ message,
44
+ history: list[tuple[str, str]],
45
+ model,
46
+ system_message,
47
+ max_tokens,
48
+ temperature,
49
+ min_p,
50
+ top_p,
51
+ top_k,
52
+ repeat_penalty,
53
+ ):
54
+ chat_template = MessagesFormatterType.GEMMA_2
55
+
56
+ global llm
57
+ global llm_model
58
+
59
+ if llm is None or llm_model != model:
60
+ llm = Llama(
61
+ model_path=f"models/{model}",
62
+ flash_attn=True,
63
+ #n_gpu_layers=81,
64
+ n_batch=1024,
65
+ n_ctx=2048,
66
+ )
67
+ llm_model = model
68
+
69
+ provider = LlamaCppPythonProvider(llm)
70
+
71
+ agent = LlamaCppAgent(
72
+ provider,
73
+ system_prompt=f"{system_message}",
74
+ predefined_messages_formatter_type=chat_template,
75
+ debug_output=True
76
+ )
77
+
78
+ settings = provider.get_provider_default_settings()
79
+ settings.temperature = temperature
80
+ settings.top_k = top_k
81
+ settings.top_p = top_p
82
+ settings.min_p = min_p
83
+ settings.max_tokens = max_tokens
84
+ settings.repeat_penalty = repeat_penalty
85
+ settings.stream = True
86
+
87
+ messages = BasicChatHistory()
88
+
89
+ for msn in history:
90
+ user = {
91
+ 'role': Roles.user,
92
+ 'content': msn[0]
93
+ }
94
+ assistant = {
95
+ 'role': Roles.assistant,
96
+ 'content': msn[1]
97
+ }
98
+ messages.add_message(user)
99
+ messages.add_message(assistant)
100
+
101
+ stream = agent.get_chat_response(
102
+ message,
103
+ llm_sampling_settings=settings,
104
+ chat_history=messages,
105
+ returns_streaming_generator=True,
106
+ print_output=False
107
+ )
108
+
109
+ outputs = ""
110
+ for output in stream:
111
+ outputs += output
112
+ yield outputs
113
+
114
+ description = """
115
+ """
116
+
117
+ demo = gr.ChatInterface(
118
+ respond,
119
+ additional_inputs=[
120
+ gr.Dropdown([
121
+ 'khanomtanllm-1b-instruct-q2_k.gguf',
122
+ 'khanomtanllm-3b-instruct-q2_k.gguf',
123
+ ],
124
+ value="khanomtanllm-1b-instruct-q2_k.gguf",
125
+ label="Model"
126
+ ),
127
+ gr.Textbox(value="You are a helpful assistant.", label="System message"),
128
+ gr.Slider(minimum=1, maximum=2048, value=2048, step=1, label="Max tokens"),
129
+ gr.Slider(minimum=0.1, maximum=4.0, value=2.0, step=0.1, label="Temperature"),
130
+ gr.Slider(
131
+ minimum=0.1,
132
+ maximum=1.0,
133
+ value=0.7,
134
+ step=0.05,
135
+ label="min-p",
136
+ ),
137
+ gr.Slider(
138
+ minimum=0.1,
139
+ maximum=1.0,
140
+ value=0.95,
141
+ step=0.05,
142
+ label="Top-p",
143
+ ),
144
+ gr.Slider(
145
+ minimum=0,
146
+ maximum=100,
147
+ value=40,
148
+ step=1,
149
+ label="Top-k",
150
+ ),
151
+ gr.Slider(
152
+ minimum=0.0,
153
+ maximum=2.0,
154
+ value=1.1,
155
+ step=0.1,
156
+ label="Repetition penalty",
157
+ ),
158
+ ],
159
+ retry_btn="Retry",
160
+ undo_btn="Undo",
161
+ clear_btn="Clear",
162
+ submit_btn="Send",
163
+ title="Chat with KhanomTanLLM using llama.cpp",
164
+ description=description,
165
+ chatbot=gr.Chatbot(
166
+ scale=1,
167
+ likeable=False,
168
+ show_copy_button=True
169
+ )
170
+ )
171
+
172
+ if __name__ == "__main__":
173
+ demo.launch()