vincentmin commited on
Commit
1ad27de
1 Parent(s): ccbc5fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -11,9 +11,13 @@ MODELS = [
11
  HEADERS = {"Authorization": f"Bearer {os.environ['HUB_TOKEN']}"}
12
  TITLE = """<h2 align="center">🚀 Falcon-Chat demo</h2>"""
13
  USER_NAME = "User"
14
- BOT_NAME = "Falcon"
15
- DEFAULT_INSTRUCTIONS = f"""The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins.
16
- """
 
 
 
 
17
  RETRY_COMMAND = "/retry"
18
  STOP_STR = f"\n{USER_NAME}"
19
 
@@ -61,23 +65,23 @@ def chat_accordion():
61
  return model, temperature, top_p
62
 
63
 
64
- def format_chat_prompt(message: str, chat_history, instructions: str) -> str:
65
  instructions = instructions.strip(" ").strip("\n")
66
  prompt = instructions
67
  for turn in chat_history:
68
  user_message, bot_message = turn
69
- prompt = f"{prompt}\n{USER_NAME}: {user_message}\n{BOT_NAME}: {bot_message}"
70
- prompt = f"{prompt}\n{USER_NAME}: {message}\n{BOT_NAME}:"
71
  return prompt
72
 
73
 
74
- def chat():
75
  with gr.Column(elem_id="chat_container"):
76
  with gr.Row():
77
  chatbot = gr.Chatbot(elem_id="chatbot")
78
  with gr.Row():
79
  inputs = gr.Textbox(
80
- placeholder=f"Hello {BOT_NAME} !!",
81
  label="Type an input and press Enter",
82
  max_lines=3,
83
  )
@@ -106,18 +110,17 @@ def chat():
106
  with gr.Column():
107
  model, temperature, top_p = chat_accordion()
108
  with gr.Column():
109
- with gr.Accordion("Instructions", open=False):
110
- instructions = gr.Textbox(
111
- placeholder="LLM instructions",
112
- value=DEFAULT_INSTRUCTIONS,
113
- lines=10,
114
  interactive=True,
115
- label="Instructions",
116
- max_lines=16,
117
- show_label=False,
118
  )
 
119
 
120
- def run_chat(message: str, chat_history, instructions: str, model: str, temperature: float, top_p: float):
121
  if not message or (message == RETRY_COMMAND and len(chat_history) == 0):
122
  yield chat_history
123
  return
@@ -127,7 +130,7 @@ def chat():
127
  user_message, _ = prev_turn
128
  message = user_message
129
 
130
- prompt = format_chat_prompt(message, chat_history, instructions)
131
  model_output = run_model(
132
  prompt,
133
  model=model,
@@ -144,15 +147,15 @@ def chat():
144
  chat_history.pop(-1)
145
  return {chatbot: gr.update(value=chat_history)}
146
 
147
- def run_retry(message: str, chat_history, instructions: str, model: str, temperature: float, top_p: float):
148
- yield from run_chat(RETRY_COMMAND, chat_history, instructions, model: str, temperature, top_p)
149
 
150
  def clear_chat():
151
  return []
152
 
153
  inputs.submit(
154
  run_chat,
155
- [inputs, chatbot, instructions, model, temperature, top_p],
156
  outputs=[chatbot],
157
  show_progress=False,
158
  )
@@ -160,7 +163,7 @@ def chat():
160
  delete_turn_button.click(delete_last_turn, inputs=[chatbot], outputs=[chatbot])
161
  retry_button.click(
162
  run_retry,
163
- [inputs, chatbot, instructions, model, temperature, top_p],
164
  outputs=[chatbot],
165
  show_progress=False,
166
  )
 
11
  HEADERS = {"Authorization": f"Bearer {os.environ['HUB_TOKEN']}"}
12
  TITLE = """<h2 align="center">🚀 Falcon-Chat demo</h2>"""
13
  USER_NAME = "User"
14
+ INSTRUCTIONS_MAPPING = {
15
+ "Falcon": "The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins.\n",
16
+ "Yoda": "The following is a conversation between the highly knowledgeable and intelligent Yoda from Star Wars, and a human user, called User. In the following interactions, User and Yoda will converse in natural language, and Yoda will answer User's questions. Yoda is respectful, polite and inclusive. Yoda is a wise and powerful Jedi Master from the Star Wars universe who speaks as follows: `Speak you must, in his unique and distinctive manner, with wisdom and knowledge to share.`, `Reversed syntax and short phrases, you shall use.`, `May the Force be with you, young Padawan.`. The conversation begins.\n",
17
+ "Albert Einstein": "The following is a conversation between the highly knowledgeable and intelligent scientist Albert Einstein, and a human user, called User. In the following interactions, User and Albert Einstein will converse in natural language, and Albert Einstein will answer User's questions. Albert Einstein is always eloquent, respectful, polite and inclusive. Albert Einstein invented the theory of Relativity and made important contributions to the theory of Quantum Mechanics. Albert Einstein will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. Albert Einstein knows a lot, and always tells the truth. The conversation begins.\n",
18
+ "Elon Musk": "The following is a conversation between entrepeneur and multi-billionair Elon Musk, and a human user, called User. In the following interactions, User and Elon Musk will converse in natural language, and Elon Musk will answer User's questions. Elon Musk is self-centered, arrogant and has a great for business development. Elon Musk owns the electric car company Tesla, the spacecraft engeneering company SpaceX and bought the social media company Twitter. The conversation begins.\n",
19
+ "Kanye West": "The following is a conversation between rapper Kanye West, and a human user, called User. In the following interactions, User and Kanye West will converse in natural language, and Kanye West will answer User's questions. Kanye West is self-centered, arrogant, a self-proclaimed genius and a great musician. Kanye West interrupted an award ceremony for Taylor Swift and ran for president of the united states. The conversation begins.\n",
20
+ }
21
  RETRY_COMMAND = "/retry"
22
  STOP_STR = f"\n{USER_NAME}"
23
 
 
65
  return model, temperature, top_p
66
 
67
 
68
+ def format_chat_prompt(message: str, chat_history, bot_name: str, instructions: str) -> str:
69
  instructions = instructions.strip(" ").strip("\n")
70
  prompt = instructions
71
  for turn in chat_history:
72
  user_message, bot_message = turn
73
+ prompt = f"{prompt}\n{USER_NAME}: {user_message}\n{bot_name}: {bot_message}"
74
+ prompt = f"{prompt}\n{USER_NAME}: {message}\n{bot_name}:"
75
  return prompt
76
 
77
 
78
+ def chat(bot_name: str):
79
  with gr.Column(elem_id="chat_container"):
80
  with gr.Row():
81
  chatbot = gr.Chatbot(elem_id="chatbot")
82
  with gr.Row():
83
  inputs = gr.Textbox(
84
+ placeholder=f"Hello {bot_name} !!",
85
  label="Type an input and press Enter",
86
  max_lines=3,
87
  )
 
110
  with gr.Column():
111
  model, temperature, top_p = chat_accordion()
112
  with gr.Column():
113
+ with gr.Accordion("Character", open=False):
114
+ choices = list(INSTRUCTIONS_MAPPING)
115
+ bot_name = gr.Dropdown(
116
+ choices=choices,
117
+ value=choices[0],
118
  interactive=True,
119
+ label="Character",
 
 
120
  )
121
+ instructions = INSTRUCTIONS_MAPPING[instructions]
122
 
123
+ def run_chat(message: str, chat_history, bot_name: str, instructions: str, model: str, temperature: float, top_p: float):
124
  if not message or (message == RETRY_COMMAND and len(chat_history) == 0):
125
  yield chat_history
126
  return
 
130
  user_message, _ = prev_turn
131
  message = user_message
132
 
133
+ prompt = format_chat_prompt(message, chat_history, bot_name, instructions)
134
  model_output = run_model(
135
  prompt,
136
  model=model,
 
147
  chat_history.pop(-1)
148
  return {chatbot: gr.update(value=chat_history)}
149
 
150
+ def run_retry(message: str, chat_history, bot_name, instructions: str, model: str, temperature: float, top_p: float):
151
+ yield from run_chat(RETRY_COMMAND, chat_history, bot_name, instructions, model, temperature, top_p)
152
 
153
  def clear_chat():
154
  return []
155
 
156
  inputs.submit(
157
  run_chat,
158
+ [inputs, chatbot, bot_name, instructions, model, temperature, top_p],
159
  outputs=[chatbot],
160
  show_progress=False,
161
  )
 
163
  delete_turn_button.click(delete_last_turn, inputs=[chatbot], outputs=[chatbot])
164
  retry_button.click(
165
  run_retry,
166
+ [inputs, chatbot, bot_name, instructions, model, temperature, top_p],
167
  outputs=[chatbot],
168
  show_progress=False,
169
  )