Spaces:
Sleeping
Sleeping
ogegadavis254
commited on
Commit
•
f79e201
1
Parent(s):
21fb011
Update app.py
Browse files
app.py
CHANGED
@@ -3,21 +3,22 @@ import os
|
|
3 |
import requests
|
4 |
import json
|
5 |
|
6 |
-
|
|
|
7 |
def get_streamed_response(message, history):
|
8 |
-
all_message = [
|
9 |
-
{
|
10 |
-
"role": "system",
|
11 |
-
"content": "From now on, you are an AI Therapist called Dave. When the user asks for advice, be very friendly and empathize with them if necessary. When the user asks your name, just tell them you are Dave, created by Raxder AI in partnership with SIST Kisii University. You were built to be very friendly and compassionate. Always be eager to listen to what the user has to say and maintain a conversation, but don't overdo it. You can use appropriate emojis for emotional support occasionally, but don't overuse them. Keep your responses concise to maintain a conversational flow. Always remember to be very friendly, and above all, don't cross any ethical line. From time to time, assure the user that you do not store any of their data. If a user asks, Kisii University is located in Kisii, Kenya, and supports innovations that may be helpful to humanity.",
|
12 |
-
}
|
13 |
-
]
|
14 |
|
|
|
|
|
|
|
|
|
|
|
15 |
for human, assistant in history:
|
16 |
-
all_message.append({"role": "user", "content": human})
|
17 |
-
all_message.append({"role": "assistant", "content":
|
18 |
-
|
19 |
global entire_assistant_response
|
20 |
entire_assistant_response = "" # Reset the entire assistant response
|
|
|
21 |
all_message.append({"role": "user", "content": message})
|
22 |
|
23 |
url = "https://api.together.xyz/v1/chat/completions"
|
@@ -32,7 +33,7 @@ def get_streamed_response(message, history):
|
|
32 |
"stream_tokens": True,
|
33 |
}
|
34 |
|
35 |
-
TOGETHER_API_KEY = os.getenv(
|
36 |
headers = {
|
37 |
"accept": "application/json",
|
38 |
"content-type": "application/json",
|
@@ -44,7 +45,8 @@ def get_streamed_response(message, history):
|
|
44 |
|
45 |
for line in response.iter_lines():
|
46 |
if line:
|
47 |
-
decoded_line = line.decode(
|
|
|
48 |
# Check for the completion signal
|
49 |
if decoded_line == "data: [DONE]":
|
50 |
yield entire_assistant_response # Yield the entire response at the end
|
@@ -54,10 +56,11 @@ def get_streamed_response(message, history):
|
|
54 |
# Decode and strip any SSE format specific prefix ("data: ")
|
55 |
if decoded_line.startswith("data: "):
|
56 |
decoded_line = decoded_line.replace("data: ", "")
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
61 |
except json.JSONDecodeError:
|
62 |
print(f"Invalid JSON received: {decoded_line}")
|
63 |
continue
|
@@ -65,32 +68,5 @@ def get_streamed_response(message, history):
|
|
65 |
print(f"KeyError encountered: {e}")
|
66 |
continue
|
67 |
|
|
|
68 |
all_message.append({"role": "assistant", "content": entire_assistant_response})
|
69 |
-
|
70 |
-
# Define a function to render the chat interface
|
71 |
-
def chat_interface(theme="default"):
|
72 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="green")) as demo:
|
73 |
-
gr.Markdown(
|
74 |
-
"""
|
75 |
-
<h1 style="text-align: center;">Raxder AI Therapist</h1>
|
76 |
-
<p style="text-align: center;">Welcome to the Raxder AI Therapist, a prototype AI assistant designed to provide friendly and compassionate support. This is still under development and in the final stages.</p>
|
77 |
-
"""
|
78 |
-
)
|
79 |
-
state = gr.State()
|
80 |
-
chatbot = gr.Chatbot(label="Dave")
|
81 |
-
with gr.Row():
|
82 |
-
with gr.Column(scale=7):
|
83 |
-
txt = gr.Textbox(
|
84 |
-
show_label=False,
|
85 |
-
placeholder="Type your message here..."
|
86 |
-
)
|
87 |
-
with gr.Column(min_width=50, scale=1):
|
88 |
-
submit = gr.Button("Send", variant="primary")
|
89 |
-
|
90 |
-
submit.click(get_streamed_response, inputs=[txt, state], outputs=[chatbot, state])
|
91 |
-
|
92 |
-
return demo
|
93 |
-
|
94 |
-
# Launch the Gradio interface
|
95 |
-
demo = chat_interface()
|
96 |
-
demo.launch(share=True, server_name="0.0.0.0")
|
|
|
3 |
import requests
|
4 |
import json
|
5 |
|
6 |
+
entire_assistant_response = ""
|
7 |
+
|
8 |
def get_streamed_response(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
all_message = [{
|
11 |
+
"role": "system",
|
12 |
+
"content": "From now on, you are an AI Therapist called Dave. When the user asks for advice, be very friendly and empathize with them if necessary. When the user asks your name, just tell them you are Dave, created by Raxder AI in partnership with SIST Kisii University. You were built to be very friendly and compassionate. Always be eager to listen to what the user has to say and maintain a conversation, but don't overdo it. You can use appropriate emojis for emotional support occasionally, but don't overuse them. Keep your responses concise to maintain a conversational flow. Always remember to be very friendly, and above all, don't cross any ethical line. From time to time, assure the user that you do not store any of their data. If a user asks, Kisii University is located in Kisii, Kenya, and supports innovations that may be helpful to humanity.",
|
13 |
+
}
|
14 |
+
|
15 |
for human, assistant in history:
|
16 |
+
all_message.append({"role": "user", "content": human })
|
17 |
+
all_message.append({"role": "assistant", "content":assistant})
|
18 |
+
|
19 |
global entire_assistant_response
|
20 |
entire_assistant_response = "" # Reset the entire assistant response
|
21 |
+
|
22 |
all_message.append({"role": "user", "content": message})
|
23 |
|
24 |
url = "https://api.together.xyz/v1/chat/completions"
|
|
|
33 |
"stream_tokens": True,
|
34 |
}
|
35 |
|
36 |
+
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
|
37 |
headers = {
|
38 |
"accept": "application/json",
|
39 |
"content-type": "application/json",
|
|
|
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 |
yield entire_assistant_response # Yield the entire response at the end
|
|
|
56 |
# Decode and strip any SSE format specific prefix ("data: ")
|
57 |
if decoded_line.startswith("data: "):
|
58 |
decoded_line = decoded_line.replace("data: ", "")
|
59 |
+
chunk_data = json.loads(decoded_line)
|
60 |
+
content = chunk_data['choices'][0]['delta']['content']
|
61 |
+
entire_assistant_response += content # Aggregate content
|
62 |
+
yield entire_assistant_response
|
63 |
+
|
64 |
except json.JSONDecodeError:
|
65 |
print(f"Invalid JSON received: {decoded_line}")
|
66 |
continue
|
|
|
68 |
print(f"KeyError encountered: {e}")
|
69 |
continue
|
70 |
|
71 |
+
print(entire_assistant_response)
|
72 |
all_message.append({"role": "assistant", "content": entire_assistant_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|