davidberenstein1957 HF staff commited on
Commit
6ea04d8
·
1 Parent(s): 13199e1

add language information to system prompt

Browse files
Files changed (1) hide show
  1. app/app.py +35 -0
app/app.py CHANGED
@@ -13,6 +13,8 @@ from gradio.components.chatbot import Option
13
  from huggingface_hub import InferenceClient
14
  from pandas import DataFrame
15
 
 
 
16
  client = InferenceClient(
17
  token=os.getenv("HF_TOKEN"),
18
  model=(
@@ -32,6 +34,20 @@ def add_user_message(history, message):
32
  return history, gr.MultimodalTextbox(value=None, interactive=False)
33
 
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def format_history_as_messages(history: list):
36
  messages = []
37
  current_role = None
@@ -267,10 +283,23 @@ with gr.Blocks(css=css) as demo:
267
  visible=False,
268
  )
269
 
 
 
 
 
 
 
 
270
  chatbot = gr.Chatbot(
271
  elem_id="chatbot",
272
  editable="all",
273
  bubble_full_width=False,
 
 
 
 
 
 
274
  type="messages",
275
  feedback_options=["Like", "Dislike"],
276
  )
@@ -293,6 +322,12 @@ with gr.Blocks(css=css) as demo:
293
  # Deal with feedback
294
  ##############################
295
 
 
 
 
 
 
 
296
  chat_input.submit(
297
  fn=add_user_message,
298
  inputs=[chatbot, chat_input],
 
13
  from huggingface_hub import InferenceClient
14
  from pandas import DataFrame
15
 
16
+ LANGUAGES = ["English", "Spanish", "Hebrew", "Dutch"]
17
+
18
  client = InferenceClient(
19
  token=os.getenv("HF_TOKEN"),
20
  model=(
 
34
  return history, gr.MultimodalTextbox(value=None, interactive=False)
35
 
36
 
37
+ def format_system_message(language: str, history: list):
38
+ if history:
39
+ if history[0]["role"] == "system":
40
+ history = history[1:]
41
+ system_message = [
42
+ {
43
+ "role": "system",
44
+ "content": f"You are a helpful assistant that speaks {language}.",
45
+ }
46
+ ]
47
+ history = system_message + history
48
+ return history
49
+
50
+
51
  def format_history_as_messages(history: list):
52
  messages = []
53
  current_role = None
 
283
  visible=False,
284
  )
285
 
286
+ language = gr.Dropdown(
287
+ label="Language",
288
+ choices=LANGUAGES,
289
+ # value="English",
290
+ interactive=True,
291
+ )
292
+
293
  chatbot = gr.Chatbot(
294
  elem_id="chatbot",
295
  editable="all",
296
  bubble_full_width=False,
297
+ value=[
298
+ {
299
+ "role": "system",
300
+ "content": "You are a helpful assistant that speaks English.",
301
+ }
302
+ ],
303
  type="messages",
304
  feedback_options=["Like", "Dislike"],
305
  )
 
322
  # Deal with feedback
323
  ##############################
324
 
325
+ language.change(
326
+ fn=format_system_message,
327
+ inputs=[language, chatbot],
328
+ outputs=[chatbot],
329
+ )
330
+
331
  chat_input.submit(
332
  fn=add_user_message,
333
  inputs=[chatbot, chat_input],