Spaces:
Build error
Build error
Update app.py
Browse files66 unmodified lines6768697071727374757677787980818283848586878889909192933 unmodified lines9798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311327 unmodified lines1401411421431441451461471481491501 unmodified line15215315415515615715815916016116216316466 unmodified linesdef 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})data = {"preferences": {"max_char": max_tokens,3 unmodified lines},"conversation_history": messages,"input": message,"stream": True}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_messagesessions[api_key]["history"].append((message, assistant_reply, "You", "P-ALPLE", session["avatar"], ASSISTANT_PIC_PATH))yield assistant_replyexcept 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"] = historyelse: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;">"""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()])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: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></div>"""return messages_htmldef escape_html(unsafe_text):66 unmodified lines6768697071727374757677787980818283843 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141157 unmodified lines1231241251261271281291301311321 unmodified line13413513613713813914014114214314414514614714866 unmodified linesdef respond(message, api_key, max_tokens, top_p, temperature):session = sessions.get(api_key, {})headers = session.get("headers", {})system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS)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,}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_messageyield assistant_replyexcept json.JSONDecodeError:passsession["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 duplicationfor user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history: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 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;'>{escape_html(assistant_message)}</span></div>"""messages_html += "</div></div>"return messages_htmldef escape_html(unsafe_text):
app.py+32-48