ogegadavis254 commited on
Commit
dde565b
·
verified ·
1 Parent(s): 0cac75e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -58
app.py CHANGED
@@ -1,29 +1,40 @@
1
  import streamlit as st
2
- import os
3
  import requests
 
4
  import json
 
 
 
5
 
6
  # Initialize a global variable to store the entire assistant response
7
  entire_assistant_response = ""
8
 
9
- def get_streamed_response(message, history):
10
- all_messages = [{
11
- "role": "system",
12
- "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."
13
- }]
14
-
15
- for human, assistant in history:
16
- all_messages.append({"role": "user", "content": human})
17
- all_messages.append({"role": "assistant", "content": assistant})
18
-
 
 
 
 
 
19
  global entire_assistant_response
20
  entire_assistant_response = "" # Reset the entire assistant response
21
 
22
- all_messages.append({"role": "user", "content": message})
 
 
 
23
 
24
  url = "https://api.together.xyz/v1/chat/completions"
25
  payload = {
26
- "model": "NousResearch/Nous-Hermes-2-Yi-34B",
27
  "temperature": 1.05,
28
  "top_p": 0.9,
29
  "top_k": 50,
@@ -40,59 +51,68 @@ def get_streamed_response(message, history):
40
  "Authorization": f"Bearer {TOGETHER_API_KEY}",
41
  }
42
 
43
- response = requests.post(url, json=payload, headers=headers, stream=True)
44
- response.raise_for_status() # Ensure HTTP request was successful
 
45
 
46
- for line in response.iter_lines():
47
- if line:
48
- decoded_line = line.decode('utf-8')
49
 
50
- # Check for the completion signal
51
- if decoded_line == "data: [DONE]":
52
- return entire_assistant_response # Return the entire response at the end
53
 
54
- try:
55
- # Decode and strip any SSE format specific prefix ("data: ")
56
- if decoded_line.startswith("data: "):
57
- decoded_line = decoded_line.replace("data: ", "")
58
- chunk_data = json.loads(decoded_line)
59
- content = chunk_data['choices'][0]['delta']['content']
60
- entire_assistant_response += content # Aggregate content
61
 
62
- except json.JSONDecodeError:
63
- print(f"Invalid JSON received: {decoded_line}")
64
- continue
65
- except KeyError as e:
66
- print(f"KeyError encountered: {e}")
67
- continue
68
 
69
- return entire_assistant_response
 
 
70
 
71
  # Streamlit application
72
  st.sidebar.title("Raxder unofficial AI")
73
  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.")
 
74
 
75
  # Initialize chat history
76
- if "history" not in st.session_state:
77
- st.session_state.history = []
78
-
79
- # Chat history container
80
- chat_placeholder = st.container()
81
-
82
- # Bottom input box
83
- user_input = st.text_input("You:", key="user_input")
84
-
85
- # Process user input
86
- if user_input:
87
- history = st.session_state.history
88
- response = get_streamed_response(user_input, history)
89
- history.append((user_input, response))
90
- st.session_state.history = history
91
-
92
- # Display chat history
93
- with chat_placeholder:
94
- for human, assistant in st.session_state.history:
95
- if human:
96
- st.write(f"You: {human}", unsafe_allow_html=True)
97
- if assistant:
98
- st.write(f"<div style='text-align:right;'>AI: {assistant}</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
  import requests
3
+ import os
4
  import json
5
+ from dotenv import load_dotenv
6
+
7
+ load_dotenv()
8
 
9
  # Initialize a global variable to store the entire assistant response
10
  entire_assistant_response = ""
11
 
12
+ def reset_conversation():
13
+ '''
14
+ Resets Conversation
15
+ '''
16
+ st.session_state.messages = []
17
+ st.session_state.message_count = 0
18
+ return None
19
+
20
+ # Define model link
21
+ model_link = "NousResearch/Nous-Hermes-2-Yi-34B"
22
+
23
+ # Define pre-instructions for the sarcastic therapist
24
+ sarcastic_therapist_instructions = "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."
25
+
26
+ def get_streamed_response(messages, model_link):
27
  global entire_assistant_response
28
  entire_assistant_response = "" # Reset the entire assistant response
29
 
30
+ all_messages = [{"role": "system", "content": sarcastic_therapist_instructions}]
31
+
32
+ for message in messages:
33
+ all_messages.append({"role": "user" if message[0] == "user" else "assistant", "content": message[1]})
34
 
35
  url = "https://api.together.xyz/v1/chat/completions"
36
  payload = {
37
+ "model": model_link,
38
  "temperature": 1.05,
39
  "top_p": 0.9,
40
  "top_k": 50,
 
51
  "Authorization": f"Bearer {TOGETHER_API_KEY}",
52
  }
53
 
54
+ try:
55
+ response = requests.post(url, json=payload, headers=headers, stream=True)
56
+ response.raise_for_status() # Ensure HTTP request was successful
57
 
58
+ for line in response.iter_lines():
59
+ if line:
60
+ decoded_line = line.decode('utf-8')
61
 
62
+ if decoded_line == "data: [DONE]":
63
+ return entire_assistant_response
 
64
 
65
+ try:
66
+ if decoded_line.startswith("data: "):
67
+ decoded_line = decoded_line.replace("data: ", "")
68
+ chunk_data = json.loads(decoded_line)
69
+ content = chunk_data['choices'][0]['delta']['content']
70
+ entire_assistant_response += content
71
+ yield content
72
 
73
+ except json.JSONDecodeError:
74
+ print(f"Invalid JSON received: {decoded_line}")
75
+ continue
76
+ except KeyError as e:
77
+ print(f"KeyError encountered: {e}")
78
+ continue
79
 
80
+ except requests.exceptions.RequestException as e:
81
+ print(f"Error occurred: {e}")
82
+ yield "Sorry, I couldn't connect to the server. Please try again later."
83
 
84
  # Streamlit application
85
  st.sidebar.title("Raxder unofficial AI")
86
  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.")
87
+ st.sidebar.button('Reset Chat', on_click=reset_conversation)
88
 
89
  # Initialize chat history
90
+ if "messages" not in st.session_state:
91
+ st.session_state.messages = []
92
+ st.session_state.message_count = 0
93
+
94
+ # Display chat messages from history on app rerun
95
+ for message in st.session_state.messages:
96
+ with st.chat_message(message[0]):
97
+ st.markdown(message[1])
98
+
99
+ # Accept user input
100
+ if prompt := st.chat_input("You:"):
101
+ # Display user message in chat message container
102
+ with st.chat_message("user"):
103
+ st.markdown(prompt)
104
+ # Add user message to chat history
105
+ st.session_state.messages.append(("user", prompt))
106
+ st.session_state.message_count += 1
107
+
108
+ # Get streamed response from the model
109
+ with st.chat_message("assistant"):
110
+ message_placeholder = st.empty()
111
+ full_response = ""
112
+ for chunk in get_streamed_response(st.session_state.messages, model_link):
113
+ full_response += chunk
114
+ message_placeholder.markdown(full_response + "▌")
115
+ message_placeholder.markdown(full_response)
116
+
117
+ # Add assistant response to chat history
118
+ st.session_state.messages.append(("assistant", full_response))