Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,27 +67,18 @@ def authorize(user, api_key, system_message):
|
|
| 67 |
|
| 68 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
| 69 |
session = sessions.get(api_key, {})
|
| 70 |
-
history = session.get("history", [])
|
| 71 |
headers = session.get("headers", {})
|
| 72 |
system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if assistant_message:
|
| 84 |
-
messages.append({
|
| 85 |
-
"role": "assistant",
|
| 86 |
-
"content": assistant_message,
|
| 87 |
-
"profile": assistant_profile,
|
| 88 |
-
"picture": assistant_pic
|
| 89 |
-
})
|
| 90 |
-
|
| 91 |
data = {
|
| 92 |
"preferences": {
|
| 93 |
"max_char": max_tokens,
|
|
@@ -97,36 +88,28 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 97 |
},
|
| 98 |
"conversation_history": messages,
|
| 99 |
"input": message,
|
| 100 |
-
"stream": True
|
| 101 |
}
|
| 102 |
-
assistant_reply = ""
|
| 103 |
|
| 104 |
-
|
| 105 |
try:
|
| 106 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
|
| 107 |
-
|
| 108 |
if response.status_code == 200:
|
| 109 |
for line in response.iter_lines(decode_unicode=True):
|
| 110 |
if line.strip():
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
yield assistant_reply
|
| 124 |
-
except json.JSONDecodeError as e:
|
| 125 |
-
print(f"Stream chunk error: {e} with line: {line}")
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))
|
| 129 |
-
sessions[api_key]["history"] = history
|
| 130 |
else:
|
| 131 |
yield f"Error: {response.status_code} - {response.text}"
|
| 132 |
except Exception as e:
|
|
@@ -140,11 +123,10 @@ def render_message(history):
|
|
| 140 |
<div id="chatbox-container" class="chatbox" style="height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;">
|
| 141 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
| 142 |
|
|
|
|
| 143 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 144 |
-
if user_message:
|
| 145 |
-
|
| 146 |
-
user_message, extensions=["fenced_code", CodeHiliteExtension()]
|
| 147 |
-
)
|
| 148 |
user_message_html = markdown.markdown(escape_html(user_message), extensions=["fenced_code", "codehilite"])
|
| 149 |
messages_html += f"""
|
| 150 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
|
@@ -152,13 +134,15 @@ def render_message(history):
|
|
| 152 |
<span style='color: white;'>{user_message_html}</span>
|
| 153 |
</div>"""
|
| 154 |
|
| 155 |
-
if assistant_message:
|
|
|
|
| 156 |
messages_html += f"""
|
| 157 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
| 158 |
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
| 159 |
-
<span style='color: white;'>{assistant_message}</span>
|
| 160 |
</div>"""
|
| 161 |
|
|
|
|
| 162 |
return messages_html
|
| 163 |
|
| 164 |
def escape_html(unsafe_text):
|
|
|
|
| 67 |
|
| 68 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
| 69 |
session = sessions.get(api_key, {})
|
|
|
|
| 70 |
headers = session.get("headers", {})
|
| 71 |
system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS)
|
| 72 |
+
|
| 73 |
+
messages = [
|
| 74 |
+
{"role": "system", "content": system_message},
|
| 75 |
+
*[
|
| 76 |
+
{"role": "user", "content": user_msg} if user_msg else {"role": "assistant", "content": assistant_msg}
|
| 77 |
+
for user_msg, assistant_msg, _, _, _, _ in session.get("history", [])
|
| 78 |
+
],
|
| 79 |
+
{"role": "user", "content": message}
|
| 80 |
+
]
|
| 81 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
data = {
|
| 83 |
"preferences": {
|
| 84 |
"max_char": max_tokens,
|
|
|
|
| 88 |
},
|
| 89 |
"conversation_history": messages,
|
| 90 |
"input": message,
|
| 91 |
+
"stream": True,
|
| 92 |
}
|
|
|
|
| 93 |
|
| 94 |
+
assistant_reply = ""
|
| 95 |
try:
|
| 96 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
|
| 97 |
+
|
| 98 |
if response.status_code == 200:
|
| 99 |
for line in response.iter_lines(decode_unicode=True):
|
| 100 |
if line.strip():
|
| 101 |
+
try:
|
| 102 |
+
if line.startswith("data:"):
|
| 103 |
+
line = line[5:].strip()
|
| 104 |
+
if line:
|
| 105 |
+
chunk = json.loads(line)
|
| 106 |
+
chunk_message = chunk.get("chunk", {}).get("content", "")
|
| 107 |
+
assistant_reply += chunk_message
|
| 108 |
+
yield assistant_reply
|
| 109 |
+
except json.JSONDecodeError:
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
+
session["history"].append((message, assistant_reply, "You", "P-ALPLE", session["avatar"], ASSISTANT_PIC_PATH))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
yield f"Error: {response.status_code} - {response.text}"
|
| 115 |
except Exception as e:
|
|
|
|
| 123 |
<div id="chatbox-container" class="chatbox" style="height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;">
|
| 124 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
| 125 |
|
| 126 |
+
seen_messages = set() # Track rendered messages to avoid duplication
|
| 127 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 128 |
+
if user_message and user_message not in seen_messages:
|
| 129 |
+
seen_messages.add(user_message)
|
|
|
|
|
|
|
| 130 |
user_message_html = markdown.markdown(escape_html(user_message), extensions=["fenced_code", "codehilite"])
|
| 131 |
messages_html += f"""
|
| 132 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
|
|
|
| 134 |
<span style='color: white;'>{user_message_html}</span>
|
| 135 |
</div>"""
|
| 136 |
|
| 137 |
+
if assistant_message and assistant_message not in seen_messages:
|
| 138 |
+
seen_messages.add(assistant_message)
|
| 139 |
messages_html += f"""
|
| 140 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
| 141 |
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
| 142 |
+
<span style='color: white;'>{escape_html(assistant_message)}</span>
|
| 143 |
</div>"""
|
| 144 |
|
| 145 |
+
messages_html += "</div></div>"
|
| 146 |
return messages_html
|
| 147 |
|
| 148 |
def escape_html(unsafe_text):
|