kaerez commited on
Commit
04b4c37
1 Parent(s): b8f0c0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -1
app.py CHANGED
@@ -1,3 +1,169 @@
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
 
3
- gr.load("models/bartowski/Reflection-Llama-3.1-70B-GGUF").launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ llm = None
13
+ llm_model = None
14
+
15
+ hf_hub_download(
16
+ repo_id="bartowski/Reflection-Llama-3.1-70B-GGUF",
17
+ filename="Reflection-Llama-3.1-70B-Q3_K_M.gguf",
18
+ local_dir = "./models"
19
+ )
20
+
21
+
22
+
23
+ def get_messages_formatter_type(model_name):
24
+ if "Llama" in model_name:
25
+ return MessagesFormatterType.LLAMA_3
26
+ else:
27
+ raise ValueError(f"Unsupported model: {model_name}")
28
+
29
+
30
+ @spaces.GPU
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
+ global llm
43
+ global llm_model
44
+
45
+ chat_template = get_messages_formatter_type(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><center>
102
+ <a href="https://huggingface.co/mattshumer/Reflection-Llama-3.1-70B" target="_blank">[Reflection Llama 3.1 70B Model Page]</a>
103
+
104
+ <a href="https://huggingface.co/bartowski/Reflection-Llama-3.1-70B-GGUF" target="_blank">[70B Model GGUF]</a>
105
+ </center></p>
106
+ """
107
+
108
+ demo = gr.ChatInterface(
109
+ respond,
110
+ additional_inputs=[
111
+ gr.Dropdown([
112
+ "Reflection-Llama-3.1-70B-Q3_K_M.gguf"
113
+ ],
114
+ value="Reflection-Llama-3.1-70B-Q3_K_M.gguf",
115
+ label="Model"
116
+ ),
117
+ gr.Textbox(value="You are a world-class AI system, capable of complex reasoning and reflection. Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags. If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags.", label="System message"),
118
+ gr.Slider(minimum=1, maximum=8192, value=2048, step=1, label="Max tokens"),
119
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
120
+ gr.Slider(
121
+ minimum=0.1,
122
+ maximum=1.0,
123
+ value=0.95,
124
+ step=0.05,
125
+ label="Top-p",
126
+ ),
127
+ gr.Slider(
128
+ minimum=0,
129
+ maximum=100,
130
+ value=40,
131
+ step=1,
132
+ label="Top-k",
133
+ ),
134
+ gr.Slider(
135
+ minimum=0.0,
136
+ maximum=2.0,
137
+ value=1.1,
138
+ step=0.1,
139
+ label="Repetition penalty",
140
+ ),
141
+ ],
142
+ theme=gr.themes.Soft(primary_hue="violet", secondary_hue="violet", neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(
143
+ body_background_fill_dark="#16141c",
144
+ block_background_fill_dark="#16141c",
145
+ block_border_width="1px",
146
+ block_title_background_fill_dark="#1e1c26",
147
+ input_background_fill_dark="#292733",
148
+ button_secondary_background_fill_dark="#24212b",
149
+ border_color_accent_dark="#343140",
150
+ border_color_primary_dark="#343140",
151
+ background_fill_secondary_dark="#16141c",
152
+ color_accent_soft_dark="transparent",
153
+ code_background_fill_dark="#292733",
154
+ ),
155
+ retry_btn="Retry",
156
+ undo_btn="Undo",
157
+ clear_btn="Clear",
158
+ submit_btn="Send",
159
+ title="Reflection Llama-3.1 70B",
160
+ description=description,
161
+ chatbot=gr.Chatbot(
162
+ scale=1,
163
+ likeable=False,
164
+ show_copy_button=True
165
+ )
166
+ )
167
+
168
+ if __name__ == "__main__":
169
+ demo.launch()