prithivMLmods commited on
Commit
2cd2236
·
verified ·
1 Parent(s): 0b1f5f8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
14
+
15
+ hf_hub_download(
16
+ repo_id="mradermacher/GWQ-9B-Preview-GGUF",
17
+ filename="GWQ-9B-Preview.Q5_K_M.gguf",
18
+ local_dir="./models"
19
+ )
20
+
21
+ hf_hub_download(
22
+ repo_id="mradermacher/GWQ-9B-Preview2-GGUF",
23
+ filename="GWQ-9B-Preview2.Q5_K_M.gguf",
24
+ local_dir="./models"
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
+ demo = gr.ChatInterface(
102
+ respond,
103
+ additional_inputs=[
104
+ gr.Dropdown([
105
+ 'GWQ-9B-Preview.Q5_K_M.gguf',
106
+ 'GWQ-9B-Preview2.Q5_K_M.gguf'
107
+ ],
108
+ value="GWQ-9B-Preview.Q5_K_M.gguf",
109
+ label="Model"
110
+ ),
111
+ gr.Textbox(value="You are a helpful assistant.", label="System message"),
112
+ gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
113
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
114
+ gr.Slider(
115
+ minimum=0.1,
116
+ maximum=1.0,
117
+ value=0.95,
118
+ step=0.05,
119
+ label="Top-p",
120
+ ),
121
+ gr.Slider(
122
+ minimum=0,
123
+ maximum=100,
124
+ value=40,
125
+ step=1,
126
+ label="Top-k",
127
+ ),
128
+ gr.Slider(
129
+ minimum=0.0,
130
+ maximum=2.0,
131
+ value=1.1,
132
+ step=0.1,
133
+ label="Repetition penalty",
134
+ ),
135
+ ],
136
+ title="GWQ PREV",
137
+ chatbot=gr.Chatbot(
138
+ scale=1,
139
+ show_copy_button=True,
140
+ type="messages"
141
+ )
142
+ )
143
+
144
+ if __name__ == "__main__":
145
+ demo.launch()