Spaces:
Runtime error
Runtime error
michailroussos
commited on
Commit
·
1188d49
1
Parent(s):
584beb9
more
Browse files
app.py
CHANGED
|
@@ -16,19 +16,19 @@ FastLanguageModel.for_inference(model) # Enable optimized inference
|
|
| 16 |
|
| 17 |
# Define the response function
|
| 18 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 19 |
-
#
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
|
| 22 |
-
# Append the
|
| 23 |
if history:
|
| 24 |
for entry in history:
|
| 25 |
messages.append({"role": "user", "content": entry["user"]})
|
| 26 |
messages.append({"role": "assistant", "content": entry["assistant"]})
|
| 27 |
|
| 28 |
-
# Add the user's new
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
-
# Tokenize
|
| 32 |
inputs = tokenizer.apply_chat_template(
|
| 33 |
messages,
|
| 34 |
tokenize=True,
|
|
@@ -48,20 +48,21 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 48 |
)
|
| 49 |
response = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
print("Raw Assistant Response:", response)
|
| 53 |
-
|
| 54 |
-
# Clean up the assistant response (strip out unwanted system info)
|
| 55 |
response = response.replace("Cutting Knowledge Date", "").replace("You are a helpful assistant.", "").strip()
|
| 56 |
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
| 58 |
if history is None:
|
| 59 |
history = []
|
| 60 |
history.append({"user": message, "assistant": response})
|
| 61 |
|
|
|
|
| 62 |
print("Updated History:", history)
|
| 63 |
|
| 64 |
-
# Format history
|
| 65 |
formatted_history = []
|
| 66 |
for entry in history:
|
| 67 |
formatted_history.append({"role": "user", "content": entry["user"]})
|
|
|
|
| 16 |
|
| 17 |
# Define the response function
|
| 18 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 19 |
+
# Prepare the messages, separating the system message from user/assistant pairs
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
|
| 22 |
+
# Append the conversation history (user-assistant pairs)
|
| 23 |
if history:
|
| 24 |
for entry in history:
|
| 25 |
messages.append({"role": "user", "content": entry["user"]})
|
| 26 |
messages.append({"role": "assistant", "content": entry["assistant"]})
|
| 27 |
|
| 28 |
+
# Add the user's new message to the list of messages
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
+
# Tokenize the input
|
| 32 |
inputs = tokenizer.apply_chat_template(
|
| 33 |
messages,
|
| 34 |
tokenize=True,
|
|
|
|
| 48 |
)
|
| 49 |
response = tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
|
| 50 |
|
| 51 |
+
# Clean the response to ensure no system messages are included
|
|
|
|
|
|
|
|
|
|
| 52 |
response = response.replace("Cutting Knowledge Date", "").replace("You are a helpful assistant.", "").strip()
|
| 53 |
|
| 54 |
+
# Debug: Print the raw and cleaned assistant response
|
| 55 |
+
print("Raw Assistant Response:", response)
|
| 56 |
+
|
| 57 |
+
# Update the conversation history with the new user-assistant interaction
|
| 58 |
if history is None:
|
| 59 |
history = []
|
| 60 |
history.append({"user": message, "assistant": response})
|
| 61 |
|
| 62 |
+
# Debug: Print updated history
|
| 63 |
print("Updated History:", history)
|
| 64 |
|
| 65 |
+
# Format the history into the structure expected by Gradio
|
| 66 |
formatted_history = []
|
| 67 |
for entry in history:
|
| 68 |
formatted_history.append({"role": "user", "content": entry["user"]})
|