grimjim commited on
Commit
ec501f5
·
1 Parent(s): 4858c8d

UI fixes/workaround

Browse files
Files changed (1) hide show
  1. app.py +62 -27
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  from functools import lru_cache
3
- from typing import Any, Iterator
4
 
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download
@@ -128,7 +128,7 @@ def build_messages(
128
  return messages
129
 
130
 
131
- def respond(
132
  message: str,
133
  history: list[Any],
134
  system_message: str,
@@ -136,7 +136,16 @@ def respond(
136
  temperature: float,
137
  top_p: float,
138
  repeat_penalty: float,
139
- ) -> Iterator[str]:
 
 
 
 
 
 
 
 
 
140
  llm = get_llm()
141
  messages = build_messages(message, history, system_message or DEFAULT_SYSTEM_PROMPT)
142
  output = ""
@@ -155,54 +164,80 @@ def respond(
155
  token = delta.get("content") or ""
156
  if token:
157
  output += token
158
- yield output
 
 
159
 
 
 
 
 
 
 
 
 
160
 
161
- chatbot = gr.ChatInterface(
162
- respond,
163
- type="messages",
164
- title="Dreadzone",
165
- description="CPU-only local chat with NVIDIA Nemotron 3 Nano 4B GGUF.",
166
- chatbot=gr.Chatbot(
167
  type="messages",
168
  height=420,
169
  show_copy_button=True,
170
  container=True,
171
- ),
172
- additional_inputs=[
173
- gr.Textbox(
 
 
 
 
 
 
 
 
 
 
 
174
  value=DEFAULT_SYSTEM_PROMPT,
175
  label="System message",
176
  lines=3,
177
- ),
178
- gr.Slider(
179
  minimum=32,
180
  maximum=1024,
181
  value=384,
182
  step=16,
183
  label="Max new tokens",
184
- ),
185
- gr.Slider(
186
  minimum=0.0,
187
  maximum=1.5,
188
  value=0.7,
189
  step=0.05,
190
  label="Temperature",
191
- ),
192
- gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
193
- gr.Slider(
194
  minimum=1.0,
195
  maximum=1.3,
196
  value=1.1,
197
  step=0.01,
198
  label="Repeat penalty",
199
- ),
200
- ],
201
- additional_inputs_accordion=gr.Accordion("Generation settings", open=False),
202
- css=CSS,
203
- theme=gr.themes.Soft(),
204
- )
 
 
 
 
 
 
 
 
 
 
205
 
206
 
207
  if __name__ == "__main__":
208
- chatbot.queue(max_size=8).launch(ssr_mode=False)
 
1
  import os
2
  from functools import lru_cache
3
+ from typing import Any
4
 
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download
 
128
  return messages
129
 
130
 
131
+ def generate_reply(
132
  message: str,
133
  history: list[Any],
134
  system_message: str,
 
136
  temperature: float,
137
  top_p: float,
138
  repeat_penalty: float,
139
+ ) -> Any:
140
+ if not message.strip():
141
+ yield history, ""
142
+ return
143
+
144
+ visible_history = list(history or [])
145
+ visible_history.append({"role": "user", "content": message})
146
+ visible_history.append({"role": "assistant", "content": ""})
147
+ yield visible_history, ""
148
+
149
  llm = get_llm()
150
  messages = build_messages(message, history, system_message or DEFAULT_SYSTEM_PROMPT)
151
  output = ""
 
164
  token = delta.get("content") or ""
165
  if token:
166
  output += token
167
+ visible_history[-1] = {"role": "assistant", "content": output}
168
+ yield visible_history, ""
169
+
170
 
171
+ def clear_chat() -> tuple[list[dict[str, str]], str]:
172
+ return [], ""
173
+
174
+
175
+ with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
176
+ gr.Markdown(
177
+ "## Dreadzone\nCPU-only local chat with NVIDIA Nemotron 3 Nano 4B GGUF."
178
+ )
179
 
180
+ chat = gr.Chatbot(
 
 
 
 
 
181
  type="messages",
182
  height=420,
183
  show_copy_button=True,
184
  container=True,
185
+ label="Chat",
186
+ )
187
+
188
+ with gr.Row():
189
+ message = gr.Textbox(
190
+ placeholder="Type a message...",
191
+ show_label=False,
192
+ scale=8,
193
+ container=False,
194
+ )
195
+ submit = gr.Button("Send", variant="primary", scale=1)
196
+
197
+ with gr.Accordion("Generation settings", open=False):
198
+ system_message = gr.Textbox(
199
  value=DEFAULT_SYSTEM_PROMPT,
200
  label="System message",
201
  lines=3,
202
+ )
203
+ max_tokens = gr.Slider(
204
  minimum=32,
205
  maximum=1024,
206
  value=384,
207
  step=16,
208
  label="Max new tokens",
209
+ )
210
+ temperature = gr.Slider(
211
  minimum=0.0,
212
  maximum=1.5,
213
  value=0.7,
214
  step=0.05,
215
  label="Temperature",
216
+ )
217
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p")
218
+ repeat_penalty = gr.Slider(
219
  minimum=1.0,
220
  maximum=1.3,
221
  value=1.1,
222
  step=0.01,
223
  label="Repeat penalty",
224
+ )
225
+
226
+ clear = gr.Button("Clear")
227
+
228
+ inputs = [
229
+ message,
230
+ chat,
231
+ system_message,
232
+ max_tokens,
233
+ temperature,
234
+ top_p,
235
+ repeat_penalty,
236
+ ]
237
+ submit.click(generate_reply, inputs=inputs, outputs=[chat, message])
238
+ message.submit(generate_reply, inputs=inputs, outputs=[chat, message])
239
+ clear.click(clear_chat, outputs=[chat, message])
240
 
241
 
242
  if __name__ == "__main__":
243
+ demo.queue(max_size=8).launch(ssr_mode=False)