Update app.py
Browse files
app.py
CHANGED
|
@@ -82,12 +82,10 @@ def count_tokens(text: str) -> int:
|
|
| 82 |
|
| 83 |
def truncate_history(history: list[tuple[str, str]], system_message: str, max_length: int) -> list[tuple[str, str]]:
|
| 84 |
"""Truncates the conversation history to fit within the maximum token limit.
|
| 85 |
-
|
| 86 |
Args:
|
| 87 |
history: The conversation history (list of user/assistant tuples).
|
| 88 |
system_message: The system message.
|
| 89 |
max_length: The maximum number of tokens allowed.
|
| 90 |
-
|
| 91 |
Returns:
|
| 92 |
The truncated history.
|
| 93 |
"""
|
|
@@ -102,7 +100,7 @@ def truncate_history(history: list[tuple[str, str]], system_message: str, max_le
|
|
| 102 |
turn_tokens = user_tokens + assistant_tokens
|
| 103 |
|
| 104 |
if current_length + turn_tokens <= max_length:
|
| 105 |
-
truncated_history.insert(0, (user_msg, assistant_msg))
|
| 106 |
current_length += turn_tokens
|
| 107 |
else:
|
| 108 |
break # Stop adding turns if we exceed the limit
|
|
@@ -112,61 +110,61 @@ def truncate_history(history: list[tuple[str, str]], system_message: str, max_le
|
|
| 112 |
def respond(
|
| 113 |
message,
|
| 114 |
history: list[tuple[str, str]],
|
| 115 |
-
system_message,
|
| 116 |
max_tokens,
|
| 117 |
temperature,
|
| 118 |
top_p,
|
|
|
|
| 119 |
):
|
| 120 |
"""Responds to a user message, maintaining conversation history, using special tokens and message list."""
|
| 121 |
-
|
| 122 |
-
if message.lower() == "clear memory"
|
| 123 |
return "", [] # Return empty message and empty history to reset the chat
|
| 124 |
|
| 125 |
-
formatted_system_message = system_message
|
| 126 |
-
truncated_history = truncate_history(history, formatted_system_message, MAX_CONTEXT_LENGTH - max_tokens - 100)
|
| 127 |
|
| 128 |
-
messages = [{"role": "system", "content": formatted_system_message}]
|
| 129 |
for user_msg, assistant_msg in truncated_history:
|
| 130 |
if user_msg:
|
| 131 |
-
messages.append({"role": "user", "content": f"<|user|>\n{user_msg}</s>"})
|
| 132 |
if assistant_msg:
|
| 133 |
-
messages.append({"role": "assistant", "content": f"<|assistant|>\n{assistant_msg}</s>"})
|
| 134 |
|
| 135 |
-
messages.append({"role": "user", "content": f"<|user|>\n{message}</s>"})
|
| 136 |
|
| 137 |
response = ""
|
| 138 |
try:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
except Exception as e:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
# --- Gradio Interface ---
|
| 154 |
demo = gr.ChatInterface(
|
| 155 |
respond,
|
| 156 |
additional_inputs=[
|
| 157 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 159 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 160 |
-
gr.Slider(
|
| 161 |
-
|
| 162 |
-
maximum=1.0,
|
| 163 |
-
value=0.95,
|
| 164 |
-
step=0.05,
|
| 165 |
-
label="Top-p (nucleus sampling)",
|
| 166 |
-
),
|
| 167 |
-
gr.Button("Clear Memory"), # Keep the Clear Memory Button (as text command trigger)
|
| 168 |
],
|
| 169 |
)
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
-
demo.launch()
|
|
|
|
| 82 |
|
| 83 |
def truncate_history(history: list[tuple[str, str]], system_message: str, max_length: int) -> list[tuple[str, str]]:
|
| 84 |
"""Truncates the conversation history to fit within the maximum token limit.
|
|
|
|
| 85 |
Args:
|
| 86 |
history: The conversation history (list of user/assistant tuples).
|
| 87 |
system_message: The system message.
|
| 88 |
max_length: The maximum number of tokens allowed.
|
|
|
|
| 89 |
Returns:
|
| 90 |
The truncated history.
|
| 91 |
"""
|
|
|
|
| 100 |
turn_tokens = user_tokens + assistant_tokens
|
| 101 |
|
| 102 |
if current_length + turn_tokens <= max_length:
|
| 103 |
+
truncated_history.insert(0, (user_msg, assistant_msg))
|
| 104 |
current_length += turn_tokens
|
| 105 |
else:
|
| 106 |
break # Stop adding turns if we exceed the limit
|
|
|
|
| 110 |
def respond(
|
| 111 |
message,
|
| 112 |
history: list[tuple[str, str]],
|
| 113 |
+
system_message, # System message is now an argument
|
| 114 |
max_tokens,
|
| 115 |
temperature,
|
| 116 |
top_p,
|
| 117 |
+
clear_memory # Added extra parameter to match the 7 inputs provided
|
| 118 |
):
|
| 119 |
"""Responds to a user message, maintaining conversation history, using special tokens and message list."""
|
| 120 |
+
# Check for the clear memory command (or if the Clear Memory button is triggered)
|
| 121 |
+
if message.lower() == "clear memory" or clear_memory:
|
| 122 |
return "", [] # Return empty message and empty history to reset the chat
|
| 123 |
|
| 124 |
+
formatted_system_message = system_message # Use the system_message argument
|
| 125 |
+
truncated_history = truncate_history(history, formatted_system_message, MAX_CONTEXT_LENGTH - max_tokens - 100)
|
| 126 |
|
| 127 |
+
messages = [{"role": "system", "content": formatted_system_message}]
|
| 128 |
for user_msg, assistant_msg in truncated_history:
|
| 129 |
if user_msg:
|
| 130 |
+
messages.append({"role": "user", "content": f"<|user|>\n{user_msg}</s>"})
|
| 131 |
if assistant_msg:
|
| 132 |
+
messages.append({"role": "assistant", "content": f"<|assistant|>\n{assistant_msg}</s>"})
|
| 133 |
|
| 134 |
+
messages.append({"role": "user", "content": f"<|user|>\n{message}</s>"})
|
| 135 |
|
| 136 |
response = ""
|
| 137 |
try:
|
| 138 |
+
for chunk in client.chat_completion(
|
| 139 |
+
messages,
|
| 140 |
+
max_tokens=max_tokens,
|
| 141 |
+
stream=True,
|
| 142 |
+
temperature=temperature,
|
| 143 |
+
top_p=top_p,
|
| 144 |
+
):
|
| 145 |
+
token = chunk.choices[0].delta.content
|
| 146 |
+
response += token
|
| 147 |
+
yield response
|
| 148 |
except Exception as e:
|
| 149 |
+
print(f"An error occurred: {e}")
|
| 150 |
+
yield "I'm sorry, I encountered an error. Please try again."
|
| 151 |
|
| 152 |
# --- Gradio Interface ---
|
| 153 |
demo = gr.ChatInterface(
|
| 154 |
respond,
|
| 155 |
additional_inputs=[
|
| 156 |
+
gr.Textbox(
|
| 157 |
+
value=default_nvc_prompt_template,
|
| 158 |
+
label="System message",
|
| 159 |
+
visible=True,
|
| 160 |
+
lines=10 # Increased height for more space to read the prompt
|
| 161 |
+
),
|
| 162 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 163 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 164 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 165 |
+
gr.Button("Clear Memory"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
],
|
| 167 |
)
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
+
demo.launch(share=True)
|