akhaliq HF Staff commited on
Commit
d142097
Β·
1 Parent(s): 41e409d

add model change button

Browse files
Files changed (1) hide show
  1. app.py +36 -4
app.py CHANGED
@@ -24,6 +24,20 @@ Always respond with code that can be executed or rendered directly.
24
 
25
  Always output only the HTML code inside a ```html ... ``` code block, and do not include any explanations or extra text."""
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  DEMO_LIST = [
28
  {
29
  "title": "Todo App",
@@ -60,7 +74,7 @@ DEMO_LIST = [
60
  ]
61
 
62
  # HF Inference Client
63
- YOUR_API_TOKEN = os.getenv('HF_TOKEN3')
64
  client = InferenceClient(
65
  provider="auto",
66
  api_key=YOUR_API_TOKEN,
@@ -175,6 +189,7 @@ with gr.Blocks(css_paths="app.css") as demo:
175
  setting = gr.State({
176
  "system": SystemPrompt,
177
  })
 
178
 
179
  with ms.Application() as app:
180
  with antd.ConfigProvider():
@@ -189,6 +204,7 @@ with gr.Blocks(css_paths="app.css") as demo:
189
  <h1>Hugging Face Coder</h1>
190
  </div>
191
  """)
 
192
  input = antd.InputTextarea(
193
  size="large", allow_clear=True, placeholder="Please enter what kind of application you want", visible=False)
194
  btn = antd.Button("send", type="primary", size="large", visible=False)
@@ -205,6 +221,7 @@ with gr.Blocks(css_paths="app.css") as demo:
205
  with antd.Flex(gap="small", wrap=True, visible=False) as setting_flex:
206
  settingPromptBtn = antd.Button(
207
  "βš™οΈ set system Prompt", type="default", visible=False)
 
208
  codeBtn = antd.Button("πŸ§‘β€πŸ’» view code", type="default", visible=False)
209
  historyBtn = antd.Button("πŸ“œ history", type="default", visible=False)
210
 
@@ -219,6 +236,15 @@ with gr.Blocks(css_paths="app.css") as demo:
219
  system_prompt_modal.cancel(lambda: gr.update(
220
  open=False), outputs=[system_prompt_modal])
221
 
 
 
 
 
 
 
 
 
 
222
  with antd.Drawer(open=False, title="code", placement="left", width="750px") as code_drawer:
223
  code_output = legacy.Markdown()
224
 
@@ -257,6 +283,8 @@ with gr.Blocks(css_paths="app.css") as demo:
257
  gr.update(visible=False),
258
  gr.update(visible=False),
259
  gr.update(visible=False),
 
 
260
  )
261
  else:
262
  return (
@@ -269,9 +297,11 @@ with gr.Blocks(css_paths="app.css") as demo:
269
  gr.update(visible=True),
270
  gr.update(visible=True),
271
  gr.update(visible=True),
 
 
272
  )
273
 
274
- def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History], profile: gr.OAuthProfile | None):
275
  if profile is None:
276
  return (
277
  "Please sign in with Hugging Face to use this feature.",
@@ -289,7 +319,7 @@ with gr.Blocks(css_paths="app.css") as demo:
289
 
290
  try:
291
  completion = client.chat.completions.create(
292
- model="deepseek-ai/DeepSeek-V3-0324",
293
  messages=messages,
294
  stream=True
295
  )
@@ -328,7 +358,7 @@ with gr.Blocks(css_paths="app.css") as demo:
328
 
329
  btn.click(
330
  generation_code,
331
- inputs=[input, setting, history],
332
  outputs=[code_output, history, sandbox, state_tab, code_drawer]
333
  )
334
 
@@ -340,11 +370,13 @@ with gr.Blocks(css_paths="app.css") as demo:
340
  outputs=[
341
  login_message,
342
  input,
 
343
  btn,
344
  clear_btn,
345
  examples_flex,
346
  setting_flex,
347
  settingPromptBtn,
 
348
  codeBtn,
349
  historyBtn,
350
  ]
 
24
 
25
  Always output only the HTML code inside a ```html ... ``` code block, and do not include any explanations or extra text."""
26
 
27
+ # Available models
28
+ AVAILABLE_MODELS = [
29
+ {
30
+ "name": "DeepSeek V3",
31
+ "id": "deepseek-ai/DeepSeek-V3-0324",
32
+ "description": "DeepSeek V3 model for code generation"
33
+ },
34
+ {
35
+ "name": "DeepSeek R1",
36
+ "id": "deepseek-ai/DeepSeek-R1-0528",
37
+ "description": "DeepSeek R1 model for code generation"
38
+ }
39
+ ]
40
+
41
  DEMO_LIST = [
42
  {
43
  "title": "Todo App",
 
74
  ]
75
 
76
  # HF Inference Client
77
+ YOUR_API_TOKEN = os.getenv('HF_TOKEN')
78
  client = InferenceClient(
79
  provider="auto",
80
  api_key=YOUR_API_TOKEN,
 
189
  setting = gr.State({
190
  "system": SystemPrompt,
191
  })
192
+ current_model = gr.State(AVAILABLE_MODELS[0]) # Default to first model
193
 
194
  with ms.Application() as app:
195
  with antd.ConfigProvider():
 
204
  <h1>Hugging Face Coder</h1>
205
  </div>
206
  """)
