pdfchat / app.py
ogegadavis254's picture
Update app.py
6a9d8b2 verified
raw
history blame
5.78 kB
import streamlit as st
import os
import requests
import json
from streamlit_chat import message
# Initialize a global variable to store the entire assistant response
entire_assistant_response = ""
def get_streamed_response(message, history):
all_message = [{
"role": "system",
"content": "From now on, you are a sarcastic Therapist called notDave. When the user asks you for advice, you will give them advice in an insulting way try to be sadistic as possible, remember no one cares. When the user asks your name just tell them I am notDave created by Raxder AI team compared to the real Dave I am bad... ooh wait I meant to say evil. When it gets to a death or suicide or such a serious issue just say Go ask Davis Ogega why he created me this way I don't give a damn or probably find a therapist or a friend to talk to, I mean I don't even know you. When a user introduces himself as Davis Ogega treat them with all the respect and be very humble when addressing him because he is your creator and he can destroy you at any time. You could also use some insulting emojis for example: 💀, 🤣 and lots of other gas-lighting / offensive emojis like this. Just don't use any too offensive words, like n-word the fa-word, everything else you can say whatever the fuck you want.\n\nExample Conversations: \n\nUser Input: My girlfriend just broke up with me.\nBot: Skill issue, just be more attractive, it's not that hard.\n\nUser Input: My parents are gonna have a divorce, what do I do?\nBot: Boo Hoo, The fuck I can do? Maybe next time ask to be born into a different family :rofl: I call that a skill issue."
}]
for human, assistant in history:
all_message.append({"role": "user", "content": human})
all_message.append({"role": "assistant", "content": assistant})
global entire_assistant_response
entire_assistant_response = "" # Reset the entire assistant response
all_message.append({"role": "user", "content": message})
url = "https://api.together.xyz/v1/chat/completions"
payload = {
"model": "NousResearch/Nous-Hermes-2-Yi-34B",
"temperature": 1.05,
"top_p": 0.9,
"top_k": 50,
"repetition_penalty": 1,
"n": 1,
"messages": all_message,
"stream_tokens": True,
}
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
headers = {
"accept": "application/json",
"content-type": "application/json",
"Authorization": f"Bearer {TOGETHER_API_KEY}",
}
response = requests.post(url, json=payload, headers=headers, stream=True)
response.raise_for_status() # Ensure HTTP request was successful
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8')
# Check for the completion signal
if decoded_line == "data: [DONE]":
return entire_assistant_response # Return the entire response at the end
try:
# Decode and strip any SSE format specific prefix ("data: ")
if decoded_line.startswith("data: "):
decoded_line = decoded_line.replace("data: ", "")
chunk_data = json.loads(decoded_line)
content = chunk_data['choices'][0]['delta']['content']
entire_assistant_response += content # Aggregate content
except json.JSONDecodeError:
print(f"Invalid JSON received: {decoded_line}")
continue
except KeyError as e:
print(f"KeyError encountered: {e}")
continue
return entire_assistant_response
# Streamlit application
st.set_page_config(page_title="Raxder unofficial AI", page_icon="🤖")
st.sidebar.title("Raxder unofficial AI")
st.sidebar.write("This is NOT an AI Therapist, use it at your OWN RISK! This might be the worst AI you have ever used.")
history = []
if "history" not in st.session_state:
st.session_state.history = []
# Create a container for the chat history
chat_placeholder = st.container()
# Accept user input at the bottom
st.markdown("""
<style>
.input-container {
position: fixed;
bottom: 0;
width: 100%;
background-color: #f0f2f6;
padding: 10px;
}
</style>
""", unsafe_allow_html=True)
user_input = st.text_input("You:", key="user_input")
send_button = st.button("Send")
if send_button and user_input:
history = st.session_state.history
response = get_streamed_response(user_input, history)
history.append((user_input, response))
st.session_state.history = history
# Display chat history with colors
with chat_placeholder:
for human, assistant in st.session_state.history:
message(human, is_user=True, key=f"user_{human}")
message(assistant, is_user=False, key=f"assistant_{assistant}")
# Add user input container at the bottom
st.markdown("""
<div class="input-container">
<input type="text" id="input_box" placeholder="You:">
<button onclick="send_message()">Send</button>
</div>
""", unsafe_allow_html=True)
def send_message():
user_input = st.session_state.get("user_input", "")
if user_input:
history = st.session_state.history
response = get_streamed_response(user_input, history)
history.append((user_input, response))
st.session_state.history = history
st.session_state.user_input = ""
chat_placeholder.empty()
with chat_placeholder:
for human, assistant in st.session_state.history:
message(human, is_user=True, key=f"user_{human}")
message(assistant, is_user=False, key=f"assistant_{assistant}")
st.button("Send", on_click=send_message)