Subhasmita commited on
Commit
36fc761
1 Parent(s): a05dc84

application init

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