207
+ current_model_display = gr.Markdown("**Current Model:** DeepSeek V3", visible=False)
208
  input = antd.InputTextarea(
209
  size="large", allow_clear=True, placeholder="Please enter what kind of application you want", visible=False)
210
  btn = antd.Button("send", type="primary", size="large", visible=False)
 
221
  with antd.Flex(gap="small", wrap=True, visible=False) as setting_flex:
222
  settingPromptBtn = antd.Button(
223
  "βš™οΈ set system Prompt", type="default", visible=False)
224
+ modelBtn = antd.Button("πŸ€– switch model", type="default", visible=False)
225
  codeBtn = antd.Button("πŸ§‘β€πŸ’» view code", type="default", visible=False)
226
  historyBtn = antd.Button("πŸ“œ history", type="default", visible=False)
227
 
 
236
  system_prompt_modal.cancel(lambda: gr.update(
237
  open=False), outputs=[system_prompt_modal])
238
 
239
+ with antd.Modal(open=False, title="Select Model", width="600px") as model_modal:
240
+ with antd.Flex(vertical=True, gap="middle"):
241
+ for i, model in enumerate(AVAILABLE_MODELS):
242
+ with antd.Card(hoverable=True, title=model["name"]) as modelCard:
243
+ antd.CardMeta(description=model["description"])
244
+ modelCard.click(lambda m=model: (m, gr.update(open=False), f"**Current Model:** {m['name']}"), outputs=[current_model, model_modal, current_model_display])
245
+
246
+ modelBtn.click(lambda: gr.update(open=True), inputs=[], outputs=[model_modal])
247
+
248
  with antd.Drawer(open=False, title="code", placement="left", width="750px") as code_drawer:
249
  code_output = legacy.Markdown()
250
 
 
283
  gr.update(visible=False),
284
  gr.update(visible=False),
285
  gr.update(visible=False),
286
+ gr.update(visible=False),
287
+ gr.update(visible=False),
288
  )
289
  else:
290
  return (
 
297
  gr.update(visible=True),
298
  gr.update(visible=True),
299
  gr.update(visible=True),
300
+ gr.update(visible=True),
301
+ gr.update(visible=True),
302
  )
303
 
304
+ def generation_code(query: Optional[str], _setting: Dict[str, str], _history: Optional[History], profile: gr.OAuthProfile | None, _current_model: Dict):
305
  if profile is None:
306
  return (
307
  "Please sign in with Hugging Face to use this feature.",
 
319
 
320
  try:
321
  completion = client.chat.completions.create(
322
+ model=_current_model["id"],
323
  messages=messages,
324
  stream=True
325
  )
 
358
 
359
  btn.click(
360
  generation_code,
361
+ inputs=[input, setting, history, current_model],
362
  outputs=[code_output, history, sandbox, state_tab, code_drawer]
363
  )
364
 
 
370
  outputs=[
371
  login_message,
372
  input,
373
+ current_model_display,
374
  btn,
375
  clear_btn,
376
  examples_flex,
377
  setting_flex,
378
  settingPromptBtn,
379
+ modelBtn,
380
  codeBtn,
381
  historyBtn,
382
  ]