Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,11 +22,11 @@ def format_messages_for_model(messages):
|
|
| 22 |
formatted_messages += f"DeepSeek-R1: {msg['content']}\n"
|
| 23 |
return formatted_messages
|
| 24 |
|
| 25 |
-
def generate_response(user_input,
|
| 26 |
"""
|
| 27 |
Generate a response using DeepSeek-R1 and return it as a string.
|
| 28 |
"""
|
| 29 |
-
formatted_history = format_messages_for_model(
|
| 30 |
full_input = f"{formatted_history}User: {user_input}\nDeepSeek-R1: "
|
| 31 |
|
| 32 |
inputs = tokenizer(full_input, return_tensors="np")
|
|
@@ -56,27 +56,34 @@ def chatbot(
|
|
| 56 |
# Generate a unique session ID for this chat session
|
| 57 |
session_id = str(uuid.uuid4())
|
| 58 |
|
| 59 |
-
#
|
| 60 |
messages = []
|
| 61 |
if system_message:
|
| 62 |
messages.append({"role": "system", "content": system_message})
|
|
|
|
|
|
|
| 63 |
for user_msg, bot_msg in history:
|
| 64 |
-
|
| 65 |
-
messages.append({"role": "user", "content": user_msg})
|
| 66 |
if bot_msg:
|
| 67 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
# Generate response
|
| 70 |
response = generate_response(message, messages)
|
| 71 |
|
| 72 |
-
# Add to
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Save chat history to Hugging Face storage
|
| 76 |
storage = gr.HuggingFace()
|
| 77 |
-
storage.set(key=session_id, value={"messages": messages
|
| 78 |
|
| 79 |
-
return response
|
| 80 |
|
| 81 |
def create_chat_interface():
|
| 82 |
"""
|
|
|
|
| 22 |
formatted_messages += f"DeepSeek-R1: {msg['content']}\n"
|
| 23 |
return formatted_messages
|
| 24 |
|
| 25 |
+
def generate_response(user_input, messages):
|
| 26 |
"""
|
| 27 |
Generate a response using DeepSeek-R1 and return it as a string.
|
| 28 |
"""
|
| 29 |
+
formatted_history = format_messages_for_model(messages)
|
| 30 |
full_input = f"{formatted_history}User: {user_input}\nDeepSeek-R1: "
|
| 31 |
|
| 32 |
inputs = tokenizer(full_input, return_tensors="np")
|
|
|
|
| 56 |
# Generate a unique session ID for this chat session
|
| 57 |
session_id = str(uuid.uuid4())
|
| 58 |
|
| 59 |
+
# Initialize the messages with the system message
|
| 60 |
messages = []
|
| 61 |
if system_message:
|
| 62 |
messages.append({"role": "system", "content": system_message})
|
| 63 |
+
|
| 64 |
+
# Add previous chat history
|
| 65 |
for user_msg, bot_msg in history:
|
| 66 |
+
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 67 |
if bot_msg:
|
| 68 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 69 |
|
| 70 |
+
# Add the new user message
|
| 71 |
+
messages.append({"role": "user", "content": message})
|
| 72 |
+
|
| 73 |
# Generate response
|
| 74 |
response = generate_response(message, messages)
|
| 75 |
|
| 76 |
+
# Add the bot's response to messages
|
| 77 |
+
messages.append({"role": "assistant", "content": response})
|
| 78 |
+
|
| 79 |
+
# Update chat history in the interface
|
| 80 |
+
new_history = history + [(message, response)]
|
| 81 |
|
| 82 |
# Save chat history to Hugging Face storage
|
| 83 |
storage = gr.HuggingFace()
|
| 84 |
+
storage.set(key=session_id, value={"messages": messages})
|
| 85 |
|
| 86 |
+
return response, new_history
|
| 87 |
|
| 88 |
def create_chat_interface():
|
| 89 |
"""
|