el-el-san commited on
Commit
42708f7
1 Parent(s): 8905663

Update app.py

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