Spaces:
Build error
Build error
Update app.py
Browse files66 unmodified lines676869707071737475767778798081828384858687888990727374757677787980818283843 unmodified lines8889901009192102931049495961079798991001111121131141151161171181191201211221231241251261271281291011021031041051061071081091101111121131141157 unmodified lines1231241251261271441451461471281291301311321 unmodified line13413513615513713813914014115914214314414514614714866 unmodified lines
def respond(message, api_key, max_tokens, top_p, temperature): session = sessions.get(api_key, {}) history = session.get("history", []) headers = session.get("headers", {}) system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS) messages = []
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history: if user_message: messages.append({ "role": "user", "content": user_message, "profile": user_profile, "picture": user_pic }) if assistant_message: messages.append({ "role": "assistant", "content": assistant_message, "profile": assistant_profile, "picture": assistant_pic })
messages = [ {"role": "system", "content": system_message}, *[ {"role": "user", "content": user_msg} if user_msg else {"role": "assistant", "content": assistant_msg} for user_msg, assistant_msg, _, _, _, _ in session.get("history", []) ], {"role": "user", "content": message} ] data = { "preferences": { "max_char": max_tokens,3 unmodified lines }, "conversation_history": messages, "input": message, "stream": True "stream": True, } assistant_reply = ""
assistant_reply = "" try: response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
if response.status_code == 200: for line in response.iter_lines(decode_unicode=True): if line.strip(): try: if line.startswith("data:"): line = line[5:].strip() if line: chunk = json.loads(line) chunk_message = chunk.get("chunk", {}).get("content", "") assistant_reply += chunk_message sessions[api_key]["history"].append((message, assistant_reply, "You", "P-ALPLE", session["avatar"], ASSISTANT_PIC_PATH))
yield assistant_reply except json.JSONDecodeError as e: print(f"Stream chunk error: {e} with line: {line}")
history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH)) sessions[api_key]["history"] = history try: if line.startswith("data:"): line = line[5:].strip() if line: chunk = json.loads(line) chunk_message = chunk.get("chunk", {}).get("content", "") assistant_reply += chunk_message yield assistant_reply except json.JSONDecodeError: pass
session["history"].append((message, assistant_reply, "You", "P-ALPLE", session["avatar"], ASSISTANT_PIC_PATH)) else: yield f"Error: {response.status_code} - {response.text}" except Exception as e:7 unmodified lines <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;"> <div id="messages" style="display: block; margin-bottom: 10px;">"""
seen_messages = set() # Track rendered messages to avoid duplication for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history: if user_message: user_message_html = markdown.markdown( user_message, extensions=["fenced_code", CodeHiliteExtension()] ) if user_message and user_message not in seen_messages: seen_messages.add(user_message) user_message_html = markdown.markdown(escape_html(user_message), extensions=["fenced_code", "codehilite"]) messages_html += f""" <div style='display: flex; align-items: center; margin-bottom: 10px;'>1 unmodified line <span style='color: white;'>{user_message_html}</span> </div>"""
if assistant_message: if assistant_message and assistant_message not in seen_messages: seen_messages.add(assistant_message) messages_html += f""" <div style='display: flex; align-items: center; margin-bottom: 10px;'> <img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'> <span style='color: white;'>{assistant_message}</span> <span style='color: white;'>{escape_html(assistant_message)}</span> </div>"""
messages_html += "</div></div>" return messages_html
def escape_html(unsafe_text):app.py+32-48